2

I have a stream of xml being returned from a webservice which is coming in via HttpWebResponse. Currently I am loading the xml into an XmlTextReader, and then looping through the nodes to grab variables. This works, but is extremely unwieldy, as certain child nodes contain data that affect how I organize the data that comes before it. The XmlTextReader as far as I know, is iterative and forward-reading only. So if child node x contains data that potentially would affect parent node a, I've already read through parent node a by the time I get to child node x, and the whole thing is a massive headache.

I am relatively new to working with xml in .net... is there a better way to approach this? I guess thinking out loud, I would love to be able to read the inbound xml stream into an object where I can say: Show me everything in all node x's : then set them to a class. Based on what they are, okay should me everything in node a's.... etc, etc. In other words, not a forward reading method, but more of an top down look approach to the entire xml content. Is this possible?

Here is some sample code as to how I'm handling it now:

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
XmlTextReader reader = new XmlTextReader(responseStream);

while (reader.Read())
{
    if (reader.Name == "Whatever")
        {
            var = reader.ReadString();
            reader.Read();

... etc etc until the end of the xml.

Okay, I think I'm almost there, I just need a little more help. Below is my new code, which I am using Linq to XML for, and its working fine. I just have a question about nested data. The final element I am getting out of the xml, "ProductAlignment" actually has child nodes that I need to parse out in a readable format. So right now, with my code the way it is, it takes all of the child elements in ProductAlignment, and jumbles them together in one cell when I databind the whole thing to a gridview. How can I subquery the child elements in ProductAlignment to a more readable format?

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
XDocument xdoc = XDocument.Load(XmlReader.Create(responseStream));
var People= from Person in xdoc.Descendants("Person")
                          where Person.Element("Role").Value != "Admin"
                          orderby Person.Element("Role").Value
                          select new
                          {
                              Role= Person.Element("TerritoryRole").Value,
                              FirstName = Person.Element("FirstName").Value,
                              LastName = Person.Element("LastName").Value,
                              ProductAlignment = Person.Element("ProductAlignment").Value
                          };

        gvTest.DataSource = People;
        gvTest.DataBind();
optionsix
  • 956
  • 1
  • 8
  • 27

2 Answers2

4

you can use linq to xml with following...

  1. load the document using XDocument.Load method
  2. Then you can use XDocument methods to get to your nodes, as specified here
  3. or you could also use XPathSelectElement method to do the old xpath queries on the document if you want.

this approach would let you traverse in any direction at the cost of whole document being in memory...

Community
  • 1
  • 1
np-hard
  • 5,725
  • 6
  • 52
  • 76
  • Will #1 work with a stream? The xml doesn't actually exist as a "document", its just data coming through a response object. Do I have to put it IN a local xml doc, then load it? Or is there a way to feed the stream directly into an XDocument object? – optionsix Nov 18 '11 at 16:08
  • @optionsix: `XDocument.Load` takes a stream. – SLaks Nov 18 '11 at 16:14
  • What is a computer document anyway, but a finite stream in a given format, that someone has frozen in some way (memory, file) and called a document? Once the streams loaded into an XDocument, it's a document :) – Jon Hanna Nov 18 '11 at 17:10
  • Fair enough :) I'll give it a whirl. – optionsix Nov 18 '11 at 17:22
  • is the xdocument.load method for a stream 4.0 only? I'm using 3.5 and it no workie. I think I may have found a workaround though...http://stackoverflow.com/questions/7666835/xdocument-load-error – optionsix Nov 18 '11 at 18:03
  • yeah it looks like it only for 4.0 but in 3.5 seems to have a Load method with Xmlreader, which you can create with a stream. – np-hard Nov 18 '11 at 19:11
1

You're looking for LINQ to XML.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964