XElement questions

C

CSharper

I have an XML and I am using XDocument to traverse the xml document. A
sample document look like the following
<root>
<node1>
<element1 name="n" type="1" />
</node1>
<node2 name='n1' type='2'>
<element2>test</element2>
</node2>
</root>

I would like to find out all the type attribute values for a given
name attribute. How can I do it with a simple linq? I am able to do it
with XmlDocument by writing recursion but there must be a easier way.
I tried the following but it is not giving any result


var result = from c in doc.Elements()
where ((c.Attribute("name") != null) &&
(c.Attribute("type") != null) &&
(c.Attribute("name").Value == 'n'))
select (string)c.Attribute("type").value;

When I debug, I can see only the root element get processed and none
of the other nodes get processed. The same when I applied Elements.

Could someone help?
 
M

Martin Honnen

CSharper said:
var result = from c in doc.Elements()
where ((c.Attribute("name") != null) &&
(c.Attribute("type") != null) &&
(c.Attribute("name").Value == 'n'))
select (string)c.Attribute("type").value;

When I debug, I can see only the root element get processed and none
of the other nodes get processed. The same when I applied Elements.

Use
doc.Descendants()
instead of
doc.Elements()
 
A

Alex Meleta

Hi CSharper,

Use doc.Descendants() instead of doc.Elements() in your example to get it
work. And, by the way, string convertion in select is redundant coz Value
already is string.

Regards, Alex
[TechBlog] http://devkids.blogspot.com

C> I have an XML and I am using XDocument to traverse the xml document.
C> A
C> sample document look like the following
C> <root>
C> <node1>
C> <element1 name="n" type="1" />
C> </node1>
C> <node2 name='n1' type='2'>
C> <element2>test</element2>
C> </node2>
C> </root>
C> I would like to find out all the type attribute values for a given
C> name attribute. How can I do it with a simple linq? I am able to do
C> it with XmlDocument by writing recursion but there must be a easier
C> way. I tried the following but it is not giving any result
C>
C> var result = from c in doc.Elements()
C> where ((c.Attribute("name") != null) &&
C> (c.Attribute("type") != null) &&
C> (c.Attribute("name").Value == 'n'))
C> select (string)c.Attribute("type").value;
C> When I debug, I can see only the root element get processed and none
C> of the other nodes get processed. The same when I applied Elements.
C>
C> Could someone help?
C>
 
F

Family Tree Mike

The others have answered, but there is another option. You could change
doc.elements() to doc.DocumentElement.Elements(). It's just a style I like
that clarifies there is a document element to be skipped.
 
C

CSharper

The others have answered, but there is another option.  You could change
doc.elements() to doc.DocumentElement.Elements().  It's just a style I like
that clarifies there is a document element to be skipped.









- Show quoted text -

Excellent thanks Guys.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top