0

I have a xml in the following format.

<?xml version="1.0" encoding="UTF-8" standalone= "yes"?>
<rss>
  <report name="rpt1">
    <title>AAA</title>
    <image></image>
    <weblink></weblink>
    <pdflink></pdflink>
    <pdfsize></pdfsize>
  </report>
  <report name="rpt2">
    <title>BBB</title>
    <image>CCC</image>
    <weblink>DDD</weblink>
    <pdflink>EEE</pdflink>
    <pdfsize>FFF</pdfsize>
  </report>
</rss>

Now I want to iterate this xml and get the report node and from there get childnodes like title/pdflink/size etc which would be thru. looping using for loop.

I want to use xmltextreader to accomplish this. I tried using while but I get only 1 loop after iterating. I don't know why. If thru for loop how do I iterate like,
for(loop when reader.element("reports)){} and then get the rest of the nodes and put them in an array or list or so. Once I get them stored in list I would want to display them in a feed.

What is the best way to do this? Pls, help.

Martin54
  • 1,349
  • 2
  • 13
  • 34
Geek
  • 3,187
  • 15
  • 70
  • 115
  • *Why* do you want to use `XmlTextReader`? Using LINQ to XML would be significantly simpler. Additionally, it would be useful if you could edit your post so it's easier to read - fix your capitalization, expand the pointless abbreviation etc. – Jon Skeet Aug 09 '12 at 17:56
  • I understand but my application is already using xmltext writer for someother purpose so thought it would be better to go with the same API. – Geek Aug 09 '12 at 17:59
  • If i use LINQ what should i import? – Geek Aug 09 '12 at 18:43
  • You'd use `System.Xml.Linq` - but you should really read a tutorial on LINQ to XML. I'd also suggest using it instead of `XmlTextWriter` unless you need to write really *huge* docs. – Jon Skeet Aug 09 '12 at 18:44
  • possible duplicate of [xmlreader traversing nodes](http://stackoverflow.com/questions/11871630/xmlreader-traversing-nodes) – Shyju Aug 09 '12 at 18:48
  • I already posted an answer using LINQtoXML in your other (original of this duplicate) question. http://stackoverflow.com/questions/11871630/xmlreader-traversing-nodes/11872400#11872400 – Shyju Aug 09 '12 at 18:49
  • looks like i cannot use LINQ because i use .Net frameworkv2.0.50727 and Linq is not referenced in this. I didnt want to update the framework now since it will affect other enviornments. Any other parser available for .Net other than LINQ or XMl Reader? – Geek Aug 09 '12 at 20:34

1 Answers1

0

In my case I was worried about the performance of loading a large document. What I have done is define a constructor on my objects to receive a XmlReader and hydrate - passing the reader back after it reaches a complete node.

This allows me to yield a populated object back as IEnumerable for each object as it's being read. Then I launch a new Task/Thread to handle processing that individual item and go back to processing the file.

private IEnumerable<Report> readReports(Stream reader)
{
    var settings = new XmlReaderSettings();
    settings.IgnoreWhitespace = true;
    var xmlReader = XmlReader.Create(reader, settings);
    xmlReader.MoveToContent();
    xmlReader.Read();
    while (!xmlReader.EOF)
    {
        if (xmlReader.Name.ToUpper() == "report") 
            yield return new Report(xmlReader);

        xmlReader.Read();
    }
}

public Report(XmlReader reader) : this()
{
    reader.MoveToContent();
    if (reader.IsEmptyElement)
    {
        reader.Read();
        return;
    }

    reader.Read();
    while (!reader.EOF)
    {
        if (reader.IsStartElement())
        {
            switch (reader.Name.ToLower())
            {
                case "order_id":
                    this.OrderId = reader.ReadElementContentAsString();
                    break;
                    // abreviated the rest of the fields
                default:
                    reader.Skip();
                    break;
            }
        }
        else if (reader.Name.ToLower() == "report") //this watches for the end element of the container and returns after populating all properties
            return;
    }
}

I would definitly appreciate any feedback from the rest of the community, if there is a better approach or if there are any errors here please let me know.

Matt Klinker
  • 773
  • 5
  • 18