structure of xml classes in C#

A

a.kamroot

Hi

I need to exchange XML formated information between two methods in my
C# program. I have been trying to play with XMLNode and XMLElement but
without much success. I think I want to stop now and understand the
xml classes in C# (rather in dot net) before proceeding further.

Can any one point me to such a resource ? Or even a primer on XML,
which explains the definitions of nodes, elements and attributes ?

Thanks.
 
J

Jon Skeet [C# MVP]

I need to exchange XML formated information between two methods in my
C# program. I have been trying to play with XMLNode and XMLElement but
without much success. I think I want to stop now and understand the
xml classes in C# (rather in dot net) before proceeding further.

There's no special XML handling in C# - any resources on handling XML
in .NET will be appropriate.
Can any one point me to such a resource ? Or even a primer on XML,
which explains the definitions of nodes, elements and attributes ?

http://www.w3schools.com/xml/default.asp might help.
 
J

jblivingston

As Mr. Skeet suggested, w3schools will give you a good understanding
of XML itself but in .Net there are a number of helpful XML classes.
Just to give you a jumpstart on the coding side, I'll give you a few
hints (study MSDN and experiment on your own for more infomation) to
get you started. XmlDocument manages the document, gives you the
ability to create the nodes for that document, and import nodes from
other documents. XmlNode is the base class for all XML classes (even
XmlDocument). XmlElement is the class that manages the actual
element. Take for instance the following methods to convert an XML
string to a CSV string:

string convertXmlToCsv(string stringContainingXmlToLoad)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(stringContainingXmlToLoad);

return recurse(doc.DocumentElement);
}

string recurse(XmlNode node)
{
string value = string.Empty;
foreach (XmlNode child in node.ChildNodes)
{
if (value != string.Empty)
value += ",";

if (child is XmlText)
value += child.Value;
else if (child is XmlElement)
value += recurse(child);
}
return value;
}

As you can see, iterating through the children of an arbitrary node we
can extract its contents and concatinate them. Now, there are much
better ways to perform this operation but the above seemed to me to be
a simple illustration of the XML classes within the .Net framework.
Remember, study the MSDN and experiment on your own to really learn
what can be accomplished.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top