Trying to write a select that will let me select a specific office element
Here is my XML
<?xml version="1.0">
<regions>
<region>
<office>
<name>Office One</name>
</office>
<office>
<name>Office Two</name>
</office>
<settings>
<name>Main Regional Name</name>
</settings>
</region>
<region>
<office>
<name>Office Three</name>
</office>
<office>
<name>Office Four</name>
</office>
<settings>
<name>Secondary Regional Name</name>
</settings>
</region>
Here is my code
Dim clfWizardXml As XElement
Dim selectRegion = lstRegions.SelectedItem
Dim selectOffice = lstOffices.SelectedItem
Console.WriteLine(selectRegion + " " + selectOffice)
Dim officeList As IEnumerable(Of XElement) = _
From region In clfWizardXml.Elements("region"), _
office In clfWizardXml.Elements("region").Elements("office") _
Where region.Element("settings").Element("name").Value = selectRegion _
And office.Element("name").Value = selectOffice
Select office
I think my issue is somewhere around here: And office.Element("name").Value = selectOffice
Thanks for your help everyone, the issue was I had an office element at the wrong level in the second region.
It looks like your issue is that clfWizardXml.Elements("region")
doesn't return anything, since region
is not directly under the document. Instead you need clfWizardXml.Root.Elements("region")
, or clfWizardXml.Descendants("region")
:
Dim officeList As IEnumerable(Of XElement) = _
From region In clfWizardXml.Root.Elements("region"), _
office In clfWizardXml.Root.Elements("region").Elements("office") _
Where region.Element("settings").Element("name").Value = selectRegion _
And office.Element("name").Value = selectOffice
Select office
Also, since VB.NET supports XML literals, you can make it look a little nicer (to me anyway, you may disagree):
Dim officeList As IEnumerable(Of XElement) =
From region In clfWizardXml...<region>,
office In region.<office>
Where region.<settings>.<name>.Value = selectRegion _
AndAlso office.<name>.Value = selectOffice
Select office
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.