我想读取一个特定的xml节点,例如它的值
<customers>
<name>John</name>
<lastname>fetcher</lastname>
</customer>
我背后的代码应该是这样的东西(我不知道应该怎么做)。)
Response.Write(xml.Node["name"].Value)
等等等等等等。正如我所说,这只是一个例子,因为我不知道该怎么做。你能帮帮我吗。
感谢。
此致..
答案 0 :(得分:3)
您使用的是哪个版本的.NET?如果您使用的是.NET 3.5并且可以使用LINQ to XML,那就简单如下:
document.Descendant("name").Value
(除了一些错误处理!)如果您使用DOM API,您可能需要:
document.SelectSingleNode("//name").InnerText
请注意,这首先没有显示您如何阅读XML - 如果您需要帮助,请在问题中提供更多详细信息。
答案 1 :(得分:3)
最基本的答案:
假设“xml”是XMLDocument,XMLNodeList,XMLNode等......
Response.Write(xml.SelectSingleNode("//name").innerText)
答案 2 :(得分:2)
如果使用早期版本的.Net框架,请首先查看XMLDocument类,因为这是您将XML字符串加载到的内容。像XMLElement和XMLNode这样的子类对于完成这项工作也很有用。
答案 3 :(得分:1)
尚未尝试过测试,但无论如何都应该指向正确的方向
'Create the XML Document
Dim l_xmld As XmlDocument
'Create the XML Node
Dim l_node As XmlNode
l_xmld = New XmlDocument
'Load the Xml file
l_xmld.LoadXml("XML Filename as String")
'get the attributes
l_node = l_xmld.SelectSingleNode("/customers/name")
Response.Write(l_node.InnerText)