XMLDOCUMENT: Any good online references to understand and use?

D

David

I need to find a good online resource which teaches the use of the
XmlDocument framework in more depth than is covered in MS's online
doc. I need to create a multi-level XML document like the one listed
below. Using XmlDocument, I can create the <books> and the individual
<name> and <price> elements, but I am unsure how to create the
<category> and <title> elements and close them after creating the
detail elements. Eg. how can I go 2+ levels deep?

Also, is it adventatgious to use the XmlDocument or the XmlWriter
class to create more complex XML?

Thanks





<?xml version="1.0" encoding="utf-8" ?>
<books>
<category name="dotnet">
<title>
<name>ASP.NET in 15 seconds</name>
<price>$34.99</price>
</title>
<title>
<name>C# in 15 seconds</name>
<price>$44.99</price>
</title>
</category>
<category name="othernet">
<title>
<name>WHY AM I HERE</name>
<price>$34.99</price>
</title>
</category>
</books>
 
D

Derek Harmon

David said:
Using XmlDocument, I can create the <books> and the individual
<name> and <price> elements, but I am unsure how to create the
<category> and <title> elements and close them after creating the
detail elements. Eg. how can I go 2+ levels deep?

So you've created the <name> and <price> elements using
CreateElement( ), right?

OK, it sounds like what you're missing is that XmlNodes
can contain other XmlNodes.

So, go ahead and create <category> and <title> elements
exactly like you created <name> and <price> (but don't
put any XmlText nodes into them).

Use the AppendChild( ) methods on these <category>
elements to 'add' the <title> element as a child. Then
use AppendChild( ) again to add <name> and <price>
elements underneath <title>. Here's an example of how
these can be built from the bottom-up,

XmlElement nameElem = xmlDocument.CreateElement( "name");
XmlText nameText = xmlDocument.CreateTextNode( "C# in 15 seconds");
nameElem.AppendChild( nameText);

XmlElement priceElem = xmlDocument.CreateElement( "price");
XmlText priceText = xmlDocument.CreateTextNode( "44.99");
priceElem.AppendChild( priceText);

XmlElement titleElem = xmlDocument.CreateElement( "title");
titleElem.AppendChild( nameElem);
titleElem.AppendChild( priceElem);

XmlElement categoryElem = xmlDocument.CreateElement( "category");
categoryElem.AppendChild( titleElem);

You don't have to worry about "closing" the tags, when
you serialize the XmlDocument out it will take care of
that for you. It'll say, here's a <title> element.. it'll write
the start tag for <title>, then it'll see <title> contains two
child elements so it'll write them. When it's through with
all of the <title>'s children, it'll write the closing tag for
Also, is it adventatgious to use the XmlDocument or the XmlWriter
class to create more complex XML?

For large documents, XmlWriter is faster, but also more complex.
XmlDocument is easy to work with, but has more overhead. For
example, in XmlWriter you have to take care of the closing tags,
whereas I've shown how XmlDocument takes care of this for you
automatically.


Derek Harmon
 

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

Top