public void ResolveEntity2() { document.RemoveAll(); 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>"; XmlTextReader xtr = new XmlTextReader(xml, XmlNodeType.Document, null); xtr.Read(); document.AppendChild(document.ReadNode(xtr)); document.AppendChild(document.ReadNode(xtr)); xtr.Close(); Assert.AreEqual(xml, document.OuterXml); XmlNodeReader nr = new XmlNodeReader(document); nr.Read(); // DTD nr.Read(); // root nr.Read(); // &ent3; Assert.AreEqual(XmlNodeType.EntityReference, nr.NodeType); // ent3 does not exist in this dtd. nr.ResolveEntity(); Assert.AreEqual(XmlNodeType.EntityReference, nr.NodeType); nr.Read(); #if false // Hmm... MS.NET returned as it is a Text node. Assert.AreEqual(XmlNodeType.Text, nr.NodeType); Assert.AreEqual(String.Empty, nr.Value); nr.Read(); // Really!? Assert.AreEqual(XmlNodeType.EndEntity, nr.NodeType); Assert.AreEqual(String.Empty, nr.Value); #endif }
public void NodeReaderReadStringWithEmptyXml() { var xmlDoc = new XmlDocument(); var nodeReader = new XmlNodeReader(xmlDoc); Assert.Throws <InvalidOperationException>(() => { nodeReader.ResolveEntity(); }); Assert.Equal(string.Empty, nodeReader.ReadString()); }
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>"; document.LoadXml(xml); Assert.AreEqual(xml, document.OuterXml); XmlNodeReader nr = new XmlNodeReader(document); nr.Read(); // DTD nr.Read(); // root nr.Read(); // &ent; Assert.AreEqual(XmlNodeType.EntityReference, nr.NodeType); Assert.AreEqual(1, nr.Depth, "depth#1"); nr.ResolveEntity(); // It is still entity reference. Assert.AreEqual(XmlNodeType.EntityReference, nr.NodeType); nr.Read(); Assert.AreEqual(XmlNodeType.Text, nr.NodeType); Assert.AreEqual(2, nr.Depth, "depth#2"); Assert.AreEqual("entity string", nr.Value); nr.Read(); Assert.AreEqual(XmlNodeType.EndEntity, nr.NodeType); Assert.AreEqual(1, nr.Depth, "depth#3"); Assert.AreEqual("", nr.Value); nr.Read(); // &ent2; Assert.AreEqual(XmlNodeType.EntityReference, nr.NodeType); Assert.AreEqual(1, nr.Depth, "depth#4"); nr.ResolveEntity(); Assert.AreEqual(xml, document.OuterXml); // It is still entity reference. Assert.AreEqual(XmlNodeType.EntityReference, nr.NodeType); // It now became element node. nr.Read(); Assert.AreEqual(XmlNodeType.Element, nr.NodeType); Assert.AreEqual(2, nr.Depth, "depth#5"); Assert.AreEqual(xml, document.OuterXml); }
public override void ResolveEntity() { if (IsXmlDataNode) { _xmlNodeReader.ResolveEntity(); } else { Fx.Assert("ExtensionDataReader ResolveEntity method should only be called for IXmlSerializable"); } }
public void NodeReaderReadStringWithEntityXml() { string fst = "<!ENTITY fst 'Sample String'>]>"; string dtd = "<!DOCTYPE root[<!ELEMENT root (#PCDATA)>" + fst; string xml = dtd + "<root>&fst;</root>"; XmlNodeReader nodeReader = NodeReaderTestHelper.CreateNodeReader(xml); Assert.True(nodeReader.Read()); //DTD Read Assert.True(nodeReader.Read()); //Root Element Read Assert.Equal("root", nodeReader.Name); Assert.True(nodeReader.Read()); //EntityReference Read Assert.Equal(ReadState.Interactive, nodeReader.ReadState); Assert.Equal(XmlNodeType.EntityReference, nodeReader.NodeType); nodeReader.ResolveEntity(); Assert.True(nodeReader.CanResolveEntity); Assert.Equal("Sample String", nodeReader.ReadString()); }
public static void Main() { XmlNodeReader reader = null; try { //Create and load an XML document. XmlDocument doc = new XmlDocument(); doc.LoadXml("<!DOCTYPE book [<!ENTITY h 'hardcover'>]>" + "<book>" + "<title>Pride And Prejudice</title>" + "<misc>&h;</misc>" + "</book>"); //Create the reader. reader = new XmlNodeReader(doc); 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(); //You must call ResolveEntity to expand the entity reference. //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(); } } }
public void ResolveEntityWithoutDTD() { document.RemoveAll(); string xml = "<root>&ent;&ent2;</root>"; XmlTextReader xtr = new XmlTextReader(xml, XmlNodeType.Document, null); xtr.Read(); document.AppendChild(document.ReadNode(xtr)); xtr.Close(); Assert.AreEqual(xml, document.OuterXml); XmlNodeReader nr = new XmlNodeReader(document); nr.Read(); // root nr.Read(); // &ent; Assert.AreEqual(XmlNodeType.EntityReference, nr.NodeType); // ent does not exist in this dtd. nr.ResolveEntity(); }