[Mono-list] Xml Entity Resolution

colin at breame.net colin at breame.net
Wed Mar 7 06:26:37 EST 2007


Maybe I am misunderstanding you - see the code below.  When compiled
with gmcs, it throws the XmlException (but not when compiled with mcs -
is this a difference between XmlTextReader vs. XmlTextReader2?) 
XmlTextReader2.Read calls ResolveEntity directly.

C.

using System;
using System.IO;
using System.Xml;

public class test_t {
  public static void Main() {
    TextReader sreader = new StringReader("<doc>&unknown;</doc>");
    XmlTextReader reader = new XmlTextReader(sreader);
    reader.XmlResolver = null;
    XmlDocument doc = new XmlDocument();
    while (!reader.EOF) {
      doc.AppendChild (doc.ReadNode (reader));
    }
  }
}


On Wed, 07 Mar 2007 16:06:04 +0900, "Atsushi Eno" <atsushi at ximian.com>
said:
> Well, you are confused. You can't use XmlDocument.Load() even with
> your blablaXmlReader. XmlTextReader.XmlResolver = null is just a
> replacement to your class. The workaround (using ReadNode method)
> is *still* required.
> 
> To summarize technical side:
> - XmlDocument.Load() does need corresponding entity declarations
> - XmlTextReader does not need corresponding entity.
> - XmlTextReader does not ignore external DTD unless null XmlResolver
>    is set.
> 
> Atsushi Eno
> 
> colin at breame.net wrote:
> > Tried that (see below) but it still gets "System.Xml.XmlException:
> > Reference to undeclared entity" exceptions.
> > 
> > Thanks again,
> > C.
> > 
> > XmlDocument doc = new XmlDocument();
> > XmlTextReader reader = new XmlTextReader(new
> > StringReader(xml_text_with_entities));
> > reader.XmlResolver = null;
> > doc.Load(reader);
> > 
> > On Tue, 06 Mar 2007 06:07:02 +0900, "Atsushi Eno" <atsushi at ximian.com>
> > said:
> >> Well, you could just try XmlTextReader.XmlResolver = null here ...
> >>
> >> Atsushi Eno
> >>
> >> Colin John Nicholas Breame wrote:
> >>> Hi,
> >>>
> >>> I've tried your solution with both XmlTextReader and XmlValidatingReader
> >>> but I can't make it work.  The problem is that the XmlTextReader
> >>> actually requires entities to be resolved.
> >>>
> >>> As a work around I've subclassed XmlTextReader to return false for
> >>> CanResolveEntity, which seems to (at least initially) do the trick.  See
> >>> below.
> >>>
> >>> Thanks
> >>> C.
> >>>
> >>> using System.IO;
> >>> using System.Xml;
> >>>
> >>> public class NonEntityResolvingXmlTextReader : XmlTextReader {
> >>>   public NonResolvingXmlTextReader(TextReader reader) : base(reader) {
> >>>   }
> >>>
> >>>   public override bool CanResolveEntity {
> >>>     get {
> >>>       return false;
> >>>     }
> >>>   }
> >>> }
> >>>
> >>>
> >>> On Mon, 05 Mar 2007 14:00:00 +0900, "Atsushi Eno" <atsushi at ximian.com>
> >>> said:
> >>>> Hi,
> >>>>
> >>>> If you skip the DTD, any entity references (except for &lt;, &gt;, 
> >>>> &apos; and &quot;) are rejected during XmlDocument.Load().
> >>>>
> >>>> If you don't care such buggy document, you can try
> >>>>
> >>>> 	while (!reader.EOF)
> >>>> 		doc.AppendChild (doc.ReadNode (reader));
> >>>>
> >>>> which does not reject unresolved entity references (funny, but
> >>>> this is what .net System.Xml.dll does).
> >>>>
> >>>> Atsushi Eno
> >>>>
> >>>> colin at breame.net wrote:
> >>>>> I'm trying to turn off entity resolution using XmlDocument as I don't
> >>>>> have a DTD and I'm getting "System.Xml.XmlException: Cannot resolve
> >>>>> entity without DTD".  I can't see an explicit "ResolveEntity" property I
> >>>>> could set to off.  Any ideas?  Is there a mono specific way of doing
> >>>>> this?
> >>>>>
> >>>>> Thanks
> >>>>> Colin.
> >>>>> _______________________________________________
> >>>>> Mono-list maillist  -  Mono-list at lists.ximian.com
> >>>>> http://lists.ximian.com/mailman/listinfo/mono-list
> >>>>>
> >>>> _______________________________________________
> >>>> Mono-list maillist  -  Mono-list at lists.ximian.com
> >>>> http://lists.ximian.com/mailman/listinfo/mono-list
> > 
> 


More information about the Mono-list mailing list