从 xml doc 元素中检索单个属性值 [英] Retrieve single attribute value from an xml doc element

查看:31
本文介绍了从 xml doc 元素中检索单个属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an xml document with the following structure

<?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 文件中包含元素的列表.

I am trying to populate a dropdown of the continent names, retrieving only those that have elements in the xml file by accessing there name attribute and populating a list to bind to the dropdown.

我似乎得到了正确的语法,我不断收到对象引用错误.这是我的最新迭代,它也不起作用.我正在将Continent"传递给函数

I can seem to get the syntax right I keep getting an object reference error. Here is my latest iteration, which also doesn't work. I am passing "Continent" to the function

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 个项目.

Now I am trying to access the country attributes once Ive selected a continent This is my latest attempt, all seem to return 0 items.

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

推荐答案

这有点老派,但它有效并且非常可读/可维护...

This is a little older-school, but it works and is very readable/maintainable...

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

这篇关于从 xml doc 元素中检索单个属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆