2012-06-17 108 views
1

如果我在字符串中有一些xml,我該如何通讀並挑選出每個元素的值?什麼是最快最簡單的方法?asp.net(vb)在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>[email protected]</email>" 
    strXml = strXml & "</customer>" 
    strXml = strXml & "</subscription>" 

感謝,

+0

使用XLINQ。 – SLaks

回答

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()