Note that there are some explanatory texts on larger screens.

plurals
  1. POLooking for a better approach to organizing a stream of xml using c#
    primarykey
    data
    text
    <p>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.</p> <p>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? </p> <p>Here is some sample code as to how I'm handling it now:</p> <pre><code>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(); </code></pre> <p>... etc etc until the end of the xml.</p> <p>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?</p> <pre><code>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(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload