There are multiple root elements load xml

后端 未结 2 489
无人共我
无人共我 2020-12-21 21:02
sRecieved = \"2.03.0\" 
Dim xml As New XmlDocument();    
xml.LoadXml(sRecieved);


        
相关标签:
2条回答
  • 2020-12-21 21:06

    This error is occur because there is no root element in your xml string.

    Try this

    sRecieved = "<xmlroot><XmlClient>2.0</XmlClient><XmlVersion>3.0</XmlVersion></xmlroot>"
    
    Dim xml As New XmlDocument()
    
    xml.LoadXml(sRecieved)
    
    0 讨论(0)
  • 2020-12-21 21:28

    Well yes, your data isn't a valid XML document. (The error message is pretty clear - you've got multiple top-level elements.) You could make it a valid document by adding a dummy root element:

    xml.LoadXml("<root>" & sReceived & "</root>")
    

    ... but if you get the chance to change whatever's sending the data, it would be better if it sent an actual XML document.

    EDIT: If you're able to use LINQ to XML instead of XmlDocument, getting the client number and the version number are easy. For example, as text:

    Dim clientVersion = doc.Root.Element("XmlClient").Value
    Dim xmlVersion = doc.Root.Element("XmlVersion").Value 
    

    EDIT: Okay, if you're stuck with XmlDocument, I believe you could use:

    Dim clientVersionNode = doc.DocumentElement.GetElementsByTagName("XmlClient")(0)
    Dim clientVersion = (CType(clientVersionNode, XmlElement)).InnerText
    

    (and likewise for xmlVersion)

    0 讨论(0)
提交回复
热议问题