2

I have one xml file name "person.xml" with structure like this

<?xml version="1.0" encoding="utf-8" ?>
<people>
    <person id="1" name="" age="" />
    <person id="2" name="" age="" />
    <person id="3" name="" age="" />
</people>

I want to update the person id 1 over its name and age; my code to do that is like this

using(IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
    filename = "person.xml";
    if(storage.FileExists(filename))
    {
        using(IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(filename,FileMode.Open,storage))
        {
            XDocument doc = XDocument.Load("person.xml");
            foreach (var item in (from item in doc.Descendants("person")
                                  where item.Attribute("id").Equals("1")
                                  select item).ToList())
            {
                item.Attribute("name").SetValue("Hello");
                item.Attribute("age").SetValue("24");
            }
            doc.Save(isoStream);
        }
    }
}

However, after I run the application and check the value in person.xml, I saw the value on person id 1 has never been updated. As I debuge the application during runtime, the breakpoint skipped at line "if(storage.FileExists(filename))" as though the file person.xml does not exist but I did create it and see it in the solution explorer.

Last, please forgive me I know that my question might be duplicated as one I followed on updating an existing xml file in Windows Phone and the other sources on web, yet following them, I've still never abled to solve it by own problem.

Community
  • 1
  • 1
Houy Narun
  • 1,557
  • 5
  • 37
  • 86
  • 3
    I would suggest that your issue is that you are looking in Isolated Storage for person.xml. My belief is that Isolated storage doesn't contain any of the files that you create within your solution, which is why your software believes it doesn't exist. – JayDev Sep 24 '14 at 22:43
  • "...This method can be used only when the application identity can be determined - for example, when the application is published through ClickOnce deployment or is a Silverlight-based application." http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.getuserstoreforapplication%28v=vs.110%29.aspx – joym8 Sep 25 '14 at 00:14
  • @greatbear302 what is application identity? how can I find it and create one for my app ? – Houy Narun Sep 25 '14 at 10:19
  • @JayDev how can I make sure it does exist in Isolated storage and simply create it in Isolated storage? sorry that I ask much because I only want to know and learn, thanks – Houy Narun Sep 25 '14 at 10:24
  • Could you not simply add an else to your if else statement and if the file does not exist then create the file rather than editting it?..try something like what is on this thread http://stackoverflow.com/questions/2948255/xml-file-creation-using-xdocument-in-c-sharp – JayDev Sep 25 '14 at 17:05

0 Answers0