finally{}
My interactions with .Net and Web development - What's most important is what happens finally{}
Blog Stats
Posts - 42
Articles - 0
Comments - 98
Trackbacks - 34
Home
Archives
Contact
Login
<< Splitting delimited characters into array
|
Home
|
Evolution of User Interface >>
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">< FONT>
Field
>
<
Field
text
="Email : "
length
="9">< FONT>
Field
>
<
Field
text
="Married : "
length
="2">< FONT>
Field
>
<
Field
text
="Occupation : "
length
="2">< FONT>
Field
>
<
Field
text
="Age : "
length
="3">< FONT>
Field
>
<
Field
text
="ALLERGIC : "
length
="1">< FONT>
Field
>
<
Field
text
="Item1 : "
length
="1">< FONT>
Field
>
<
Field
text
="Item2 : "
length
="1">< FONT>
Field
>
<
Field
text
="Item3 : "
length
="1">< FONT>
Field
>
<
Field
text
="Do you live : "
length
="">
<
Field
text
="Alone : "
length
="">< FONT>
Field
>
<
Field
text
="Family : "
length
="">< FONT>
Field
>
< FONT>
Field
>
<
Field
text
="COMMENTS : "
length
="">< FONT>
Field
>
<
Field
text
="BP : "
length
="">< FONT>
Field
>
<
Field
text
="DRINK : "
length
="">
<
Field
text
="COFFEE : "
length
="">< FONT>
Field
>
<
Field
text
="CUPS Per Day : "
length
="">< FONT>
Field
>
< FONT>
Field
>
<
Field
text
="SSN# : "
length
="">
< FONT>
Field
>
<
Field
text
="PRIMARY ADDRESS : "
length
="">< FONT>
Field
>
<
Field
text
="SECONDARY ADDRESS : "
length
="">< FONT>
Field
>
<
Field
text
="Date Time : "
length
="">< FONT>
Field
>
<
Field
text
="AM : "
length
="">< FONT>
Field
>
<
Field
text
="PM : "
length
="">< FONT>
Field
>
< FONT>
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
Share This Post:
Print
posted @ Wednesday, November 10, 2004 3:07 PM
Feedback
#
re: XML Processing in C#
GOOD CODE
4/22/2005 7:30 PM |
iNDRAJEET KUMPAVAT
#
re: XML Processing in C#
really helpful
1/22/2006 5:42 PM |
name
#
re: XML Processing in C#
really helpful
1/22/2006 5:42 PM |
naresh
#
re: XML Processing in C#
Bad one
4/10/2008 7:03 AM |
Swapnil
#
re: XML Processing in C#
Is there a reason why this page is broken in FF? Not very geeky!
6/4/2008 9:04 AM |
Mikey
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
Enter the code shown above
Remember Me?
News
I am currently working on....
C#
XML
XSLT
Javascript, DHTML
VSIP- Visual Studio Extensibility
Winforms
ASP.Net 2.0
Web Services
with Microsoft .Net 2.0
Recent Comments
good article
by JITU
Thanks. I was really stuck on that one. Just put...
by Matthew Theroux
really helpful.
by iSat
thanks, it was very helpfull
by dolev
I use sonething like preparedStatement.setBytes(2,...
by John Wong
Archives
May, 2006 (1)
March, 2006 (2)
February, 2006 (3)
January, 2006 (5)
November, 2005 (1)
October, 2005 (2)
September, 2005 (4)
August, 2005 (3)
July, 2005 (1)
June, 2005 (1)
December, 2004 (3)
November, 2004 (8)
October, 2004 (1)
September, 2004 (5)
August, 2004 (2)
Post Categories
XSLT
DBMS
.Net Framework
Web Services
C#
ASP.Net
Web
General
SQL Server/Database
Image Galleries
Vimage
Blogroll
Adam Bosworth
Aspects of AJAX
BradAdams
Chris Lyon
Craig Skibo
David Ing's blog
dotnetindia
Gaurav Khanna
IT Digest - TPatel
Joel on Software
Martin Fowler's bliki
Mehnaz Singh
Scobleizer
ScottGu's Blog
Sells Brothers
dotNet
.NetBookClub
XSLT
XSLT mailing list
Syndication:
RSS
ATOM
Hosted by
Copyright © Vinayak