Share via


how to read child nodes using xmlreader in c#

Question

Tuesday, August 18, 2015 9:48 AM

Hi All,

I have a multi root xml document.

Its like this,

<tag1>

<child1>1</child1>

<child2>2</child2>

</tag1>

<tag2>

some more child tags here

</tag2>

I need to validate <tag1> and its children.I need not validate <tag2>.

I cannot use xml.load as this doc has multiple roots.

I came to know that I can use xmlreader for this purpose.

Can anyone please help how to achieve this using xmlreader.

Thanks,

Vids

Vidya Suryanarayana

All replies (7)

Wednesday, August 19, 2015 8:04 AM âś…Answered | 1 vote

Hi Vidya,

Below code reads only the 1st tag1 (1st sub tree) and its elements, you can extend for your requirements.

- you can use switch cases to find specific elements to validate.

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.IO;

namespace ForumConsoleApplication
{
    class XMLReaderClass
    {
        static void Main(string[] args)
        {
            string str = @"<tag1>
                            <child1>1</child1>
                            <child2>2</child2>
                        </tag1>
                        <tag2>
                            <child1>1</child1>
                            <child2>2</child2>
                        </tag2>";

            XmlReader reader = XmlReader.Create(new StringReader(str));

            if (reader.Read())
            {
                XmlReader r = reader.ReadSubtree();
                while (r.Read())
                {
                    if (r.NodeType == XmlNodeType.Element || r.NodeType == XmlNodeType.Text || r.NodeType == XmlNodeType.EndElement)
                    {
                        if (r.Name != string.Empty)
                        {
                            Console.Write(r.Name);
                        }
                        if (r.Value != string.Empty)
                        {
                            Console.Write("  " + r.Value);
                        }
                    }
                    Console.WriteLine();
                }
            }
            Console.ReadKey();

        }
    }
}

Output:

         

Thanks, SMSVikasK


Tuesday, August 18, 2015 10:34 AM | 1 vote

Hi Vidya,

You can do like below:

string xmlFile = @"....\file.xml";
            XmlReader reader = XmlReader.Create(xmlFile);
            if (reader.ReadToFollowing("tag1"))
            {
              // do what you want
            }

and also may check below links:

reading-xml-with-the-xmlreader

XMlReaderClass

parse xml with XmlReader Class

If this post is helpful, please vote as helpful or marked as answer.


Wednesday, August 19, 2015 2:52 AM

Thanks . I need to know how to access the child elements under the <tag1> node.

I'm trying to use - string displayname = xr.ReadElementContentAsString("displayname", " ");

But its throwing an exception mentioning the element not found at mentioned position. The URI may be not correct. Please help.

Vidya Suryanarayana


Wednesday, August 19, 2015 3:43 AM

Can anyone please help how to achieve

http://www.dotnetcurry.com/ShowArticle.aspx?ID=564

http://csharpadvance.blogspot.com/2012/12/c-xml-document-xpath-query.html


Wednesday, August 19, 2015 4:13 AM

Thanks. The document that I'm trying to read has multiple roots. So i can't use xml.loadxml.

So i have to use xml reader and it is just not reading the child nodes.

Vidya Suryanarayana


Wednesday, August 19, 2015 10:17 AM

Thanks. The document that I'm trying to read has multiple roots. So i can't use xml.loadxml.

So i have to use xml reader and it is just not reading the child nodes.

Vidya Suryanarayana

So? I would chose either Linq-2-XML or X-path over what you are doing any day of the week period. Like with anything else, one has to know the possibilities and what technology can do. And if you don't know then you don't know. 

I have seen XML data driven Websites where  data drives the pages that are dynamic with many XML roots  and data coming up from the database that drives the application and uses the technology mentioned to make the decision on what controls to put on the page and the associated XML data for the controls on the page.

And then the XML is sent back to the backend application where the XML data only based on multiple tables is queried to send the data to the appropriate database tables using said technology. Many roots are in the XML data that represents the data used by the database tables.

http://blogs.perl.org/users/ovid/2014/04/the-hidden-benefit-of-data-driven-programming.html


Monday, August 24, 2015 4:02 AM

Thank you. This worked!

Vidya Suryanarayana