[C# – XML DOM] XmlDocument

System.Xml.XmlDocument” represents an XML document in DOM(Document Object Model). LINQ’s “XDocument” provides the much easier way to process XML documents and fragments but “XmlDocument” can be the way to process XML documents using DOM APIs.

 

1. Loading XML Document with “XmlDocument” Class

“XmlDocument” implements the W3C Document Object Model (DOM) Level 1 Core and the Core DOM Level 2.

The “Load()” method can load as XML document from a local file or from a URL via the web.

  • public void Load(Stream inStream)
  • public void Load(string filename)
  • public void Load(TextReader txtReader)
  • public void Load(XmlReader reader)

The “Load()” method parses the specified XML document and builds an in-memory representation of it. If the document is not well-formed, XmlException will be thrown.

XmlDocument doc = new XmlDocument();

try
{
  doc.Load("book.xml");
  System.Console.WriteLine("Success : Well-formedness Document");
}
catch(XmlException e)
{
  Console.WriteLine("XML document is not well-formed: {0}", e.Message);
}

 

2. XmlNode class

The abstract “XmlNode” class represents a single node in the XML document.

public string Name { get; }
public XmlNodeType NodeType { get; } // Element, Attribute, Text, Comment, Document, ProcessingInstruction, etc...
public XmlAttributeCollection Attributes { get; }
public XmlNode ParentNode { get; }
public bool HasChildNodes { get; }
public XmlNodeList ChildNodes { get; }
public XmlNode FirstChild { get; }
public XmlNode LastChild { get; }
public string Value { get; set; }

 

3. Parsing XML using XmlDocument

The “DocumentElement” property of “XmlDocument” returns the root “XmlElement” (subclass of “XmlNode”) for the document.

  • public XmlElement DocumentElement {get;}

Now, you can parse XML documents like this:

  • Create a blank “XmlDocument” object
  • Load an XML document using one of  “Load()” methods
  • Gets the root element (“DocumentElement” property)
  • Gets the elements and atrributes recursively
public static void LoadXmlDocument()
{
  XmlDocument doc = new XmlDocument();
  try
  {
    doc.Load("book.xml");
    processNode(doc.DocumentElement);
  }
  catch(XmlException e)
  {
    Console.WriteLine("XML document is not well-formed: {0}", e.Message);
  }
}

private static void processNode(XmlNode node)
{
  System.Console.WriteLine("{0}: {1} - {2}", node.NodeType, node.Name, node.Value);
  if (node.Attributes != null)
  {
    foreach (XmlAttribute attr in node.Attributes)
    {
      System.Console.WriteLine("{0}: {1} - {2}", attr.NodeType, attr.Name, attr.Value);
    }
  }
  if (node.HasChildNodes)
  {
    foreach (XmlNode child in node.ChildNodes)
    {
      processNode(child);
    }
  }
}

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s