EDN Admin
Well-known member
linq to xml select parent node of select distinct node<br/>
<br/>
I am fairly new to Linq to xml and I have spent some time looking at various posts and have learned a lot. I am using vb.net<br/>
<br/>
What I need to know is how to get a collection of parent nodes where the child nodes meet certain criteria.<br/>
<br/>
Specifically, I want to get a collection of all level2 nodes which have the ele node that have the Name attribute=ObjID with a value of the node that is unique.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Dim d <span style="color:Blue; As <span style="color:Blue; String = <span style="color:#A31515; "<root>" +
<span style="color:#A31515; "<level1>" +
<span style="color:#A31515; "<level2>" +
<span style="color:#A31515; "<ele Name=""ObjID" 1</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" xx</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "</level2>" +
<span style="color:#A31515; "<level2>" +
<span style="color:#A31515; "<ele Name=""ObjID" 1</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "</level2>" +
<span style="color:#A31515; "<level2>" +
<span style="color:#A31515; "<ele Name=""ObjID" 2</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "</level2>" +
<span style="color:#A31515; "</level1>" +
<span style="color:#A31515; "</root>"
<span style="color:Blue; Dim tr <span style="color:Blue; As TextReader = <span style="color:Blue; New StringReader(d)
<span style="color:Blue; Dim doc <span style="color:Blue; As XDocument = XDocument.Load(tr)
<span style="color:Blue; Dim wantLevel2Elements = (<span style="color:Blue; From prop <span style="color:Blue; In doc.Descendants(<span style="color:#A31515; "ele")
<span style="color:Blue; Where prop.@Name = <span style="color:#A31515; "ObjID"
<span style="color:Blue; Select <span style="color:Blue; New MyDataElement() <span style="color:Blue; With
{
.ObjectTypeID = prop.Value,
.Value = <span style="color:Blue; Integer.Parse(prop.Value)
}).<span style="color:Blue; Distinct(<span style="color:Blue; New DataExtractorElementComparer())
[/code]
<br/>
<br/>
In the above example, I am getting two distinct ele nodes that meet my criteria. What I want is the level2 parent nodes for each of the ele nodes.<br/>
<br/>
Any help will be greatly appreciated.<br/>
<br/>
<br/>
Thank you<br/>
<br/>
Ada L
<hr class="sig Ada L
View the full article
<br/>
I am fairly new to Linq to xml and I have spent some time looking at various posts and have learned a lot. I am using vb.net<br/>
<br/>
What I need to know is how to get a collection of parent nodes where the child nodes meet certain criteria.<br/>
<br/>
Specifically, I want to get a collection of all level2 nodes which have the ele node that have the Name attribute=ObjID with a value of the node that is unique.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Dim d <span style="color:Blue; As <span style="color:Blue; String = <span style="color:#A31515; "<root>" +
<span style="color:#A31515; "<level1>" +
<span style="color:#A31515; "<level2>" +
<span style="color:#A31515; "<ele Name=""ObjID" 1</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" xx</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "</level2>" +
<span style="color:#A31515; "<level2>" +
<span style="color:#A31515; "<ele Name=""ObjID" 1</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "</level2>" +
<span style="color:#A31515; "<level2>" +
<span style="color:#A31515; "<ele Name=""ObjID" 2</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "<ele Name=""NotNeeded" x</ele>" +
<span style="color:#A31515; "</level2>" +
<span style="color:#A31515; "</level1>" +
<span style="color:#A31515; "</root>"
<span style="color:Blue; Dim tr <span style="color:Blue; As TextReader = <span style="color:Blue; New StringReader(d)
<span style="color:Blue; Dim doc <span style="color:Blue; As XDocument = XDocument.Load(tr)
<span style="color:Blue; Dim wantLevel2Elements = (<span style="color:Blue; From prop <span style="color:Blue; In doc.Descendants(<span style="color:#A31515; "ele")
<span style="color:Blue; Where prop.@Name = <span style="color:#A31515; "ObjID"
<span style="color:Blue; Select <span style="color:Blue; New MyDataElement() <span style="color:Blue; With
{
.ObjectTypeID = prop.Value,
.Value = <span style="color:Blue; Integer.Parse(prop.Value)
}).<span style="color:Blue; Distinct(<span style="color:Blue; New DataExtractorElementComparer())
[/code]
<br/>
<br/>
In the above example, I am getting two distinct ele nodes that meet my criteria. What I want is the level2 parent nodes for each of the ele nodes.<br/>
<br/>
Any help will be greatly appreciated.<br/>
<br/>
<br/>
Thank you<br/>
<br/>
Ada L
<hr class="sig Ada L
View the full article