从XML获取所有属性

从XML获取所有属性,xml,vb.net,Xml,Vb.net,我有这样一个XML: <list> <sublist id="a"> <item name="name1"> <property1>a</property1> <property2>b</property2> </item> <item name="name2">

我有这样一个XML:

<list>
    <sublist id="a">
        <item name="name1">
            <property1>a</property1>
            <property2>b</property2>
        </item>
        <item name="name2">
            <property1>c</property1>
            <property2>d</property2>
        </item>
    </sublist>
    <sublist id="b">
        [...more XML here...]
    </sublist>
</list>
但很明显,我得到了整个XML。如何返回新的XML?
谢谢

不清楚这是否是您想要的,但这将为您提供所有
id
值的列表:

Dim ids As New List(Of String)()
For Each i As XmlNode In xmlData.SelectNodes("//list/sublist/@id")
    ids.Add(i.Value)
Next

但是,我建议您也可以考虑使用XSLT来完成这项任务。XSLT非常适合将XML从一种格式转换为另一种格式。例如,此XSLT脚本将把您提供的XML转换为示例所需的输出:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/list">
    <list>
      <xsl:apply-templates select="sublist"/>
    </list>
  </xsl:template>

  <xsl:template match="/list/sublist">
    <sublist>
      <xsl:attribute name="id">
        <xsl:value-of select="@id"/>
      </xsl:attribute>
    </sublist>
  </xsl:template>
</xsl:stylesheet>

不清楚这是否是您想要的,但这将为您提供所有
id
值的列表:

Dim ids As New List(Of String)()
For Each i As XmlNode In xmlData.SelectNodes("//list/sublist/@id")
    ids.Add(i.Value)
Next

但是,我建议您也可以考虑使用XSLT来完成这项任务。XSLT非常适合将XML从一种格式转换为另一种格式。例如,此XSLT脚本将把您提供的XML转换为示例所需的输出:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/list">
    <list>
      <xsl:apply-templates select="sublist"/>
    </list>
  </xsl:template>

  <xsl:template match="/list/sublist">
    <sublist>
      <xsl:attribute name="id">
        <xsl:value-of select="@id"/>
      </xsl:attribute>
    </sublist>
  </xsl:template>
</xsl:stylesheet>


谢谢,第一个对我来说应该很好。我会调查一下XSLT。谢谢,第一个对我来说应该很好。我将研究XSLT。