XmlTextReader and DTD files

  • Thread starter Jason (Kusanagihk)
  • Start date
J

Jason (Kusanagihk)

To all,

I've tried to write a program to parse / read the contents of an XML
file. Now if the xml file has no DTD references; it seems to be ok (can
parse and get values from the tags); but when the xml file has a DTD
reference an exception pop out saying invalid Text declaration.... (the
error message is not very clear what I've done wrong in the XML files
and for sure I can run the xml files correctly under IE 5!

The xml file(s):
data file:
<?xml version="1.0"?>
<!DOCTYPE Template SYSTEM "TestingTemplate.dtd">
<Template>
<Item>
<Name>a Name</Name>
<Value>a Value</Value>
</Item>

<Item>
<Name>What ??? &CONST_TITLE;</Name>
</Item>
</Template>

the DTD file:
<?xml version="1.0"?>
<!ELEMENT Template (Item+)>
<!ELEMENT Item (Name+, Value?)>
<!ELEMENT Name (#PCDATA)>
<!ELEMENT Value (#PCDATA)>

<!ENTITY CONST_TITLE "*** Constants ***">

Parsing method (C#):
System.Xml.XmlTextReader oReader = null;
System.Collections.Stack oStack = new Stack (50);
System.Text.StringBuilder oSB =
new System.Text.StringBuilder (255);
try
{
oReader = new XmlTextReader (this.mTxt_XmlFile.Text);
while (oReader.Read ())
{
System.Xml.XmlNodeType oType = oReader.NodeType ;

switch (oType)
{
case XmlNodeType.Element:
oSB.Append (oReader.Name + ": " + oReader.Value +
"\r\n");
break;
case XmlNodeType.Text:
oSB.Append ("Text within Tags..." + oReader.Value
+ "\r\n");
break;
case XmlNodeType.EntityReference:
MessageBox.Show (oReader.ProhibitDtd + "");
oSB.Append (oReader.Value + "\r\n");
break;
case XmlNodeType.EndElement:
oSB.Append ("End -- " + oReader.Name + ": " +
oReader.Value + "\r\n");
break;
}

}
this.mTxt_MsgArea00.Text += oSB.ToString ();
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString ());
}
finally
{
if (oReader != null)
{
oReader.Close ();
}
oSB = null;
}


Also the XmlResolver or DTD members of the XmlTextReade is always
null.... I bet the reader can't resovlve the DTD file....

Any ideas what was wrong ???
 
B

Brendan Green

It's probably looking for the DTD somewhere, and can't find it. In this
instance, where would it be looking for the DTD? Possibly in the same
location as the executable?

I notice that the XML file has already been read into memory...is the DTD in
the same location as the source XML file?
 
J

Jason (Kusanagihk)

To Brendon,

Not really, I've put the DTD and xml files in the same directory; I
think the program can read the DTD file since when i changed
<!DOCTYPE Template SYSTEM "TestingTemplate.dtd">
into
<!DOCTYPE Template SYSTEM "noSuchFile.txt">
where there is no such file at all at that directory; it will pop up an
exception saying that the Reader can't find the file.

and horribly, if I removed all the contents in the DTD file; everything
works again.....

So I don't know what's wrong with the DTD file....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top