【问题标题】:Retrieve single attribute value from an xml doc element从 xml 文档元素中检索单个属性值
【发布时间】:2013-03-15 12:13:37
【问题描述】:

我有一个具有以下结构的 xml 文档

<?xml version="1.0" encoding="utf-8" ?>
<CoordinateData>
  <Continent name="Australia">
    <Country name="Australia">
    <Marker custid="1">     
        <LocationName>Port of Brisbane</LocationName>
        <Longitude>153.1678</Longitude>
        <Latitude>-27.3832</Latitude>       
    </Marker>
    <Marker custid="1">     
        <LocationName>Port of Newcastle</LocationName>
        <Longitude>151.7833</Longitude>
        <Latitude>-32.9333</Latitude>       
    </Marker>  
    </Country>
  </Continent>
  <Continent name="North America">
  <Country name="Canada">
        <Marker custid="2">     
            <LocationName>Port of Toronto</LocationName>
            <Longitude>79.3724</Longitude>
            <Latitude>43.633</Latitude>     
        </Marker>
        <Marker custid="2">     
            <LocationName>Port of Vancouver</LocationName>
            <Longitude>122.422</Longitude>
            <Latitude>45.386</Latitude>     
        </Marker>  
    </Country>
  </Continent>
</CoordinateData>

我正在尝试填充大陆名称的下拉列表,通过访问名称属性并填充列表以绑定到下拉列表,仅检索那些在 xml 文件中具有元素的内容。

我似乎得到了正确的语法,但我不断收到对象引用错误。 这是我最新的迭代,它也不起作用。我将“大陆”传递给函数

Public Shared Function GetContinentList(ByVal nodestring As String) As List(Of String)
    Dim doc As New XmlDocument()

    doc.Load(Hosting.HostingEnvironment.MapPath(xmlfilepath_InjectLocation))      
    Dim list As List(Of String) = (From attribute As XmlAttribute In   doc.DocumentElement(nodestring).Attributes() Select (attribute("name").Value)).ToList()

    Return list
End Function

工作函数;

Public Shared Function GetContinents() As List(Of String)
    Dim doc As New XmlDocument()
    doc.Load(Hosting.HostingEnvironment.MapPath(XmlfilepathInjectLocation))
    Return (From node As XmlNode In doc.SelectNodes("//Continent/@name") Select node.InnerText).ToList()

End Function

现在,一旦我选择了一个大陆,我就会尝试访问国家/地区属性 这是我最近的一次尝试,似乎都返回了 0 个项目。

Public Shared Function GetContinentSubItems(ByVal continentname As String) As List(Of String)
    Dim doc As New XmlDocument()
    doc.Load(Hosting.HostingEnvironment.MapPath(XmlfilepathInjectLocation))
    Return (From node As XmlNode In doc.SelectNodes("///Country/@name") Where doc.SelectSingleNode("CoordinateData/Continent").Attributes("name").Value = continentname Select node.InnerText.ToList()
End Function

【问题讨论】:

    标签: xml vb.net


    【解决方案1】:

    这是一个有点老的学校,但它工作并且非常可读/可维护......

    Public Function GetContinents() As List(Of String)
        Dim doc As New XmlDocument
        doc.Load("c:\yourfile.xml")
        Dim ReturnValue As New List(Of String)
        For Each node As XmlNode In doc.SelectNodes("//Continent")
            ReturnValue.Add(node.Attributes("name").Value)
        Next
        Return ReturnValue
    End Function
    

    【讨论】:

    • 需要一些小改动,但这有助于我弄清楚
    • 我同意。 XPath 是解决这个问题的完美解决方案。更好的是,使用doc.SelectNodes("//Continent/@name") 只选择实际的属性值,然后在循环内,使用ReturnValue.Add(node.InnerText) 将它们添加到列表中。
    • 谢谢你们,我现在正在纠结如何在选择大陆后检索大陆下的国家。
    猜你喜欢
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2013-07-07
    相关资源
    最近更新 更多