我如何将xml转换为字符串值

时间:2018-10-11 15:42:13

标签: vb.net

我有这个xml,但是我需要转换为字符串值

  <CheckList>
            <CheckListItem>
                <CheckListItemID>1532</CheckListItemID>
                <Notes>Note1</Notes>
                <Termination_ID>1532</Termination_ID>
                <Titel>Access Card</Titel>
            </CheckListItem>

            <CheckListItem>
                <CheckListItemID>1533</CheckListItemID>
                <Notes>note2</Notes>
                <Termination_ID>1532</Termination_ID>
                <Titel>Badge</Titel>
            </CheckListItem>

            <CheckListItem>
                <CheckListItemID>1531</CheckListItemID>
                <Notes>note3</Notes>
                <Termination_ID>1532</Termination_ID>
                <Titel>Laptop</Titel>
            </CheckListItem>
        </CheckList>

我需要将此Xml转换为String

我想要这些输出

Access Card : Note1 , Badge : Note2 , Laptop : note3

1 个答案:

答案 0 :(得分:0)

使用Xpath。您可能需要将system.xml和system.xml.xpath导入到页面中

    Dim myxml As String = "<CheckList><CheckListItem><CheckListItemID>1532</CheckListItemID><Notes>Note1</Notes><Termination_ID>1532</Termination_ID><Titel>AccessCard</Titel></CheckListItem><CheckListItem><CheckListItemID>1533</CheckListItemID><Notes>note2</Notes><Termination_ID>1532</Termination_ID><Titel>Badge</Titel></CheckListItem><CheckListItem><CheckListItemID>1531</CheckListItemID><Notes>note3</Notes><Termination_ID>1532</Termination_ID><Titel>Laptop</Titel></CheckListItem></CheckList>"
    Dim xp As XPathDocument = New XPathDocument(New StringReader(myxml.ToString))
    Dim xn As XPathNavigator = xp.CreateNavigator
    Dim xi As XPathNodeIterator = xn.Select("//CheckList/CheckListItem")
    Dim sb As New StringBuilder
    Do While xi.MoveNext
        sb.Append(xi.Current.SelectSingleNode("Titel").InnerXml & " : " & xi.Current.SelectSingleNode("Notes").InnerXml & " , ")
    Loop
    sb.Length = sb.Length - 3
    Response.Write(sb.ToString.TrimEnd(" , "))

这将输出

AccessCard:Note1,徽章:note2,笔记本电脑:note3