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