|
-
Mar 20th, 2024, 03:03 PM
#1
Thread Starter
Frenzied Member
[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.
-
Mar 21st, 2024, 08:31 AM
#2
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
-
Mar 21st, 2024, 09:49 AM
#3
Thread Starter
Frenzied Member
Re: XML help: get subnode by value
 Originally Posted by dbasnett
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
-
Mar 21st, 2024, 10:34 AM
#4
Re: XML help: get subnode by value
 Originally Posted by nbrege
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.
-
Mar 21st, 2024, 11:46 AM
#5
Thread Starter
Frenzied Member
Re: XML help: get subnode by value
 Originally Posted by dbasnett
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...
-
Mar 21st, 2024, 12:29 PM
#6
Re: XML help: get subnode by value
 Originally Posted by nbrege
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
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
|