Solved

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

Posted on 2010-08-31
6
865 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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!
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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