Serializing from XML with remote DTD

N

Nathan Baker

Hey,

I have an XML file which I am serializing into a class using the
following code:
//
XmlSerializer s = new XmlSerializer(typeof(edition));
StreamReader r = new StreamReader(ConfigFileName);
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ProhibitDtd = false;
xrs.ValidationType = ValidationType.None;
XmlReader xr = XmlReader.Create(r, xrs);
currentEdition = s.Deserialize(xr) as edition;
//
So this works dandy when I'm connected to the Internet. But
unfortunately, the XML file has a dtd in it that points to a remote
site. So when I yank the cat5, I get an error (An error has occurred
while opening external DTD '...: The remote name could not be
resolved)

Obviously, setting 'ProhibitDtd' to true causes an exception as well.

So is there a way to serialize the XML file while ignoring the DTD? I
mean, it's not trying to validate it (ValidationType is None), so why
does it need the DTD? It's just mapping tag names to class names and
attributes to fields. Is there a way to do this, or am I going to have
to cache the DTD locally? -_-

Thanks,
Nathan
 
M

Martin Honnen

Nathan said:
I have an XML file which I am serializing into a class using the
following code:
//
XmlSerializer s = new XmlSerializer(typeof(edition));
StreamReader r = new StreamReader(ConfigFileName);
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ProhibitDtd = false;
xrs.ValidationType = ValidationType.None;
XmlReader xr = XmlReader.Create(r, xrs);
currentEdition = s.Deserialize(xr) as edition;
//
So this works dandy when I'm connected to the Internet. But
unfortunately, the XML file has a dtd in it that points to a remote
site. So when I yank the cat5, I get an error (An error has occurred
while opening external DTD '...: The remote name could not be
resolved)

Set
xrs.XmlResolver = null;
that way external resources like the DTD are not fetched. However if the
DTD defines entities that are referenced in the XML document you will
need a custom resolver that resolves the DTD uri to a local copy.
 
N

Nathan Baker

Hi Martin,

Thanks for the suggestion. It looks like it's working out. Much
appreciated.

Thanks,
Nathan
 

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