Guck.
"Tom Goff" <partymonkey@gmail.com> wrote in message
news:1109793833.513595.300610@g14g2000cwa.googlegroups.com...
> I recently wanted to accomplish the same thing (validate an XML file
> using a local DTD file and not the DTD defined in the XML file) and
> could not find a solid answer. I found the answer when trying to
> validate an XML file using XSD, and I wanted to ignore the DOCTYPE
> because it was adding an ever-so-slight overhead to retrieve the DTD.
>
> In short, you can write a custom XmlUrlResolver and then "ignore" the
> remote DTD and load a local DTD.
>
> ex.
>
> XmlTextReader xtr = new XmlTextReader("myfile.xml");
> xtr.XmlResolver = new LocalDocumentTypeResolver("local.dtd");
>
> XmlValidatingReader xvr = new XmlValidatingReader(xtr);
> xvr.ValidationType = ValidationType.DTD;
>
> while (xvr.Read());
>
> ...and the custom resolver...
>
> class LocalDocumentTypeResolver : XmlUrlResolver
> {
> public LocalDocumentTypeResolver(String systemEntry)
> {
> this.systemEntry = systemEntry;
> }
>
> protected String systemEntry = "";
>
> override public object GetEntity(Uri absoluteUri, string role, Type
> ofObjectToReturn)
> {
> Regex re = new Regex(@"(.)*\.dtd$");
> Match m = re.Match(absoluteUri.AbsolutePath);
> if (true == m.Success)
> {
> return new FileStream(systemEntry,
> FileMode.Open,
> FileAccess.Read,
> FileShare.Read);
> }
>
> return base.GetEntity(absoluteUri, role, ofObjectToReturn);
> }
> }
>
>
> Oleg Tkachenko [MVP] wrote:
> > Vlad wrote:
> >
> > > Thanks Oleg for your suggestion. There is still a permance hit
> though
> > > because I still have read the whole source file into a new file
> before
> > > reading the new file again using the validating reader.
> > > I guess I could change your code to generate the new file in memory
> using a
> > > MemoryStream that might improve the performace somewhat but the
> main theme
> > > stays the same--double processing.
> >
> > Yeah, MemoryStream or file or whatever appropriate for your
> application,
> > but unfortunately I don't see how you can avoid this step.
> >
> > --
> > Oleg Tkachenko [XML MVP]
> >
http://blog.tkachenko.com >