来自 VB.net 属性的 Xml 节点值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14747094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:52:32  来源:igfitidea点击:

Xml node value from an attribute in VB.net

xmlvb.netxmlnodexml-attribute

提问by Jigar Shah

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

我如何获得<SubCategoryName>from的值<category name="a">

回答by Steven Doggart

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 XDocumentclass or the older XmlDocumentclass.

正如 Usman 推荐的那样,您可以使用 LINQ,但另一个流行的选择是使用 XPath。您可以使用 XPath 选择使用XDocument类或旧XmlDocument类的匹配元素。

Here's how you would do it with XPath via the XDocumentclass:

以下是通过XDocument该类使用 XPath 执行此操作的方法:

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 XmlDocumentclass:

以下是您将如何通过XmlDocument该类使用 XPath 执行此操作:

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:

以下是 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 /Categorieselement.
  • [@name='a']- The brackets mean that it is a condition--like and Ifstatement, 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 categoryelement that matched that condition.
  • /Categories- 开头的斜杠指示它查看 XML 文档的根目录。斜杠后面是我们在根中寻找的子元素的名称。
  • /category- 我们在元素中寻找的/Categories元素的名称。
  • [@name='a']- 括号意味着它是一个条件——类似和If陈述,各种各样。@ 符号意味着我们正在指定一个属性名称(而不是元素名称)。
  • /SubCategoryName- 我们在category与该条件匹配的元素内寻找的子元素的名称。

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.

XPath 非常强大和灵活。XPath 是一种标准查询语言,许多 XML 工具和技术(例如 XSLT)都在使用它,因此学习它非常有用。此外,有时,即使在文档中,能够通过简单的字符串专门引用文档中的特定 XML 节点也很方便。LINQ 很棒,但它是 Microsoft 的专有技术,如果需要,您不能将 LINQ 路径作为字符串存储在数据库或配置文件中,因此有时 XPath 是一种更可取的方法。

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.

XPath 的另一个变体是//category[@name='a']/SubCategoryName. 开头的双斜杠指示它在文档中的任何位置查找类别元素,而不是在任何特定的父元素下。

回答by sloth

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

resultis now name1.

result现在是name1

回答by Usman

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

回答by SysDragon

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

回答by Rene Zadak

code is fine just instead of

代码很好,而不是

sResult = cat("SubCategoryName").Value

sResult = cat("SubCategoryName").Value

use

sResult = cat("SubCategoryName").InnerText

sResult = cat("SubCategoryName").InnerText