문제

Windows Phone의 XML 파일에 데이터를 저장하려면 코드 아래를 사용하고 있습니다.먼저 대상 XML 파일이 격리 된 저장소에 있는지 여부를 확인하고 있습니다. 존재하지 않으면 파일을 작성하고 필요한 요소 데이터를 추가하고 있습니다.파일이 있으면 먼저 속성 값을 업데이트하는 경우 요소가 이미 있는지 여부를 확인하십시오. 그렇지 않으면 XML 파일에 새 요소를 추가하십시오.

이미 표시하는 문제는 이미 요소가 있고 속성을 업데이트하려고합니다 (w / 아래 코드) - 새 데이터가 추가 된 추가 요소가 추가되고 이전 데이터가 파일에 여전히 존재합니다.대신 맵핑되지 않습니다.

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (storage.FileExists(fileName))
                {
                    using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
                    {
                        XDocument doc = XDocument.Load(isoStream);

                        bool isUpdated = false;
                        foreach (var item in (from item in doc.Descendants("Employee")
                                              where item.Attribute("name").Value.Equals(empName)
                                              select item).ToList())
                        {
                            // updating existing employee data
                            // element already exists, need to update the existing attributes
                            item.Attribute("name").SetValue(empName);
                            item.Attribute("id").SetValue(id);
                            item.Attribute("timestamp").SetValue(timestamp);

                            isUpdated = true;
                        }

                        if (!isUpdated)
                        {
                            // adding new employee data
                            doc.Element("Employee").Add(
                                    new XAttribute("name", empName),
                                    new XAttribute("id", id),
                                    new XAttribute("timestamp", timestamp));
                        }

                        doc.Save(isoStream);
                    }
                }
                else
                {
                    // creating XML file and adding employee data
                    using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
                    {
                        XDocument doc = new XDocument(new XDeclaration("1.0", "utf8", "yes"),
                            new XElement("Employees",
                                new XElement("Employee",
                                    new XAttribute("name", empName),
                                    new XAttribute("id", id),
                                    new XAttribute("timestamp", timestamp))));

                        doc.Save(isoStream, SaveOptions.None);
                    }
                }
            }
.

도움이 되었습니까?

해결책

열린 스트림 위치를 0으로 설정하거나 새로 열린 스트림에 XML 문서를 저장하십시오.

XDocument doc = null;

using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
{
    doc = XDocument.Load(isoStream);
    bool isUpdated = false;
    foreach (var item in (from item in doc.Descendants("Employee")
                     where item.Attribute("name").Value.Equals(empName)
                     select item).ToList())
    {
        // updating existing employee data
        // element already exists, need to update the existing attributes
        item.Attribute("name").SetValue(empName);
        item.Attribute("id").SetValue(id);
        item.Attribute("timestamp").SetValue(timestamp);

        isUpdated = true;
    }

    if (!isUpdated)
    {
        // adding new employee data
        doc.Element("Employee").Add(
                    new XAttribute("name", empName),
                    new XAttribute("id", id),
                    new XAttribute("timestamp", timestamp));
    }      

    //First way
    //isoStream.Position = 0;
    //doc.Save(isoStream);                  
}

//Or second way
using (var stream = storage.OpenFile(fileName, FileMode.Open, FileAccess.Write))
{
    doc.Save(stream);
}       
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top