[C# – XML] XML Processing

There are several ways to process XML documents in C#. LINQ can be a smart choice for many developers. But this article focuses on the legacy types in  .NET (located “System.Xml” and “System.Xml.xxx” namespaces)

 

1. .NET XML Namespaces

  • System.Xml: provides standard-based support for processing XML
  • System.Xml.Schema: provides standard-based support for XML schemas
  • System.Xml.Serialization: contains classes that are used to serialize objects into XML format documents or streams
  • System.Xml.XPath: contains XPath parser and evaluation engine
  • System.Xml.Xsl: provides support for XSL Transformation(XSLT)

 

2. The Supported Standards

The System.Xml namespace provides standard-based support for processing XML.

The supported standards are:

  • XML 1.0
  • XML Namespaces
  • XSD Schemas
  • XPath Expressions
  • XSLT Transformations
  • DOM Level 1 & DOM Level 2

 

3. .NET XML Classes

  • System.Xml.XmlReader: read well-formed XML with/without validation (DTD and XML Schema definition language (XSD) )
  • System.Xml.XmlWriter: write well-formed XML
  • System.Xml.XmlDocument: represents an XML document, implements W3C DOM model
  • System.Xml.XPath.XPathNavigator: provides XPath data model over any data store
  • System.Xml.Xsl.XslTransform: transforms XML data using an XSLT style sheet

 

4. DOM classes

  • XmlDocument: represents an XML document
  • XmlNode: represents a single node in the XML document
  • XmlElement: represents an element
  • XmlAttribute: represents an attribute

The “XmlNode” is an abstract class that represents a single node. The following properties are used to get the information of a node:

public string Name { get; }
public XmlNodeType NodeType { get; } // Element, Attribute, Text, Comment, Document, ProcessingInstruction, etc...
public XmlAttributeCollection Attributes { get; } // the attributes of this node
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; }

XmlDocument” and “XmlAttribute” are the direct subclasses of “XmlNode“.

XmlElement” is not a direct sublcass of “XmlNode“. It is derived from “XmlLinkedNode” class, which dervies from “XmlNode“. “XmlLinkedNode” represents a node that can be immediately linked to another node.

public XmlNode NextSibling { get; }
public XmlNode PreviousSibling { get; }

 

5. XML classes

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