Blog Stats
  • Posts - 42
  • Articles - 0
  • Comments - 61
  • Trackbacks - 36

 

XML Processing in C#

XML Processing in C# is nice and easy.
Just like in good old classic ASP, but using XmlReader etc in the System.Xml namespace now its much more sophisticated.
Here's the code:
First the example XML file:
xml version="1.0" encoding="utf-8" ?>
<Fields>
<Field text="Name : " length="8">Field>
<Field text="Email : " length="9">Field>
<Field text="Married : " length="2">Field>
<Field text="Occupation : " length="2">Field>
<Field text="Age : " length="3">Field>
<Field text="ALLERGIC : " length="1">Field>
<Field text="Item1 : " length="1">Field>
<Field text="Item2 : " length="1">Field>
<Field text="Item3 : " length="1">Field>
<Field text="Do you live : " length="">
<Field text="Alone : " length="">Field>
<Field text="Family : " length="">Field>
Field>
<Field text="COMMENTS : " length="">Field>
<Field text="BP : " length="">Field>
<Field text="DRINK : " length="">
<Field text="COFFEE : " length="">Field>
<Field text="CUPS Per Day : " length="">Field>
Field>
<Field text="SSN# : " length=""> Field>
<Field text="PRIMARY ADDRESS : " length="">Field>
<Field text="SECONDARY ADDRESS : " length="">Field>
<Field text="Date Time : " length="">Field>
<Field text="AM : " length="">Field>
<Field text="PM : " length="">Field>
Fields>
Place a button and a richtextbox 'rtbResultString' in a winform then in the class file.
Now in that C# class file:
private System.Windows.Forms.Button btnProcessXML;
private System.Windows.Forms.RichTextBox rtbResultString;
private string strReturnText = string.Empty;
private void btnProcessXML_Click(object sender, System.EventArgs e)
{
XmlTextReader reader =
new XmlTextReader("FieldNames.xml");

reader.WhitespaceHandling = WhitespaceHandling.All;
XmlDocument xmlDoc =
new XmlDocument();
//Load the file into the XmlDocument
xmlDoc.Load(reader);
//Close off the connection to the file.
reader.Close();
//Find the root node,
XmlNode xnod = xmlDoc.DocumentElement;
GetFormattedFieldString(xnod);
rtbResultString.Text = strReturnText;
}
# region GetFormattedFieldString Method
///
/// Constructs the 2D string array from the field text and length from XML.
///

/// XmlNode
private void GetFormattedFieldString(XmlNode xnod)
{
XmlNode fieldText, fieldLength;
string strFieldText = string.Empty;
//For an element node,
if (xnod.NodeType == XmlNodeType.Element)
{
//retrive the childnodes.
XmlNodeList mapChildNodes = xnod.ChildNodes;
foreach(XmlNode xnodChildNode in mapChildNodes)
{
// Retrieve field text attribute.
fieldText = xnodChildNode.SelectSingleNode("@text");
// Retrieve field length attribute.
fieldLength = xnodChildNode.SelectSingleNode("@length");
strReturnText = strReturnText + fieldText.Value + " : " + fieldLength.Value + "\n";
// strReturnText = strReturnText + GetFieldText(fieldText.Value, fieldLength.Value) + "\n";
// If has subfields, call the method recursively.
if (xnodChildNode.HasChildNodes )
{
GetFormattedFieldString(xnodChildNode);
}
}
}
}
# endregion

Feedback

# re: XML Processing in C#

Gravatar Great sample code! Thanks! 1/28/2005 3:27 AM | Mike

# re: XML Processing in C#

Gravatar GOOD CODE 4/22/2005 7:30 PM | iNDRAJEET KUMPAVAT

# re: XML Processing in C#

Gravatar really helpful 1/22/2006 5:42 PM | name

# re: XML Processing in C#

Gravatar really helpful 1/22/2006 5:42 PM | naresh

# re: XML Processing in C#

Gravatar Bad one 4/10/2008 7:03 AM | Swapnil

# re: XML Processing in C#

Gravatar Is there a reason why this page is broken in FF? Not very geeky! 6/4/2008 9:04 AM | Mikey

Post a comment





 

 

 

Copyright © Vinayak