2014-02-27 6 views
0

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

이미 요소가 존재하고 속성 (w/below 코드)을 업데이트하려고하면 문제가 발생합니다. 새로운 데이터로 추가 된 추가 요소가 표시되고 이전 데이터가 파일에 계속 표시됩니다. 추가하는 대신 업데이트하지 않습니다.

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

... – har07

+0

내가이 예상대로 일하고, 콘솔 응용 프로그램을 시도했다. 그러나 WP에서는 예상대로 작동하지 않습니다. 나는 내가 뭘 잘못하고 있는지 확신하지 못한다. – Mahender

답변

1

열려있는 스트림 위치를 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); 
}  
+0

감사합니다 Jan, 처음으로 스트림 위치를 재설정하여 문제가 해결되었습니다. – Mahender

관련 문제