Results 1 to 18 of 18

Thread: format of an XML file when reading

  1. #1

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    63

    Question 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

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: format of an XML file when reading

    Please show the code you are using to parse the XML.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    63

    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

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: format of an XML file when reading

    Quote Originally Posted by gadjet View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: format of an XML file when reading

    Quote Originally Posted by gadjet View Post
    This is the If statement that captures the textfrom the element
    Please show the code you are using to parse the XML.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    63

    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

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    63

    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
    Last edited by gadjet; Oct 17th, 2023 at 04:54 AM. Reason: typo

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    63

    Re: format of an XML file when reading

    Quote Originally Posted by dbasnett View Post
    Try adding this setting and using it in your .Create method call.

    https://learn.microsoft.com/en-us/do...norewhitespace
    Good call but made no difference :-(

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: format of an XML file when reading

    Quote Originally Posted by gadjet View Post
    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    63

    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
    Last edited by gadjet; Oct 18th, 2023 at 02:02 AM. Reason: typo

  16. #16
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  17. #17

    Thread Starter
    Member
    Join Date
    May 2009
    Posts
    63

    Re: format of an XML file when reading

    Quote Originally Posted by dbasnett View Post
    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

  18. #18
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: format of an XML file when reading

    Quote Originally Posted by gadjet View Post
    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width