-
Jun 25th, 2013, 12:41 PM
#1
Thread Starter
Addicted Member
Retriving Child elements of particular node using LINQ
Hey,
I started trying to solve this problem in another thread but it is big enough for its own
xml Code:
<?xml version="1.0" encoding="utf-8"?>
<UserEx>
<List>
<member name="INFO HERE">
<summary>SUMMARY HERE</summary>
</member>
<member name="INFO HERE123">
<summary>SUMMARY HERE 123</summary>
</member>
</List>
<List id="ers" Key="1" fier="Cont">
<member name="INFO HERE">
<summary>SUMMARY HERE</summary>
</member>
<member name="INFO HERE123">
<summary>SUMMARY HERE 123</summary>
</member>
</List>
<List id="er2ds" Key="3" fier="Conet">
<member name="INFO HERE">
<summary>SUMMARY HERE</summary>
</member>
<member name="INFO HERE123">
<summary>SUMMARY HERE 123</summary>
</member>
</List>
</UserEx>
I have an xml file similar to the one above where I need the child member elements of just the 2nd List.
I tried using this
vb.net Code:
Dim xdata = From n In doc.<UserEx>.<List>.<member>
Select New data With {
.Name = n.@name,
.Summary = n.<summary>.Value,
.Type = .Name.Substring(0, 1)
}
But because all 3 nodes in my xml file have the same name, it pulls up child nodes from the entire file. How would I limit it to get the child nodes from just the List
xml Code:
<List id="ers" Key="1" fier="Cont">
Thanks
-
Jun 25th, 2013, 01:44 PM
#2
Re: Retriving Child elements of particular node using LINQ
Code:
Dim node = xdoc...<List>.<member>(2)
Last edited by ident; Jun 25th, 2013 at 02:27 PM.
-
Jun 25th, 2013, 06:09 PM
#3
Re: Retriving Child elements of particular node using LINQ
try this:
Code:
Public Class Form1
Dim xml As XDocument = _
<?xml version="1.0" encoding="utf-8"?>
<UserEx>
<List>
<member name="INFO HERE">
<summary>SUMMARY HERE</summary>
</member>
<member name="INFO HERE123">
<summary>SUMMARY HERE 123</summary>
</member>
</List>
<List id="ers" Key="1" fier="Cont">
<member name="INFO HERE">
<summary>SUMMARY HERE</summary>
</member>
<member name="INFO HERE123">
<summary>SUMMARY HERE 123</summary>
</member>
</List>
<List id="er2ds" Key="3" fier="Conet">
<member name="INFO HERE">
<summary>SUMMARY HERE</summary>
</member>
<member name="INFO HERE123">
<summary>SUMMARY HERE 123</summary>
</member>
</List>
</UserEx>
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim nodes() As XElement = xml...<List>.Where(Function(n) n.@id = "ers" And n.@Key = "1" And n.@fier = "Cont")(0).<member>.ToArray
MsgBox(String.Join(Environment.NewLine, Array.ConvertAll(nodes, Function(n As XElement) n.@name & Environment.NewLine & n.Value & Environment.NewLine)))
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 26th, 2013, 09:23 AM
#4
Thread Starter
Addicted Member
Re: Retriving Child elements of particular node using LINQ
Thanks Guys!
I used what .paul. suggested as it gives me more control over what I pick if more nodes are added.
Just one more question though. In the above example, the first list doesnt have the names. So If I wanted to pick the first list how would I go about that?
-
Jun 26th, 2013, 09:28 AM
#5
Re: Retriving Child elements of particular node using LINQ
Originally Posted by Crzyrio
Thanks Guys!
I used what .paul. suggested as it gives me more control over what I pick if more nodes are added.
Just one more question though. In the above example, the first list doesnt have the names. So If I wanted to pick the first list how would I go about that?
Why are you using an xml file with an inconsistent schema?
This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.
The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.
-
Jun 26th, 2013, 09:39 AM
#6
Thread Starter
Addicted Member
Re: Retriving Child elements of particular node using LINQ
Originally Posted by MattP
Why are you using an xml file with an inconsistent schema?
I wish I could tell you, that was my first thought as well. The xml file is an output from another program, I have actually been talking to my team members as to notifying the 3rd party changing the file a bit. The first node has child elements from all of the nodes and the 2nd and 3rd nodes the elements are just filtered.
Thanks for the reply, I believe you had initially helped me get started last time with Linq and xml
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|