New to xml and how to read an xml doc advise needed


Hi,

Strange enough i have never played with xml very much.I have some questions and hopefully you guys will be able to answer them.

1)Need to read/Loop through an XML file whilst looping there might be both elements and attributes .Is there a generic way of looping through both the elements and attributes.I looked in the MSDN and I could not find an example.

2)Reading/Writing xml can be done in many ways. when shall i use the textReader when the doc class.

ANy good links with examples for beginners in xml

thanks a lot



Answer this question

New to xml and how to read an xml doc advise needed

  • Nate_CeeCual_n_D0TN3t

    1) Here you can find a sample code for looping through XML nodes: http://msdn2.microsoft.com/en-us/library/system.xml.xmlreader.read(VS.80).aspx

    2) If you are going to post-process the XML document, XmlDocument is more appropriate. If you need just to convert the XML document into a string representation, then XmlTextWriter is more efficient choice, because it won't cache the whole document in the memory.

    MSDN has a number of good articles devoted to using XML in .NET, for example:

    http://msdn.microsoft.com/msdnmag/issues/01/01/xml/

    Also there is a nice book "Applied XML Programming for Microsoft .NET".

    Hope it helps,
    Anton


  • Venkat Manohar

    Thanks alot for your very good replies.

    It helped me a lot


  • Suren...

    The same could be done with a XPathNavigator, you may use XPath navigator to navigate though the result of an XPath query... Here is a sample.

    static void TraverseElementsAndAttributes()

    {

    XPathDocument xdoc = new XPathDocument("xs\\travers.xml");

    XPathNavigator xnav = xdoc.CreateNavigator();

    TraverseElement(xnav);

    }

    static void TraverseElement(XPathNavigator xnav)

    {

    //print attributes

    if (xnav.MoveToFirstAttribute())

    {

    Console.WriteLine("Attribute: " + xnav.Name + ", Value: " + xnav.Value);

    while (xnav.MoveToNextAttribute())

    {

    Console.WriteLine("Attribute: " + xnav.Name + ", Value: " + xnav.Value);

    }

    xnav.MoveToParent();

    }

    //print element content

    if (xnav.NodeType == XPathNodeType.Text)

    Console.WriteLine("Text: " + xnav.Value.Trim());

    else if (xnav.NodeType == XPathNodeType.Element)

    Console.WriteLine("Element: " + xnav.Name + ", InnerXml: " + xnav.InnerXml);

    //go to first child

    if (xnav.MoveToFirstChild())

    {

    Console.WriteLine("...Moved to first child");

    TraverseElement(xnav);

    }

    //go to next child (as we didn't go parent, we are going to next sibling)

    while (xnav.MoveToNext(XPathNodeType.Element))

    {

    Console.WriteLine("...Moved to next sibling");

    TraverseElement(xnav);

    }

    //go back to the node itself to reset the state of the navigator to this node.

    xnav.MoveToParent();

    Console.WriteLine("...Moved to patent");

    return;

    }

    INPUT:

    < xml version="1.0" encoding="utf-8" >

    <root>

    <a attra="attra" />

    <b attrb1="attrb1" attrb2="attrb2">

    bvalue

    <c attrc="attrc">

    cvalue

    <d>dvalue</d>

    </c>

    <e attre="attre" />

    <f attrc="attrc">

    fvalue

    <g>

    <h>hvalue</h>gvalue</g>

    <i>ivalue</i>

    </f>

    </b>

    </root>

    output:

    ...Moved to first child
    Element: root, InnerXml: <a attra="attra" />
    <b attrb1="attrb1" attrb2="attrb2">
    bvalue
    <c attrc="attrc">
    cvalue
    <d>dvalue</d></c><e attre="attre" /><f attrc="attrc">
    fvalue
    <g><h>hvalue</h>gvalue</g><i>ivalue</i></f></b>
    ...Moved to first child
    Attribute: attra, Value: attra
    Element: a, InnerXml:
    ...Moved to next sibling
    Attribute: attrb1, Value: attrb1
    Attribute: attrb2, Value: attrb2
    Element: b, InnerXml:
    bvalue
    <c attrc="attrc">
    cvalue
    <d>dvalue</d></c><e attre="attre" /><f attrc="attrc">
    fvalue
    <g><h>hvalue</h>gvalue</g><i>ivalue</i></f>
    ...Moved to first child
    Text: bvalue
    ...Moved to next sibling
    Attribute: attrc, Value: attrc
    Element: c, InnerXml:
    cvalue
    <d>dvalue</d>
    ...Moved to first child
    Text: cvalue
    ...Moved to next sibling
    Element: d, InnerXml: dvalue
    ...Moved to first child
    Text: dvalue
    ...Moved to patent
    ...Moved to patent
    ...Moved to next sibling
    Attribute: attre, Value: attre
    Element: e, InnerXml:
    ...Moved to next sibling
    Attribute: attrc, Value: attrc
    Element: f, InnerXml:
    fvalue
    <g><h>hvalue</h>gvalue</g><i>ivalue</i>
    ...Moved to first child
    Text: fvalue
    ...Moved to next sibling
    Element: g, InnerXml: <h>hvalue</h>gvalue
    ...Moved to first child
    Element: h, InnerXml: hvalue
    ...Moved to first child
    Text: hvalue
    ...Moved to patent
    ...Moved to patent
    ...Moved to next sibling
    Element: i, InnerXml: ivalue
    ...Moved to first child
    Text: ivalue
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent
    ...Moved to patent



  • Saaketh

    Use XPathNavigator over XPathDocument (read-only XML optimized for XPath/XSLT) or XmlDocument (read-write XML compatible with DOM) whenever possible. Read directly from XmlReader if speed or memory consumption is an issue for you. In the latter case you may avoid caching the whole XML document in memory.
  • suyang

    Fantastic replies.

    Now I just have to play with it and digest it all.Thanks

    Silly question:

    I dont want to reinvent the wheel but is there a reason why not use the xPathNavigator At glance it looks like it does the job for you.Am I wrong

    thanks again for your replies


  • New to xml and how to read an xml doc advise needed