Xml node value from an attribute in VB.net
Asked Answered
S

5

6

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">?

Similar answered 7/2, 2013 at 8:54 Comment(0)
N
7

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.

Newsman answered 7/2, 2013 at 11:30 Comment(0)
B
0

How about simply

Dim xml = <Categories> 
                <category name="a"> 
                    <SubCategory>1</SubCategory> 
                    <SubCategoryName>name1</SubCategoryName> 
                </category> 
                <category name="b"> 
                    <SubCategory>2</SubCategory> 
                    <SubCategoryName>name2</SubCategoryName> 
                </category> 
               </Categories>

Dim result = xml.<category> _
                .First(Function(e) e.Attribute("name") = "a") _
                .<SubCategoryName>.Value

result is now name1.

Ben answered 7/2, 2013 at 9:17 Comment(0)
W
0

You can use it as

 Dim doc As XDocument = XDocument.Load("YourXMLFileName")
 Dim query = From d In doc.Descendants("Categories").Elements("category")
                Where d.Attribute("name").Value = "a"
                Select d.Element("SubCategoryName").Value
Walter answered 7/2, 2013 at 9:34 Comment(0)
G
0

You can do this:

Dim aux As New Xml.XmlDocument()
Dim nodeLst As Xml.XmlNodeList
Dim sResult As String = String.Empty

aux.Load(sXmlFilePath)
nodeLst = aux.GetElementsByTagName("category")

For Each cat As Xml.XmlElement In nodeLst
    If cat.GetAttribute("name") = "a" Then
        sResult = cat("SubCategoryName").Value
        Exit For
    End If
Next
Gifford answered 7/2, 2013 at 9:40 Comment(0)
H
0

code is fine just instead of

sResult = cat("SubCategoryName").Value

use

sResult = cat("SubCategoryName").InnerText

Hawse answered 4/3, 2015 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.