473,385 Members | 1,536 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XElement questions

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?
Jun 27 '08 #1
4 2337
CSharper wrote:
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()
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
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

CI have an XML and I am using XDocument to traverse the xml document.
CA
Csample 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>
CI would like to find out all the type attribute values for a given
Cname attribute. How can I do it with a simple linq? I am able to do
Cit with XmlDocument by writing recursion but there must be a easier
Cway. I tried the following but it is not giving any result
C>
Cvar result = from c in doc.Elements()
Cwhere ((c.Attribute("name") != null) &&
C(c.Attribute("type") != null) &&
C(c.Attribute("name").Value == 'n'))
Cselect (string)c.Attribute("type").value;
CWhen I debug, I can see only the root element get processed and none
Cof the other nodes get processed. The same when I applied Elements.
C>
CCould someone help?
C>
Jun 27 '08 #3
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.
"CSharper" wrote:
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?
Jun 27 '08 #4
On May 13, 8:33*pm, Family Tree Mike
<FamilyTreeM...@discussions.microsoft.comwrote:
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.

"CSharper" wrote:
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?- Hide quoted text -

- Show quoted text -
Excellent thanks Guys.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
2
by: Mike N. | last post by:
hello all: I am using LINQ to XML, but I think this issue is more widespread, here's an example of what I am trying to do: Dim testurl As String = "http://stats.enemyterritory.com/profile/...
0
by: Andy B | last post by:
I have a CheckBoxList that I want to dataBind to an XElement. I got this far: CheckBoxLisgt.DataSource = StockContract.Element("Glossary").Elements("Term"); CheckBoxList.DataBind(); It works...
4
by: Rory Becker | last post by:
Is there a way to do something like the following... ------------------------------------------------------------- Dim x As String =...
11
by: Michael Bray | last post by:
I'm playing around with XElement stuff, and I've come across a difficulty. The XML document that I'm reading contains an xmlns declaration on the main node... <root xmlns="http://www.me.com">...
0
by: John Meyer | last post by:
I'm going through the documentation help about using XElement and I'm finding no help with going through the elements as a recordset. here's an example of the return results I am getting through...
4
by: standerby | last post by:
I am writing a XML file suing XElement. How can I output a file like the following? I got an exception if if I use ':' in the element name or attribute name. Thanks, <?xml version="1.0"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.