Solved

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

Posted on 2010-08-31
6
866 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
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

860 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