473,320 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

XMLDOCUMENT: Any good online references to understand and use?

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>

Nov 16 '05 #1
1 1589
"David" <ma******@sbcglobal.net> wrote in message news:vg********************************@4ax.com...
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
</title>.
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
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: todd | last post by:
Simply trying to load xml into a DOM without the dom converting my escape sequence. **code snippet** XmlDocument xmlDoc = new XmlDocument() ; xmlDoc.LoadXml("<x>hello world</x>"); ...
13
by: Matthew Wieder | last post by:
In my C# application, I have class which has method that opens an XML document, modifies it and saves it out. I run that method for several different XML documents. What I've found is that the...
1
by: AJ Brown | last post by:
I'm loading an XmlDocument object from a string using LoadXml. The string is a well-formed XML fragment (see example) and loads fine, however there are entities (&amp;) within attribute values. The...
5
by: Bob P | last post by:
I have an example below that is not working correctly. I have a web application that passes a System.Xml.XmlDocument object "oInputDoc" around to functions byval. I do not want the "oInputDoc"...
7
by: emma_middlebrook | last post by:
Hi In one of our test suites we load an xml document (it's actually .svg). I'm trying to find some way of stopping the eventual call to HttpWebRequest.GetResponse which I presume it's doing when...
2
by: ian | last post by:
Hi, I've got the weirdest garbage collection problem - I'd appreciate any advice you've got. 1. A class 'X' in a system I'm working on contains a reference to an XmlDocument, populated via...
13
by: docschnipp | last post by:
Hi, I have a strange error that occurs on loading an XML document: The code is: // create a document XmlDocument myDoc = new XmlDocument(); // and load it from file myDoc.Load(filename);
5
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I've been trying to improve the performance of our xml handling, so I've been doing some timing tests lately and ran across something I thought odd - XPathDocument seems slightly slower...
4
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... We've got a lot of places in our code where we read relatively small xml user preference blocks. Currently that's creating a new XmlDocument in every spot. I was thinking we might see...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.