format of an XML file when reading
Hi,
Does anyone know if the layout of an xml file is important when using xmlreader to read the elements from and xml file.
I have a strange issue where if the source file is laid out like it would look in notepad++ using prety print and saved then it works just fine when reading in all the elements.
If the file is in one long line without any form of layout then saved, the program misses one specific element when looping through the elements and I've no idea why because there's nothing wrong with the element tag that gets missed.
One long line fails to read the <CNT-VERSION-INHALT> element
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><SW-CNT><IDENT><CNT-DATEI>LALaLALALa.xml</CNT-DATEI><CNT-VERSION-INHALT>1012</CNT-VERSION-INHALT><CNT-VERSION-DATUM>24-11-2016</CNT-VERSION-DATUM></IDENT>
formatted is OK
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SW-CNT>
<IDENT>
<CNT-DATEI>LALaLALALa.xml</CNT-DATEI>
<CNT-VERSION-INHALT>1012</CNT-VERSION-INHALT>
<CNT-VERSION-DATUM>24-11-2016</CNT-VERSION-DATUM>
</IDENT>
I've only shown the first part of the xml file due to sensitive information.
Any suggestions gratefully received
Thanks
Phil
Re: format of an XML file when reading
Please show the code you are using to parse the XML.
Re: format of an XML file when reading
Using your one line example I did this and it found that element,
Code:
'to load from file
' Dim xe As XElement = XElement.Load("path")
' literal for testing
Dim xe As XElement = <SW-CNT><IDENT><CNT-DATEI>LALaLALALa.xml</CNT-DATEI><CNT-VERSION-INHALT>1012</CNT-VERSION-INHALT><CNT-VERSION-DATUM>24-11-2016</CNT-VERSION-DATUM></IDENT></SW-CNT>
Dim selXE As XElement
selXE = xe...<CNT-VERSION-INHALT>.FirstOrDefault
Stop 'examine selXE
So as dday said we need to see more.
Re: format of an XML file when reading
This is the If statement that captures the textfrom the element
Code:
If xmlR.NodeType = XmlNodeType.Element AndAlso xmlR.Name = "CNT-VERSION-INHALT" Then
LblVerXml.Text = xmlR.ReadElementContentAsString
End If
Re: format of an XML file when reading
Quote:
Originally Posted by
gadjet
This is the If statement that captures the textfrom the element
Code:
If xmlR.NodeType = XmlNodeType.Element AndAlso xmlR.Name = "CNT-VERSION-INHALT" Then
LblVerXml.Text = xmlR.ReadElementContentAsString
End If
And? There is no context i.e. where is xmlR defined, is it part of a loop? If you are a beginner with XML you'll find XElement easier IMHO.
Re: format of an XML file when reading
Quote:
Originally Posted by
gadjet
This is the If statement that captures the textfrom the element
Please show the code you are using to parse the XML.
Re: format of an XML file when reading
I will post more of the code next week as I don't have access to it at the moment as I'm not at work.
But the code I've got works without issue on an xml file formatted as per the pretty print view but fails to read one element out of many when it's formatted as a single line so the only difference is if the source file is saved in a formatted way or as a single line.
I was thinking it was a peculiarity of the xmlreader and the format of the source xml file as the code is the same when it works and when it skips the <CNT-VERSION-INHALT> element.
With the file formatted as a single line and stepping through the code the <CNT-VERSION-INHALT> start element is skipped completely and not seen as an element although the </CNT-VERSION-INHALT> end element and all other elements are seen OK
When the file formatted with lines and indentations is stepped through the <CNT-VERSION-INHALT> start element is seen correctly.
Thanks
Re: format of an XML file when reading
Then there's something going on... because it shouldn't matter. I've never seen a difference in how XML is processed based on its format. If it is seeing the ending tag, then it saw the start tag... so yeah, I think a closer look at the code is in order.
-tg
Re: format of an XML file when reading
OK here is the part of the code that reads in the xml file and searches through the tags to place parts of the data into labels on the form along with some other actions.
This code works perfectly for 99% of the time there's just some files that when opened the <CNT-VERSION-INHALT> start tag and data in missed, the only difference I can find is that saving the file formated with indents etc. works and saving as one long line doesn't work.
I cannot think of a reason for this happening, it doesn't make any sense whatsoever!
Code:
Dim xmlR = XmlReader.Create(fn)
Dim xmlFile As String
Dim xmlfilenameArray() As String
Try
Do While xmlR.Read()
If xmlR.NodeType = XmlNodeType.Element AndAlso xmlR.Name = "DATEN-NAME" Then
xmlFile = xmlR.ReadElementContentAsString
xmlfilenameArray = xmlFile.Split("_")
'Load label texts from the xml filename data in dataset
LblWinFileName.Text = fName
Label7.Text = xmlFile ' xmlR.ReadElementContentAsString
LblDA.Text = xmlfilenameArray(0)
LblDiag.Text = xmlfilenameArray(1)
'Check for a bootloader address
If Microsoft.VisualBasic.Left(xmlfilenameArray(2), 2) = "71" Then
MsgBox("This appears to be a Bootloader Dataset" & vbCrLf & "This application cannot be used with Bootloader Datasets")
Else
LblAddr.Text = xmlfilenameArray(2)
End If
LblProd.Text = xmlfilenameArray(3)
LblVer.Text = xmlfilenameArray(4)
LblName.Text = xmlfilenameArray(5)
If xmlfilenameArray.Length > 6 Then
LblFreeText.Text = xmlfilenameArray(6)
Else
LblFreeText.Text = ""
End If
End If
'Extract the Start address from the specific tag
If xmlR.NodeType = XmlNodeType.Element AndAlso xmlR.Name = "START-ADR" Then
LblAddrXml.Text = xmlR.ReadElementContentAsString
End If
If xmlR.NodeType = XmlNodeType.Element AndAlso xmlR.Name = "CNT-VERSION-INHALT" Then
LblVerXml.Text = xmlR.ReadElementContentAsString
End If
'Load first 16 Data bytes into variable then convert to ASCII
Dim HexData As String
If xmlR.NodeType = XmlNodeType.Element AndAlso xmlR.Name = "DATEN" Then
HexData = xmlR.ReadElementContentAsString
LblRawhex.Text = Microsoft.VisualBasic.Left(HexData, 8)
LblDataNmeHex.Text = Microsoft.VisualBasic.Mid(HexData, 9, 24)
TxtFullHex.Text = Microsoft.VisualBasic.Left(HexData, HexData.Length - 8)
RichTextBox1.Text = Microsoft.VisualBasic.Left(HexData, HexData.Length - 8)
LblXmlCrc.Text = Microsoft.VisualBasic.Right(HexData, 8)
Dim testString As String = TxtFullHex.Text
Dim byteArray As Byte() = Hex.ToByteArray(testString)
Dim result As UInt32 = Crc32.ComputeChecksum(byteArray)
Dim stringResult As String = result.ToString("X8") '6889CDCF
LblCalcCRC.Text = stringResult
End If
'Extract full filename from the specific tag <CNT-DATEI>
If xmlR.NodeType = XmlNodeType.Element AndAlso xmlR.Name = "CNT-DATEI" Then
LblCNTDATEI.Text = xmlR.ReadElementContentAsString
End If
Loop
Catch ex As Exception
MsgBox("The File Specified Could not be opened" & vbCrLf & "Error message:" & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.OkOnly, "File Could Not Be Opened!")
End Try
Re: format of an XML file when reading
Try adding this setting and using it in your .Create method call.
https://learn.microsoft.com/en-us/do...norewhitespace
Re: format of an XML file when reading
Quote:
Originally Posted by
dbasnett
Good call but made no difference :-(
Re: format of an XML file when reading
Quote:
Originally Posted by
gadjet
Good call but made no difference :-(
So without seeing the actual file I'm out of suggestions. You should try the code in post #3 and see if it makes a difference.
Re: format of an XML file when reading
Yeah, I would also suggest using an XDocument as opposed to an XmlReader.
Personally I find that the former is more intuitive than the latter but I don't know if there are any differences in the underlying parser.
Re: format of an XML file when reading
Personally I think there's something else wrong here ... I'd
1) post the code
2) post a copy of a file that DOES work
3) post a copy of a file that does NOT work
I think there's something else wrong with the borked xml and it isn't wholy format related. In all the years I've worked with XML, I've never had an issue except for when it was malformed ... which I'm suspecting is the case here...
-tg
Re: format of an XML file when reading
I'll give it a try with xDocument instead of xmlreader to see if that helps.
I can't really post the xml files but I can definitely confirm that the only difference is a "Save As" and the contents of the file are exactly the same unless Notepad++ is adding/removing stuff!
Thanks for taking the time to try to help, I'll update the post if I find something out.
Cheers
Re: format of an XML file when reading
Assuming fn is the path to the XML file put this
Code:
Dim xe As XElement = XElement.Load(fn) ' XElement.Load(fn,LoadOptions.PreserveWhitespace)
xe.Save(fn)
immediately before
Code:
Dim xmlR = XmlReader.Create(fn)
Dim xmlFile As String
Dim xmlfilenameArray() As String
to see if it makes a difference.
Re: format of an XML file when reading
Quote:
Originally Posted by
dbasnett
Assuming fn is the path to the XML file put this
Code:
Dim xe As XElement = XElement.Load(fn) ' XElement.Load(fn,LoadOptions.PreserveWhitespace)
xe.Save(fn)
immediately before
Code:
Dim xmlR = XmlReader.Create(fn)
Dim xmlFile As String
Dim xmlfilenameArray() As String
to see if it makes a difference.
Sneaky but it worked :) the re-written file has the formatting with indents etc.
Thanks
Re: format of an XML file when reading
Quote:
Originally Posted by
gadjet
Sneaky but it worked :) the re-written file has the formatting with indents etc.
Thanks
But... after xe.Save add this. It will let us know if it is XmlReader.
Code:
Dim selXE As XElement
selXE = xe...<CNT-VERSION-INHALT>.FirstOrDefault
Stop 'examine selXE