Solved

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

Posted on 2010-08-31
6
868 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
Raise the IQ of Your IT Alerts

From IT major incidents to manufacturing line slowdowns, every business process generates insights that need to reach the people required to take action. You need a platform that integrates with your business tools to create fully enabled DevOps toolchains.

You need xMatters.

 
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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

690 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