我正在编写.asmx网络服务,以便从XML文件中返回所有匹配所请求模型的汽车。
在ASP.net中使用VB,你能建议我如何:
1)首先找到所请求的make的匹配,然后 2)退回所有型号?
以下是XML的示例。谢谢!
<cars>
<car>
<carmake>Acura</carmake>
<carmodels>
<carmodel>ILX</carmodel>
<carmodel>MDX</carmodel>
<carmodel>RDX</carmodel>
</carmodels>
</car>
<car>
<carmake>Aston Martin</carmake>
<carmodels>
<carmodel>DB9</carmodel>
<carmodel>DBS</carmodel>
<carmodel>Rapide</carmodel>
</carmodels>
</car>
</cars>
答案 0 :(得分:1)
您可以使用XPath
来实现Dim doc As New Xml.XmlDocument
doc.LoadXml("yourinputxmlasstring")
Dim maker As String = "Aston Martin"
For Each x As Xml.XmlNode In doc.SelectNodes("cars/car[carmake='" + maker + "']")
For Each p As Xml.XmlNode In x.SelectNodes("carmodels/carmodel")
Console.WriteLine(p.InnerText)
Next
Next