Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

i have some doubts in creating xml from a class that i have. I need to grenerate a xml file in my local filesystem the Nodes and Data of a xml should be Generated once from the first time. What ever changed made in the data should be saved in the genereated XML in my local file system using the logic some thing as below.
C#
XmlDocument doc = new XmlDocument();
           doc.LoadXml("<item><name>wrench</name></item>");

           // Add a price element.
           XmlElement newElem = doc.CreateElement("price");
           newElem.InnerText = "10.95";
           doc.DocumentElement.AppendChild(newElem);

           // Save the document to a file. White space is
           // preserved (no white space).
           doc.PreserveWhitespace = true;

           doc.Save("" + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + UserPrincipal.Current.DisplayName.Replace(' ', '_') + ".xml");


The above example is what i got from MSDN which works fine. what i need is in the LoadXML or using some other method i need to create a XML Using my Custom Class.
Posted

1 solution

I guess what you want is XML Serialization to directly store the structure of your objects. See here for more information:
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization[^]
http://support.microsoft.com/kb/815813/en-us[^]
 
Share this answer
 
Comments
[no name] 3-Apr-12 0:35am    
Hi thanks,
but how can i read and update the node values in the created file.
[no name] 3-Apr-12 0:45am    
And also how can i Generate XML using a Collection i mean using ObservableCollection or Similar type.
[no name] 3-Apr-12 1:56am    
Hi,
i have got the answer here is the code

var imageFile = new FileInfo("test.xml");
if (!imageFile.Exists)
CreateDefaultXML();
XElement root = XElement.Load("test.xml");
root.Element("Text").SetValue("Hi updated node");
root.Save("test.xml");

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900