Monday, December 29, 2008

Read XML in C#.net

We can use File object to read XML file but we mostly use XML parsing.

.NET provides five namespace to support XML classes.
  1. System.Xml;
  2. .Xml.Schema;
  3. .Xml.Serialization;
  4. .Xml.XPath;
  5. .Xml.Xsl;
The System.Xml namespace contains major XML classes.

The XmlReader class is an abstract bases classes and contains methods and properties to read a document.
The XmlTextReader, XmlNodeReader and XmlValidatingReader classes are derived from XmlReader class. As their name explains, they are used to read text, node, and schemas.
The XmlWrite class contains functionality to write data to XML documents.
XmlNode class represents a single node of XML but that could be the root node of an XML document and could represent the entire file. Three major classes derived from XmlNode are XmlDocument, XmlDataDocument and XmlDocumentFragment. XmlDocument class represents an XML document and provides methods and properties to load and save a document. It also provides functionality to add XML items such as attributes, comments, spaces, elements, and new nodes. The Load and LoadXml methods can be used to load XML documents and Save method to save a document respectively.

Reading XML Documents

XmlTextReader xmlReader = new XmlTextReader("D:\\test.xml");

The XmlTextReader, XmlNodeReader and XmlValidatingReader classes are derived from XmlReader class. Besides XmlReader methods and properties, these classes also contain members to read text, node, and schemas respectively. I am using XmlTextReader class to read an XML file. You read a file by passing file name as a parameter in constructor.

XmlTextReader xmlReader = new XmlTextReader("D:\\test.xml");

Then call read() method

xmlReader.read();


using System;

using System.Xml;

namespace ReadXml

{

class Class1

{

static void Main(string[] args)

{

// Create an isntance of XmlXmlReader and call Read method to read the file

XmlXmlReader xmlReader = new XmlXmlReader("D:\\test.xml");

xmlReader.Read();

// If the node has value

while (xmlReader.Read())

{

// Move to fist element

xmlReader.MoveToElement();

Console.WriteLine("XmlXmlReader Properties Test");

Console.WriteLine("===================");

// Read this element's properties and display them

Console.WriteLine("Name:" + xmlReader.Name);

Console.WriteLine("Base URI:" + xmlReader.BaseURI);

Console.WriteLine("Local Name:" + xmlReader.LocalName);

Console.WriteLine("Attribute Count:" + xmlReader.AttributeCount.ToString());

Console.WriteLine("Depth:" + xmlReader.Depth.ToString());

Console.WriteLine("Line Number:" + xmlReader.LineNumber.ToString());

Console.WriteLine("Node Type:" + xmlReader.NodeType.ToString());

Console.WriteLine("Attribute Count:" + xmlReader.Value.ToString());

}

}

}

}




Writing XML Documents

XmlTextWriter textWriter = new XmlTextWriter("D:\\testWrite.xml", null) ;



using System;

using System.Xml;

namespace ReadingXML2

{

class Class1

{

static void Main(string[] args)

{

// Create a new file in C:\\ dir

XmlTextWriter textWriter = new XmlTextWriter("D:\\testWrite.xml", null);

// Opens the document

textWriter.WriteStartDocument();

// Write comments

textWriter.WriteComment("First Comment XmlTextWriter Sample Example");

textWriter.WriteComment("myXmlFile.xml in root dir");

// Write first element

textWriter.WriteStartElement("Student");

textWriter.WriteStartElement("r", "RECORD", "urn:record");

// Write next element

textWriter.WriteStartElement("Name", "");

textWriter.WriteString("Student");

textWriter.WriteEndElement();

// Write one more element

textWriter.WriteStartElement("Address", ""); textWriter.WriteString("Colony");

textWriter.WriteEndElement();

// WriteChars

char[] ch = new char[3];

ch[0] = 'a';

ch[1] = 'r';

ch[2] = 'c';

textWriter.WriteStartElement("Char");

textWriter.WriteChars(ch, 0, ch.Length);

textWriter.WriteEndElement();

// Ends the document.

textWriter.WriteEndDocument();

// close writer

textWriter.Close();

}

}

}




No comments:

Post a Comment