[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...
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
Re: XML help: get subnode by value
Quote:
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
Re: XML help: get subnode by value
Quote:
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.
Re: XML help: get subnode by value
Quote:
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...
Re: XML help: get subnode by value
Quote:
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