Solved

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

Posted on 2010-08-31
6
863 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
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

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

Refer to DataSet.Read method
0
 

Author Comment

by:wilfordrocks
Comment Utility
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
Comment Utility
Otherwise use recursion in your method to extend it's functionality
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Accepted Solution

by:
GoJoeIsBlue earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
He has responded positively on GoJoelsBlue recommendation. I think he deserves the pts.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now