Working with XML files

Quick and dirty write to an XML file:

string filename = @"C:\XMLfile" + ".xml";
XmlDocument xmlDoc = new XmlDocument();
XmlNode xmlRoot, xmlNode;

xmlRoot = xmlDoc.CreateElement("Root");
xmlDoc.AppendChild(xmlRoot);
xmlNode = xmlDoc.CreateElement("Name");
xmlRoot.AppendChild(xmlNode);
xmlNode.InnerText = "Martin";
xmlDoc.Save(filename);

Quick and dirty read from an XML file:

string filename = @"C:\XMLfile" + ".xml";
XmlTextReader xReader = new XmlTextReader(filename);
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(filename);

string strName = xmlDoc.DocumentElement.ChildNodes[0].InnerText;
label1.Text = strName;

xReader.Close();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s