Click here to Skip to main content
11,496,146 members (464 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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();
            // Add a price element.
            XmlElement newElem = doc.CreateElement("price");
            newElem.InnerText = "10.95";
            // 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

1 solution

I guess what you want is XML Serialization to directly store the structure of your objects. See here for more information:[^][^]
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
i have got the answer here is the code

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web01 | 2.8.150520.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