Linq Search XDocument
-
Wednesday, February 20, 2013 7:02 PM
Hi,
Here is the XML doc:
<System>
<Config>
</Config>
<Test>
<Test Name="1"></Test>
<Test Name="2""></Test>
<Test Name="3"></Test>
</Test>
</System>
Here is the code:
var tests = from test in doc.Descendants("Test") select test;
The problem is that it returns 4 items. The first one is <Test><Test Name="1"></Test></Test>, then it returns test 1 to 3, so the total is 4 and I need a way to eliminate the first one.
Thanks.
All Replies
-
Wednesday, February 20, 2013 9:54 PM
Hi Y2011;
The issue is that the parent node is called Test and all its children are also called Test. If you change the name of the parent node to lets say Tests that will solve the issue. Or you can change the names of all the child nodes and use that name in the query. If that is not possible then you can change the query as follows.var tests = from test in doc.Descendants("Test") where test.HasAttributes select test;
Fernando (MCSD)
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".- Marked As Answer by Y2011 Monday, February 25, 2013 10:08 PM
-
Thursday, February 21, 2013 1:12 PM
You'd better navigate to the corresponding level and then do Descendants or Elements
x.Element("Config").Element("Test").Elements("Test")
or
x.Element("Config").Element("Test").Descendants("Test")
Descendants function, takes name of an elements and search children in parent no matter what level it will be.
Please Mark as Reply and Vote as Helpful if I helped.
Also please visit my blog http://msguy.net/- Marked As Answer by Y2011 Monday, February 25, 2013 10:08 PM

