|
-
Sep 10th, 2012, 09:45 AM
#1
Thread Starter
Frenzied Member
[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 whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
Sep 10th, 2012, 10:05 AM
#2
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
-
Sep 10th, 2012, 06:08 PM
#3
Thread Starter
Frenzied Member
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 whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
Sep 10th, 2012, 06:44 PM
#4
Re: Error reading xml file
Now comes the fun part... which one?
-tg
-
Sep 11th, 2012, 09:45 AM
#5
Thread Starter
Frenzied Member
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 whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
My war with a browser-redirect trojan
-
Sep 11th, 2012, 11:04 AM
#6
Re: Error reading xml file
You can use an XmlTextWriter to write the document header.
vb.net Code:
Using ds As New DataSet("TheDataSet")
Dim dt As New DataTable("TheDataTable")
With dt.Columns
.Add("Field1", GetType(Integer)).AutoIncrement = True
.Add("Field2", GetType(Integer))
End With
With dt.Rows
.Add(Nothing, 11)
.Add(Nothing, 12)
.Add(Nothing, 13)
.Add(Nothing, 14)
.Add(Nothing, 15)
End With
ds.Tables.Add(dt)
Using xtw As New XmlTextWriter("C:\Temp\output.xml", Encoding.UTF8)
xtw.Formatting = Formatting.Indented
xtw.WriteStartDocument()
ds.WriteXml(xtw)
End Using
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.
-
Sep 11th, 2012, 01:50 PM
#7
Thread Starter
Frenzied Member
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 whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|