1
sRecieved = "<XmlClient>2.0</XmlClient><XmlVersion>3.0</XmlVersion>" 
Dim xml As New XmlDocument();    
xml.LoadXml(sRecieved);

There are multiple root elements .....i want xmlclient value and xmlversion value

nKognito
  • 6,297
  • 17
  • 77
  • 138
faizan ali
  • 13
  • 1
  • 1
  • 4

2 Answers2

4

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)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • thanx man...now i want value of xclient and xversion..any idea? – faizan ali Jul 25 '12 at 07:38
  • @faizanali: Well personally I'd use LINQ to XML instead of `XmlDocument` - is that an option for you? – Jon Skeet Jul 25 '12 at 07:41
  • @faizanali: Wow, you're really stuck on .NET 2 or 3? Ick. Okay, will have a look at the `XmlDocument` API to see if I can remember it. It may not be nice VB though... – Jon Skeet Jul 25 '12 at 07:59
-1

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)
SMK
  • 2,098
  • 2
  • 13
  • 21