Пример #1
0
        public void ResolveEntity2()
        {
            string ent1 = "<!ENTITY ent 'entity string'>";
            string ent2 = "<!ENTITY ent2 '<foo/><foo/>'>]>";
            string dtd  = "<!DOCTYPE root[<!ELEMENT root (#PCDATA|foo)*>" + ent1 + ent2;
            string xml  = dtd + "<root>&ent3;&ent2;</root>";

            dvr = new XmlValidatingReader(xml, XmlNodeType.Document, null);
            dvr.ValidationType = ValidationType.None;
            dvr.EntityHandling = EntityHandling.ExpandCharEntities;
            dvr.Read();                 // DTD
            dvr.Read();                 // root
            dvr.Read();                 // &ent3;
            Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType);
#if NET_2_0
            // under .NET 2.0, an error is raised here.
            // under .NET 1.1, the error is thrown on the next read.
            try {
                dvr.ResolveEntity();
                Assert.Fail("Attempt to resolve undeclared entity should fail.");
            } catch (XmlException) {
            }
#else
            // ent3 does not exist in this dtd.
            dvr.ResolveEntity();
            Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType);
            try {
                dvr.Read();
                Assert.Fail("Attempt to resolve undeclared entity should fail.");
            } catch (XmlException) {
            }
#endif
        }
        public void ResolveEntity()
        {
            string ent1 = "<!ENTITY ent 'entity string'>";
            string ent2 = "<!ENTITY ent2 '<foo/><foo/>'>]>";
            string dtd  = "<!DOCTYPE root[<!ELEMENT root (#PCDATA|foo)*>" + ent1 + ent2;
            string xml  = dtd + "<root>&ent;&ent2;</root>";

            dvr = new XmlValidatingReader(xml, XmlNodeType.Document, null);
            dvr.ValidationType = ValidationType.None;
            dvr.EntityHandling = EntityHandling.ExpandCharEntities;
            dvr.Read();                 // DTD
            dvr.Read();                 // root
            dvr.Read();                 // &ent;
            Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType);
            Assert.AreEqual(1, dvr.Depth);
            dvr.ResolveEntity();
            // It is still entity reference.
            Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType);
            dvr.Read();
            Assert.AreEqual(XmlNodeType.Text, dvr.NodeType);
            Assert.AreEqual(2, dvr.Depth);
            Assert.AreEqual("entity string", dvr.Value);
            dvr.Read();
            Assert.AreEqual(XmlNodeType.EndEntity, dvr.NodeType);
            Assert.AreEqual(1, dvr.Depth);
            Assert.AreEqual(String.Empty, dvr.Value);

            dvr.Read();                 // &ent2;
            Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType);
            Assert.AreEqual(1, dvr.Depth);
            dvr.ResolveEntity();
            // It is still entity reference.
            Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType);
            // It now became element node.
            dvr.Read();
            Assert.AreEqual(XmlNodeType.Element, dvr.NodeType);
            Assert.AreEqual(2, dvr.Depth);
        }
Пример #3
0
    public static void Main()
    {
        XmlValidatingReader reader    = null;
        XmlTextReader       txtreader = null;

        try
        {
            //Create and load the XmlTextReader with the XML file.
            txtreader = new XmlTextReader("book1.xml");
            txtreader.WhitespaceHandling = WhitespaceHandling.None;

            //Create the XmlValidatingReader over the XmlTextReader.
            //Set the reader to not expand general entities.
            reader = new XmlValidatingReader(txtreader);
            reader.ValidationType = ValidationType.None;
            reader.EntityHandling = EntityHandling.ExpandCharEntities;

            reader.MoveToContent(); //Move to the root element.
            reader.Read();          //Move to title start tag.
            reader.Skip();          //Skip the title element.

            //Read the misc start tag.  The reader is now positioned on
            //the entity reference node.
            reader.ReadStartElement();

            //Because EntityHandling is set to ExpandCharEntities, you must call
            //ResolveEntity to expand the entity.  The entity replacement text is
            //then parsed and returned as a child node.
            Console.WriteLine("Expand the entity...");
            reader.ResolveEntity();

            Console.WriteLine("The entity replacement text is returned as a text node.");
            reader.Read();
            Console.WriteLine("NodeType: {0} Value: {1}", reader.NodeType, reader.Value);

            Console.WriteLine("An EndEntity node closes the entity reference scope.");
            reader.Read();
            Console.WriteLine("NodeType: {0} Name: {1}", reader.NodeType, reader.Name);
        }
        finally
        {
            if (reader != null)
            {
                reader.Close();
            }
        }
    }