views:

71

answers:

1

I decided to try out the tutorial on this website

http://www.csharphelp.com/2006/05/creating-a-xml-document-with-c/

Here's my code, which is more or less the same but a bit easier to read

using System;
using System.Xml;

public class Mainclass
{
    public static void Main()
    {
        XmlDocument XmlDoc = new XmlDocument();

        XmlDocument xmldoc;
        XmlNode node1;
        node1 = XmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
        XmlDoc.AppendChild(node1);

        XmlElement element1;
        element1 = XmlDoc.CreateElement("", "ROOT", "");

        XmlText text1;
        text1 = XmlDoc.CreateTextNode("this is the text of the root element");

        element1.AppendChild(text1);
        // appends the text specified above to the element1

        XmlDoc.AppendChild(element1);


        // another element

        XmlElement element2;
        element2 = XmlDoc.CreateElement("", "AnotherElement", "");

        XmlText text2;
        text2 = XmlDoc.CreateTextNode("This is the text of this element");
        element2.AppendChild(text2);

        XmlDoc.ChildNodes.Item(1).AppendChild(element2);
    }
}

So far, I'm liking XmlDocument, but I can't figure out how this line works

XmlDoc.ChildNodes.Item(1).AppendChild(element2);

Specifically, the Item() part of it

according to MSDN...

//
// Summary:
//     Retrieves a node at the given index.
//
// Parameters:
//   index:
//     Zero-based index into the list of nodes.
//
// Returns:
//     The System.Xml.XmlNode in the collection. If index is greater than or equal
//     to the number of nodes in the list, this returns null.

However, I'm still not really sure what "index" refers to, or what Item() does. Does it move down the tree or down a branch?

Also, when I was looking at it, I thought it would end up like this

what I thought would happen:

<?xml version="1.0"?>
<ROOT>this is the text of the root element</ROOT>
<AnotherElement>This is the text of this element</AnotherElement>

but it ended up like this

Actual output

<?xml version="1.0"?>
<ROOT>this is the text of the root element
      <AnotherElement>This is the text of this element</AnotherElement>
</ROOT>

(formatting added)

+2  A: 

The ChildNodes property returns the XmlNodeList of immediate children of what you call it on. Item then finds the nth member of that list. It won't recurse into grand-children etc. In particular, I believe in this case Item(0) would return the XML declaration, and Item(1) returns the root element. A nicer way of expressing "get to the root element" would be to use XmlDocument.DocumentElement.

Note that your "expected" output wouldn't even be valid XML - an XML document can only have one root element.

To be honest, this isn't a terribly nice use of it - and in particular I would recommend using LINQ to XML rather than XmlDocument if you possibly can. It's not particularly clear what you're trying to achieve with the code you've given, but it would almost certainly be much simpler in LINQ to XML.

Jon Skeet
As for use of this code, all I'm doing is practicing XML
jwaffe
@jwaffe: I suggest you practice LINQ to XML instead of `XmlDocument` :)
Jon Skeet
Do you know where I could get a good explanation of the new keywords in .net 3.0? The MSDN site has a mistake in it, and I'm having a hard time finding definitions
jwaffe
@jwaffe: Keywords are typically in *languages*, and .NET 3.0 didn't introduce any new language features. What kind of thing are you talking about?
Jon Skeet
"from', for instance the last part of the example on this sitehttp://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspxI don't know what "from", "in", "where", and "select" mean.
jwaffe
@jwaffe: That's C# 3 (part of .NET 3.5) not .NET 3.0. Where is the mistake you think you've found? As for good explanations - might I recommend my book, C# in Depth? :) (http://manning.com/skeet2)
Jon Skeet