Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML WPF IO XmlReader
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.
 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 2-Apr-12 18:33pm
cpquest1.8K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Comments
Navin Kumar K Subramanian at 3-Apr-12 0:35am
   
Hi thanks,
but how can i read and update the node values in the created file.
Navin Kumar K Subramanian at 3-Apr-12 0:45am
   
And also how can i Generate XML using a Collection i mean using ObservableCollection or Similar type.
Navin Kumar K Subramanian at 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)

  Print Answers RSS
0 OriginalGriff 455
1 Sergey Alexandrovich Kryukov 312
2 DamithSL 179
3 King Fisher 179
4 Peter Leow 154
0 OriginalGriff 240
1 Maciej Los 95
2 Sascha Lefévre 85
3 King Fisher 70
4 F-ES Sitecore 55


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 2 Apr 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100