从 xml doc 元素中检索单个属性值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15432185/
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
Retrieve single attribute value from an xml doc element
提问by dinotom
I have an xml document with the following structure
我有一个具有以下结构的 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>
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.
我正在尝试填充大陆名称的下拉列表,通过访问名称属性并填充列表以绑定到下拉列表,仅检索那些在 xml 文件中包含元素的列表。
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
我似乎得到了正确的语法,我不断收到对象引用错误。这是我的最新迭代,它也不起作用。我将“Continent”传递给函数
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
Working 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
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.
现在,一旦我选择了一个大陆,我就尝试访问国家/地区属性 这是我的最新尝试,似乎都返回了 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
回答by DWRoelands
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

