Solved

How to read the elements of an xdocument as if it was an array?

Posted on 2010-08-31
6
867 Views
Last Modified: 2012-05-10
I want to read through any XML document and find each element with an attribute called "DataCode".
The xml documents come from various vendors and will have different schemas.  The code below works for each child of the root node.  But not for grand children or great grand children.

How can I read thru the xml document as if it was a array?

 XDocument doc = _xDocument;
            IEnumerable<XElement> childList =
            from el in doc.Root.Elements()
            select el;
            foreach (XElement e in childList)
            {
                IEnumerable<XAttribute> attList = e.Attributes("DataCode");
                foreach (XAttribute att in attList)
                {
                    Debug.WriteLine(e);
                    Debug.WriteLine("datacode found " + att.Value);
                }
            }
0
Comment
Question by:wilfordrocks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33568793
You can import the XML into a DataSet and query then as required.

Refer to DataSet.Read method
0
 

Author Comment

by:wilfordrocks
ID: 33568834
Yes, thank you.  That is a fallback idea.  I'm sure this is a common need of people that use xdocument.
I'm hoping for a more graceful answer, but thank you.
0
 
LVL 8

Assisted Solution

by:Gururaj Badam
Gururaj Badam earned 250 total points
ID: 33568873
Otherwise use recursion in your method to extend it's functionality
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 3

Accepted Solution

by:
GoJoeIsBlue earned 250 total points
ID: 33568983
Try something like this:

string papirfly = @"<Xml>
	<Child id=""1"">
		<Name DataCode=""Mammal"">Awful</Name>
		<Species>Dog</Species>
	</Child>
	<Child id=""2"">
		<Name DataCode=""Insect"">Stinger</Name>
		<Species>Bee</Species>
	</Child>
	<Child id=""3"" DataCode=""Human"">
		<Name DataCode=""Mammal"">John</Name>
		<Species>Human</Species>
	</Child>
</Xml>";
			var result7 = from x in XDocument.Parse(papirfly).Root.DescendantNodes()
						  where x is XElement && (x as XElement).Attribute("DataCode") != null
						  select new
						  {
							  Element = (x as XElement),
							  ElementName = (x as XElement).Name.LocalName,
							  DataCode = (x as XElement).Attribute("DataCode").Value,
						  };

Open in new window

0
 

Author Comment

by:wilfordrocks
ID: 33569250
Thanks GoJoelsBlue.

It says data at root node is invalid.  Would you mind taking a quick look at the attached?
Copy-of-sample.XML
0
 
LVL 8

Expert Comment

by:Gururaj Badam
ID: 33569673
He has responded positively on GoJoelsBlue recommendation. I think he deserves the pts.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question