Results 1 to 7 of 7

Thread: [RESOLVED] Error reading xml file

  1. #1
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    741

    Resolved [RESOLVED] Error reading xml file


    I have the following code to read an xml document:
    Code:
                Dim ReportDoc As New Xml.XmlDocument
                Dim Settings As New System.Xml.XmlReaderSettings
    
                Settings.ProhibitDtd = False
                Settings.ValidationType = Xml.ValidationType.None
    
                Using XmlReader As Xml.XmlReader = Xml.XmlTextReader.Create(AppFramework.AppFolders.PrimaryConfigurationReportsFolder & "facility_detail_report.xml", Settings)
                    ReportDoc.Load(XmlReader)
                End Using
    The code is reading the following xml:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <!DOCTYPE report_data
     [
      <!ENTITY hd_attributes  SYSTEM "../hazard_division_attributes.xml" >
      <!ENTITY type_code_attributes     SYSTEM "../type_code_attributes.xml" >
      <!ENTITY facility_data       SYSTEM "facility_export.xml"  >
    ]
    >
    
    <report_data>
    
      &hd_attributes;
      &type_code_attributes;
      &facility_data;
    
    </report_data>
    The VB code errors on the line with ReportDoc.Load(XmlReader). The error it gives is "Invalid text declaration. Line 1, position 21." I'm thinking this may be a red herring, since the line looks like something that is standard to xml files.

    Since I'm kinda new to this whole xml/xsl transformation stuff, I'm not sure if the error is in the vb code or in the xml document. I've been copying and pasting stuff from different examples I've found, so that may be part of the issue as well.

    Any help would be appreciated.
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,651

    Re: Error reading xml file

    &hd_attributes;
    &type_code_attributes;
    &facility_data;

    is the problem... because you're reading it through a stream, it's not able to read the external files and expand out the entities into their proper format. eeeeh..... on the other hand.... check out the other three files you're referencing... the problem may be in there...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * 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??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  3. #3
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    741

    Re: Error reading xml file

    If I take my vb code and point it to a different xml file...
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="es_related_report.xsl"?>
    
    <!DOCTYPE es_related_codes 
     [
      <!ENTITY related_codes  SYSTEM "../qde_related_codes.xml" >
      <!ENTITY type_codes     SYSTEM "../typecodes.xml" >
      <!ENTITY criteria       SYSTEM "../qde_criteria.xml"  >
    ]
    >
    
    
    <es_related_codes>
    
      &related_codes;
      &type_codes;
      &criteria;
    
    </es_related_codes>
    It loads the xml, transforms it with an xsl file, and generates the html output just fine. (I knew I had copied this code from somewhere in the app that worked. ) So I'm thinking that my error is in one of the other files.
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,651

    Re: Error reading xml file

    Now comes the fun part... which one?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * 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??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  5. #5
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    741

    Re: Error reading xml file

    OK. So here's what I discovered.

    Two of the xml files referenced were ones that I had made, while the other one was generated using the Dataset.WriteXml method. I figured the ones I made were probably the ones messed up, so I excluded them from my !DOCTYPE (dtd? is that what that's called?) But the problem was still there. So I looked at the file generated by WriteXml. The topline read as thus:
    Code:
    <?xml version="1.0" standalone="yes" ?>
    I changed standalone to 'no', but that didn't help, so I changed the line to the more standard looking:
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    And now it works fine. Well, it works fine with just one file in the !DOCTYPE. Who know what I'll discover when I add those other two files back in.

    That dataset gets generated on the fly every time I display this xml report. So the question is: How can I fix that first line in the dataset generated xml file to read differently from what is (apparently) the default?
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


  6. #6
    Frenzied Member MattP's Avatar
    Join Date
    Dec 08
    Location
    WY
    Posts
    1,183

    Re: Error reading xml file

    You can use an XmlTextWriter to write the document header.

    vb.net Code:
    1. Using ds As New DataSet("TheDataSet")
    2.             Dim dt As New DataTable("TheDataTable")
    3.             With dt.Columns
    4.                 .Add("Field1", GetType(Integer)).AutoIncrement = True
    5.                 .Add("Field2", GetType(Integer))
    6.             End With
    7.             With dt.Rows
    8.                 .Add(Nothing, 11)
    9.                 .Add(Nothing, 12)
    10.                 .Add(Nothing, 13)
    11.                 .Add(Nothing, 14)
    12.                 .Add(Nothing, 15)
    13.             End With
    14.             ds.Tables.Add(dt)
    15.             Using xtw As New XmlTextWriter("C:\Temp\output.xml", Encoding.UTF8)
    16.                 xtw.Formatting = Formatting.Indented
    17.                 xtw.WriteStartDocument()
    18.                 ds.WriteXml(xtw)
    19.             End Using
    20.         End Using

    Here's the output

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <TheDataSet>
      <TheDataTable>
        <Field1>0</Field1>
        <Field2>11</Field2>
      </TheDataTable>
      <TheDataTable>
        <Field1>1</Field1>
        <Field2>12</Field2>
      </TheDataTable>
      <TheDataTable>
        <Field1>2</Field1>
        <Field2>13</Field2>
      </TheDataTable>
      <TheDataTable>
        <Field1>3</Field1>
        <Field2>14</Field2>
      </TheDataTable>
      <TheDataTable>
        <Field1>4</Field1>
        <Field2>15</Field2>
      </TheDataTable>
    </TheDataSet>
    This pattern in common to all great programmers I know: they're not experts in something as much as experts in becoming experts in something.

    The best programming advice I ever got was to spend my entire career becoming educable. And I suggest you do the same.

  7. #7
    Fanatic Member dolot's Avatar
    Join Date
    Nov 07
    Location
    Foothills of northeast Ga.
    Posts
    741

    Re: Error reading xml file

    Thanks, Matt. That's a lot better than the ugly kludge I came up with.
    I always add to the reputation of those who help me - when the system will allow it.
    My war with a browser-redirect trojan


Posting Permissions

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