Saturday, February 17, 2007

Xml in .NET

The XML types are live in System.XML namespace

1. Import System.XML like

using System.XML // now we are ready to use xml in .net

2. we have two core abstract classes in XML.NET XmlReader and XmlWriter

XmlReader : Fast,read-only and Forward only cursor for processing xml documents
Xml Writer : Alows to produce xml documents with W3C's XML 1.0 + Namespaces Rcommendations

These two are inexpensive in memory cache which makes as alternatives to the classic DOM approach

How to Read XML Document in XML.NET ?
Reading xml document with XmlReader class , here is the example

public void ReadDocument(XmlReader reader)
{

while (reader.Read())
{
// Points to the Current Node of the Xml Document
String name = (reader.NodeType == XmlNodeType.Element ||
reader.NodeType == XmlNodeType.EndElement) ?
reader.NamespaceURI + "#" + reader.Name : reader.Name;
Console.WriteLine(name);
}
}


How to Write into XML Document in XML.NET


public void WriteDocument()
{
XmlWriter writer = new XmlTextWriter(Server.MapPath("test.xml"), null);
writer.WriteStartDocument();
writer.WriteComment("This is a sample comment");
writer.WriteStartElement("Carworld");
writer.WriteStartElement("name", "");
writer.WriteString("Maruthi");
writer.WriteEndElement();
writer.WriteStartElement("name", "");
writer.WriteString("Hyundai");
writer.WriteEndElement();
writer.WriteStartElement("name", "");
writer.WriteString("Daewoo");
writer.WriteEndElement();
writer.WriteEndElement();


writer.WriteEndDocument();

writer.Close();

}

How to Read or write with XmlDocument ?


1. assaign the xml file to XmlDocument object with Load method
2. Start from the Root Element and Display the XmlNode details
3.Check if that Node contains Child Nodes if so process them too ..

public void ReadXmlDocument()
{
string fname = "test.xml";
try
{
// Load the XML from file
XmlDocument myDoc = new XmlDocument();
myDoc.Load(Server.MapPath(fname));
// Process the supplied XML file
ProcessXmlFile(myDoc.DocumentElement);
}
catch (Exception e)
{
Response.Write(ex.Message());
}
}

public void ProcessXmlFile(XmlNode node)
{
if (node != null)
Response.Write(" Node Name :: + "+node.Name +" --> Node Value :: "+node.Value+"<br>");

if (node.HasChildNodes)
{
node = node.FirstChild;
while (node != null)
{
ProcessXmlFile(node);
node = node.NextSibling;
}
}

}


In the above example ... ProcessXmlFile() is a simple and recursive fuction to traverse in between xml node elements,


How to Process Xml Nodes with XmlNode.SelectNodes()?

if you know under which XmlNode your required element is .. then XmlNode.SelectNodes() will work fine ...

Here is a example of How XmlNode.SelectNodes() will work


public void ProcessDynamic()
{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("test.xml"));
string str = "";
XmlNodeList list;
str = "/carworld/name";
list = doc.SelectNodes(str);
this.ddlcamp.Items.Clear();

ListItem li = new ListItem();

foreach (XmlNode xn in list)
{
if ( xn.Value=="maruthi")
li = new ListItem();
li.Text = cn.Attributes["name"].Value;
li.Value = cn.Attributes["name"].Value;
this.ddlcamp.Items.Add(li);

}
}

}

Happy Coding :)

5 comments:

Anonymous said...

Thanks!!
I had a problem with the encoding of my XML generated file and you gave me the solution of writing 'null' as the encoding...

Anonymous said...

how to remove "<" and ">" from xml string i am unable to load xml string in the xmldocument.

My webservice returning values like that, how to read webservice xml response string message.

please help me....

Anonymous said...

how to remove & l t ; and & g t ;

Anonymous said...

I wish not acquiesce in on it. I regard as precise post. Expressly the title-deed attracted me to study the whole story.

Anonymous said...

Amiable fill someone in on and this post helped me alot in my college assignement. Thanks you as your information.