Results 1 to 6 of 6

Thread: Retriving Child elements of particular node using LINQ

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    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:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <UserEx>
    3. <List>
    4.     <member name="INFO HERE">
    5.       <summary>SUMMARY HERE</summary>
    6.     </member>
    7.     <member name="INFO HERE123">
    8.       <summary>SUMMARY HERE 123</summary>
    9.     </member>
    10. </List>
    11.  
    12. <List id="ers" Key="1" fier="Cont">
    13.     <member name="INFO HERE">
    14.       <summary>SUMMARY HERE</summary>
    15.     </member>
    16.     <member name="INFO HERE123">
    17.       <summary>SUMMARY HERE 123</summary>
    18.     </member>
    19. </List>
    20.  
    21. <List id="er2ds" Key="3" fier="Conet">
    22.     <member name="INFO HERE">
    23.       <summary>SUMMARY HERE</summary>
    24.     </member>
    25.     <member name="INFO HERE123">
    26.       <summary>SUMMARY HERE 123</summary>
    27.     </member>
    28. </List>
    29. </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:
    1. Dim xdata = From n In doc.<UserEx>.<List>.<member>
    2.                 Select New data With {
    3.                   .Name = n.@name,
    4.                   .Summary = n.<summary>.Value,
    5.                   .Type = .Name.Substring(0, 1)
    6.                  }

    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:
    1. <List id="ers" Key="1" fier="Cont">

    Thanks

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Retriving Child elements of particular node using LINQ

    Code:
    Dim node = xdoc...<List>.<member>(2)

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    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?

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: Retriving Child elements of particular node using LINQ

    Quote Originally Posted by Crzyrio View Post
    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: Retriving Child elements of particular node using LINQ

    Quote Originally Posted by MattP View Post
    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
  •  



Click Here to Expand Forum to Full Width