//[NotWorking ("default namespace seems null, not String.Empty")]
        public void DefaultXmlnsAttributeLookup()
        {
            string xml = @"<!DOCTYPE X [
			<!ELEMENT X (Y)+>
			<!ENTITY baz 'urn:baz'>
			<!ATTLIST X
				xmlns CDATA 'urn:foo'
				xmlns:bar CDATA 'urn:bar'
				xmlns:baz CDATA #IMPLIED
				dummy CDATA 'dummy'
				baz:dummy CDATA 'dummy'>
			<!ELEMENT Y (#PCDATA)*>
			<!ATTLIST Y
				xmlns CDATA #IMPLIED
				xmlns:bar CDATA #IMPLIED>
			]>
			<X xmlns:baz='&baz;'><Y/><Y>text.</Y><Y xmlns='' xmlns:bar='urn:hoge'>text.</Y></X>"            ;
            XmlValidatingReader xvr = new XmlValidatingReader(
                xml, XmlNodeType.Document, null);

            xvr.Read();              // DTD
            xvr.Read();              // whitespace
            xvr.Read();
            Assert.AreEqual("urn:foo", xvr.LookupNamespace(String.Empty), "#1-1");
            Assert.AreEqual("urn:bar", xvr.LookupNamespace("bar"), "#1-2");

            Assert.AreEqual("urn:baz", xvr.LookupNamespace("baz"), "#1-a");
            Assert.IsTrue(xvr.MoveToAttribute("baz:dummy"), "#1-b");
            Assert.AreEqual("urn:baz", xvr.NamespaceURI, "#1-c");

            Assert.IsTrue(xvr.MoveToAttribute("dummy"), "#1-d");
            Assert.AreEqual(String.Empty, xvr.NamespaceURI, "#1-e");

            xvr.Read();              // first Y, empty element
            Assert.AreEqual("urn:foo", xvr.LookupNamespace(String.Empty), "#2-1");
            Assert.AreEqual("urn:bar", xvr.LookupNamespace("bar"), "#2-2");
            xvr.Read();              // second Y, start element
            Assert.AreEqual("urn:foo", xvr.LookupNamespace(String.Empty), "#3-1");
            Assert.AreEqual("urn:bar", xvr.LookupNamespace("bar"), "#3-2");
            xvr.Read();              // inside normal Y. Check inheritance
            Assert.AreEqual("urn:foo", xvr.LookupNamespace(String.Empty), "#4-1");
            Assert.AreEqual("urn:bar", xvr.LookupNamespace("bar"), "#4-2");
            xvr.Read();              // second Y, end element
            Assert.AreEqual("urn:foo", xvr.LookupNamespace(String.Empty), "#5-1");
            Assert.AreEqual("urn:bar", xvr.LookupNamespace("bar"), "#5-2");
            xvr.Read();              // third Y, suppresses default namespaces
            Assert.AreEqual(null, xvr.LookupNamespace(String.Empty), "#6-1");
            Assert.AreEqual("urn:hoge", xvr.LookupNamespace("bar"), "#6-2");
            xvr.Read();              // inside suppressing Y. Check inheritance
            Assert.AreEqual(null, xvr.LookupNamespace(String.Empty), "#7-1");
            Assert.AreEqual("urn:hoge", xvr.LookupNamespace("bar"), "#7-2");
            xvr.Read();              // end of suppressing element
            Assert.AreEqual(null, xvr.LookupNamespace(String.Empty), "#8-1");
            Assert.AreEqual("urn:hoge", xvr.LookupNamespace("bar"), "#8-2");
        }
        public void ResolveEntityReadAttributeValue()
        {
            string dtd = "<!DOCTYPE root [<!ELEMENT root (#PCDATA)*><!ATTLIST root attr CDATA #REQUIRED><!ENTITY ent 'entity string'>]>";
            string xml = dtd + "<root attr='&ent; text'>&ent;</root>";

            dvr = new XmlValidatingReader(xml, XmlNodeType.Document, null);
            dvr.Read();                                      // doctype
            dvr.Read();                                      // root
            dvr.MoveToAttribute(0);                          // attr
            Assert.IsTrue(dvr.ReadAttributeValue());         // Should read expanded text
            Assert.AreEqual(XmlNodeType.Text, dvr.NodeType); // not EntityReference
            Assert.AreEqual("entity string text", dvr.Value);
            Assert.IsTrue(!dvr.ReadAttributeValue());
        }
Пример #3
0
    public static void Main()
    {
        XmlValidatingReader reader = null;

        try
        {
            //Create the XML fragment to be parsed.
            string xmlFrag = "<book genre='novel' misc='sale-item &h; 1987'></book>";

            //Create the XmlParserContext.
            XmlParserContext context;
            string           subset = "<!ENTITY h 'hardcover'>";
            context = new XmlParserContext(null, null, "book", null, null, subset, "", "", XmlSpace.None);

            //Create the reader and set it to not expand general entities.
            reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
            reader.ValidationType = ValidationType.None;
            reader.EntityHandling = EntityHandling.ExpandCharEntities;

            //Read the misc attribute. Because EntityHandling is set to
            //ExpandCharEntities, the attribute is parsed into multiple text
            //and entity reference nodes.
            reader.MoveToContent();
            reader.MoveToAttribute("misc");
            while (reader.ReadAttributeValue())
            {
                if (reader.NodeType == XmlNodeType.EntityReference)
                {
                    //To expand the entity, call ResolveEntity.
                    Console.WriteLine("{0} {1}", reader.NodeType, reader.Name);
                }
                else
                {
                    Console.WriteLine("{0} {1}", reader.NodeType, reader.Value);
                }
            }
        }
        finally
        {
            if (reader != null)
            {
                reader.Close();
            }
        }
    }
Пример #4
0
    public static void Main()
    {
        XmlValidatingReader reader = null;

        try
        {
            //Create the string to parse.
            string xmlFrag = "<book genre='novel' ISBN='1-861003-78' pubdate='1987'></book> ";

            //Create the XmlNamespaceManager.
            NameTable           nt    = new NameTable();
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);

            //Create the XmlParserContext.
            XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);

            //Create the XmlValidatingReader .
            reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);

            //Read the attributes on the root element.
            reader.MoveToContent();
            if (reader.HasAttributes)
            {
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    reader.MoveToAttribute(i);
                    Console.WriteLine("{0} = {1}", reader.Name, reader.Value);
                }
                //Move the reader back to the node that owns the attribute.
                reader.MoveToElement();
            }
        }

        finally
        {
            if (reader != null)
            {
                reader.Close();
            }
        }
    }