Last post Oct 06, 2008 10:52 AM by polynaux
Oct 03, 2008 08:09 AM|polynaux|LINK
I am trying to read the XML data via the XMLData Byte Array and Memory Stream. - thats how I avoided the "..hexadecimal 0x00 error...."
Now I am facing the next issue: "Data at the root level is invalid".
I googled a while and found different ways to handle this erorr. But it did not work for me.
'XMLReader = New XmlTextReader(OpenFileDialogXMLFile.FileName)
'Dim XMLData As Byte()
'Since(Format() Is UTF8)
'XMLData = System.Text.Encoding.UTF8.GetBytes(XMLReader.Read)
Dim memStream As System.IO.MemoryStream = Nothing
Dim waArray As Byte() = System.Text.Encoding.UTF8.GetBytes(OpenFileDialogXMLFile.FileName)
memStream = New System.IO.MemoryStream(waArray)
'Dim memStream As New System.IO.MemoryStream(XMLData) 'As New MemoryStream()
'memStream.Position = 0
Dim xDocument As New XmlDocument()
' Dim xdoc As XDocument = xdoc.Load(OpenFileDialogXMLFile.FileName)
' xDocument.Declaration = New XDeclaration("1.0", "utf-8", "yes")
TextBoxOutput.Text = xDocument.ToString()
Catch ExceptionInLoadingXML As Exception
It would be great if you have any suggestions,
Oct 03, 2008 10:11 AM|O11Y|LINK
I've often found that comments at the root level tend to shaft the .NET XML validator. Whether you are allowed comments at the root level or not I'm unsure... [:D].
Oct 03, 2008 11:42 AM|polynaux|LINK
well, the xml is auto -generated from some OLAP tool. So I need to handle it . I cant change what comes out of that third party tool.
I just need to import it and read out the attributes.
Oct 03, 2008 02:29 PM|O11Y|LINK
XML is just text, so if you read it into a string you can pretty much do what you like with it....
but that's besides the point. The XmlDocument will only allow one Root level node:
Because in the XML schema, only one root node is allowed (which makes sense if you think about it). If your XML has more than one root node you will not be able to load it into an XmlDocument. I'm not sure what the alternatives are but there might be an alternative
"XML Snippet" type object that will allow it. Failing that you're going to have to have to get your hands dirty with that string, in which case an XSLT transform might be a good option as you could transform your snippet into something you can use.
Oct 06, 2008 09:00 AM|polynaux|LINK
thanks for your help. Building on your thoughts I may just read in the xml as a string and search the string for all nodes which I then just print out and do my stuff with it.
Oct 06, 2008 09:04 AM|O11Y|LINK
Why don't you dump it in a string and do:
myXml = "<DOCUMENT>" + myXml + "</DOCUMENT>";
now you have one root node and can do:
Oct 06, 2008 09:12 AM|polynaux|LINK
the document has well over 3000 lines and the other erros, like the invalid xml symbols in it $,£ will follow after I fixed this one. I think to get them all out
of the way I stick with the reading as bytes or string and searching the string for the occurence of "MyNodeAttribute".
Oct 06, 2008 09:21 AM|ksridharbabuus|LINK
Can you please go through the following forum thread on similar error:
This might solve your problem.
Oct 06, 2008 09:29 AM|polynaux|LINK
I tried it all. since days now. it just doesn't work because the xml is invalid(ill formed). -its an open source tool that generates these xml files :/
My plan is just to read the xml as a string or byte. which I am trying here: (somehow my textboxoutput just shows the file path rather then the content).
'I am quite close with that method (just that it only shows the first 2 lines of the document
Dim f As System.IO.FileStream
Dim r As System.IO.StreamReader
Dim mylength As Integer
Dim i As Integer
Dim mybuffer(9999) As Char
f = New System.IO.FileStream(OpenFileDialogXMLFile.FileName, IO.FileMode.Open, IO.FileAccess.Read)
r = New System.IO.StreamReader(f)
r.Read(mybuffer, 0, 9999)
TextBoxOutput.Text = mybuffer
Oct 06, 2008 10:20 AM|ksridharbabuus|LINK
In the above discussion thread first we are loading the xml content to a file stream and after doing necessary changes we are loading into the xmldocument object.
Is it possible to provide the sample xml segment where it is throwing an error.
Oct 06, 2008 10:52 AM|polynaux|LINK
Well, ksridharbabuus. As we have derived, my XML document has two root's - which I don't want
to play around with (..xml content...is already bad for me since my xml content is invalid xml)
Furthermore it has special (non-valid) characters like $ , £ , so the
error like "invalid hexadecimal 0x00" will follow. I am not trying any other XML
method again. I have done that since 3 days and I am fine with it now.
I stick to the non-xml treatment i.e. as a string.
Finally, you already mentioned your solution: http://forums.asp.net/t/1325084.aspx
and it did not work for me.