locked
How to add elements in xml file in windows apps RRS feed

  • Question

  • Hi, im trying to add elements in an xml file but all the methods ive seen arent working in windows store apps because the definations for Load and Save dont exist.

    For example, the xml i want to add elements to looks like:

    <?xml version="1.0" encoding="utf-8" ?> 
    <records>
    	<studentdetails name="John" />
    	<studentdetails name="Alice" />
    	<studentdetails name="Martin" />
    </records>


    And i want the saved file to look like:

    <?xml version="1.0" encoding="utf-8" ?> 
    <records>
    	<studentdetails name="John" />
    	<studentdetails name="Alice" />
    	<studentdetails name="Martin" />
    	<studentdetails name="Joseph" />
    </records

    Ive tried various methods for adding data to xml file but none of them seem to work. It usually looks like:(Errors are commented)

     XmlDocument xmlDocument = new XmlDocument();
    
                xmlDocument.Load("file.xml");//Windows.Data.Xml.Dom.XmlDocument doesnt contain a defination for Load....
    
                XmlElement studentdetails = xmlDocument.CreateElement("studentdetais");
    
                studentdetails.SetAttribute("name", "Joseph");
    
                xmlDocument.get_DocumentElement().AppendChild(studentdetails);
    
                xmlDocument.Save("file.xml");//Windows.Data.Xml.Dom.XmlDocument doesnt contain a defination for Save...


    And thats the problem, the errors that am getting. Am using VS2012 so i dont know if Visual Studio 2012 that has the problem or am not getting it right. Would appreciate if i was shown what to do. Thanks.





    • Edited by Garshdem Tuesday, July 14, 2015 12:48 PM
    Tuesday, July 14, 2015 11:57 AM