I have a XML like
<Categories>
<category name="a">
<SubCategory>1</SubCategory>
<SubCategoryName>name1</SubCategoryName>
</category>
<category name="b">
<SubCategory>2</SubCategory>
<SubCategoryName>name2</SubCategoryName>
</category>
</Categories>
How do I get the value of <SubCategoryName>
from <category name="a">
?
As Usman recommended, you can use LINQ, but another popular option is to use XPath. You can use XPath to select matching elements using either the XDocument
class or the older XmlDocument
class.
Here's how you would do it with XPath via the XDocument
class:
Dim doc As New XDocument()
doc.Load(filePath)
Dim name As String = doc.XPathSelectElement("/Categories/category[@name='a']/SubCategoryName").Value
And here's how you would do it with XPath via the XmlDocument
class:
Dim doc As New XmlDocument()
doc.Load(filePath)
Dim name As String = doc.SelectSingleNode("/Categories/category[@name='a']/SubCategoryName").InnerText
Here's the meaning of the parts of the XPath:
/Categories
- The slash at the begining instructs it to look in the root of the XML document. The slash is followed by the name of the sub-element we are looking for in the root./category
- The name of the element we are looking for within the /Categories
element.[@name='a']
- The brackets mean that it is a condition--like and If
statement, of sorts. The @ symbol means that we are specifying an attribute name (as opposed to an element name)./SubCategoryName
- The name of the sub-element that we are looking for inside of the category
element that matched that condition.XPath is very powerful and flexible. XPath is a standard query language that is used by many XML tools and technologies, such as XSLT, so it is very useful to learn. Besides, sometimes, even in documentation, it's handy to be able to specifically reference a particular XML node in a document via a simple string. LINQ is great, but it is a proprietary Microsoft technology and you can't store the LINQ path as a string in a database or configuration file, if necessary, so sometimes XPath is a preferrable method.
Another variation of the XPath would be //category[@name='a']/SubCategoryName
. The double-slash at the beginning instructs it to find the category element anywhere in the document, rather than under any particular parent element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With