Quick reminder on how to create an XML file in C#

1) Declare the XML  document

XmlDocument xmlDoc = new XmlDocument();

2) Add the XML Declaration Statement

XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);

3) Create the Root Elements can be done in two different ways

  • First way utilites the CreateElement function
XmlElement rootNode  = xmlDoc.CreateElement("Family");
xmlDoc.AppendChild(rootNode);
  • Second way is more gritty with a string
string documentStart = "<Family></Family>";
xmlDocument.Load(new StringReader(documentStart));

4) Create Parent and Child Elements

XmlElement parentNode  = xmlDoc.CreateElement("Parent");
XmlElement childNode  = xmlDoc.CreateElement("Child");
parentNode.AppendChild(childName);
Advertisements
  1. Leave a comment

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

%d bloggers like this: