Results 1 to 6 of 6

Thread: [RESOLVED] XML help: get subnode by value

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Resolved [RESOLVED] XML help: get subnode by value

    Suppose I have the following XML code:

    Code:
    Dim assembliesElement As XElement = <Job>
                                          <Assemblies>
                                           <Assembly>
                                             <Properties>
                                              <General>
                                                <Name>TS_Upper_1S</Name>
                                                <Type>Standard</Type>
                                              </General>
                                            </Properties>
                                          </Assembly>
                                          <Assembly>
                                            <Properties>
                                              <General>
                                               <Name>TS_Lower_2S</Name>
                                                <Type>Standard</Type>
                                              </General>
                                            </Properties>
                                          </Assembly>
                                          <Assembly>
                                            <Properties>
                                              <General>
                                                <Name>TS_Upper_1D</Name>
                                                <Type>Standard</Type>
                                              </General>
                                           </Properties>
                                          </Assembly>
                                          <Assembly>
                                           <Properties>
                                              <General>
                                                <Name>TS_Upper_3F</Name>
                                                <Type>Standard</Type>
                                              </General>
                                            </Properties>
                                          </Assembly>
                                        </Assemblies>
                                       </Job>
    I want to get the Assembly subnode by specifying the Name value. So for example, I want to specify "TS_Upper_1D" and get this subnode:

    Code:
                                          <Assembly>
                                            <Properties>
                                              <General>
                                                <Name>TS_Upper_1D</Name>  <-- retrieve by this value
                                                <Type>Standard</Type>
                                              </General>
                                           </Properties>
                                          </Assembly>
    There could be several dozen <Assembly> nodes under the <Assemblies> node. How would I do this? Thanks...
    Last edited by nbrege; Mar 20th, 2024 at 03:08 PM.

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: XML help: get subnode by value

    The assumptions

    Code:
            Dim path As String = ""
            Dim assembliesElement As XElement
            ' assembliesElement = XElement.Load(path)
            assembliesElement = <Job>
                                    <Assemblies>
                                        <Assembly>
                                            <Properties>
                                                <General>
                                                    <Name>TS_Upper_1S</Name>
                                                    <Type>Standard</Type>
                                                </General>
                                            </Properties>
                                        </Assembly>
                                        <Assembly>
                                            <Properties>
                                                <General>
                                                    <Name>TS_Lower_2S</Name>
                                                    <Type>Standard</Type>
                                                </General>
                                            </Properties>
                                        </Assembly>
                                        <Assembly>
                                            <Properties>
                                                <General>
                                                    <Name>TS_Upper_1D</Name>
                                                    <Type>Standard</Type>
                                                </General>
                                            </Properties>
                                        </Assembly>
                                        <Assembly>
                                            <Properties>
                                                <General>
                                                    <Name>TS_Upper_3F</Name>
                                                    <Type>Standard</Type>
                                                </General>
                                            </Properties>
                                        </Assembly>
                                    </Assemblies>
                                </Job>
    The code to extract by name.
    Code:
            Dim assem As XElement
    
            assem = (From el In assembliesElement.<Assemblies>.<Assembly>
                      Where el.<Properties>.<General>.<Name>.Value = "TS_Upper_1D"
                      Select el Take 1).FirstOrDefault
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: XML help: get subnode by value

    Quote Originally Posted by dbasnett View Post

    The code to extract by name.
    Code:
            Dim assem As XElement
    
            assem = (From el In assembliesElement.<Assemblies>.<Assembly>
                      Where el.<Properties>.<General>.<Name>.Value = "TS_Upper_1D"
                      Select el Take 1).FirstOrDefault

    Thank you very much for the help. I got your solution to work, but I had to make a small change. I had to take out the <Assemblies> path as follows:

    Code:
            assem = (From el In assembliesElement.<Assembly>
                      Where el.<Properties>.<General>.<Name>.Value = "TS_Upper_1D"
                      Select el Take 1).FirstOrDefault

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: XML help: get subnode by value

    Quote Originally Posted by nbrege View Post
    Thank you very much for the help. I got your solution to work, but I had to make a small change. I had to take out the <Assemblies> path as follows:

    Code:
            assem = (From el In assembliesElement.<Assembly>
                      Where el.<Properties>.<General>.<Name>.Value = "TS_Upper_1D"
                      Select el Take 1).FirstOrDefault

    Then the XML you originally showed was different. Glad it worked.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: XML help: get subnode by value

    Quote Originally Posted by dbasnett View Post
    Then the XML you originally showed was different. Glad it worked.
    Indeed ... my bad. I don't have the top level <Job> node anymore. Thanks again for your help with this...

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: XML help: get subnode by value

    Quote Originally Posted by nbrege View Post
    Indeed ... my bad. I don't have the top level <Job> node anymore. Thanks again for your help with this...
    Depending on the structure of the XML and uniqueness of the names you might get away with this,

    Code:
            'the given
            Dim path As String = ""
            Dim assembliesElement As XElement
            ' assembliesElement = XElement.Load(path)
            assembliesElement = <Assemblies>
                                    <Assembly>
                                        <Properties>
                                            <General>
                                                <Name>TS_Upper_1S</Name>
                                                <Type>Standard</Type>
                                            </General>
                                        </Properties>
                                    </Assembly>
                                    <Assembly>
                                        <Properties>
                                            <General>
                                                <Name>TS_Lower_2S</Name>
                                                <Type>Standard</Type>
                                            </General>
                                        </Properties>
                                    </Assembly>
                                    <Assembly>
                                        <Properties>
                                            <General>
                                                <Name>TS_Upper_1D</Name>
                                                <Type>Standard</Type>
                                            </General>
                                        </Properties>
                                    </Assembly>
                                    <Assembly>
                                        <Properties>
                                            <General>
                                                <Name>TS_Upper_3F</Name>
                                                <Type>Standard</Type>
                                            </General>
                                        </Properties>
                                    </Assembly>
                                </Assemblies>
    
            'get one assembly
            Dim assem As XElement
    
            assem = (From el In assembliesElement...<Name>
                      Where el.Value = "TS_Upper_1D"
                      Select el.Parent.Parent.Parent Take 1).FirstOrDefault
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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