asp.net(vb)读取xml中的元素值

时间:2012-06-17 04:05:40

标签: asp.net xml xml-parsing

如果我在字符串中有一些xml,我该如何读取并选出每个元素的值?什么是最快捷最简单的方式?

    Dim strXml As String = "<subscription>"
    strXml = strXml & "<reference>abc123</reference>"
    strXml = strXml & "<status>active</status>"
    strXml = strXml & "<customer>"
    strXml = strXml & "<fname>Joe</fname>"
    strXml = strXml & "<lname>bloggs</lname>"
    strXml = strXml & "<company>Bloggs inc</company>"
    strXml = strXml & "<phone>1234567890</phone>"
    strXml = strXml & "<email>joebloggs@hotmail.com</email>"
    strXml = strXml & "</customer>"
    strXml = strXml & "</subscription>"

谢谢,

1 个答案:

答案 0 :(得分:0)

你可以使用Linq到xml,如下面的代码:

    Dim xmlE As XElement = XElement.Parse(strXml)

    Dim str As String = (From el In xmlE.Elements("customer").Elements("fname")
                        Select el.Value).Single()