How do you keep .NET XML parsers from expanding parameter entities in XML?

前端 未结 1 1612
渐次进展
渐次进展 2021-01-21 07:26

When I try and parse the xml below (with code below) I keep getting &question;&signature;

expanded to

<         


        
相关标签:
1条回答
  • 2021-01-21 07:52

    Linq-to-XML does not support modeling of entity references -- they are automatically expanded to their values (source 1, source 2). There simply is no subclass of XObject defined for a general entity reference.

    However, assuming your XML is valid (i.e. the entity references exist in the DTD, which they do in your example) you can use the old XML Document Object Model to parse your XML and insert XmlEntityReference nodes into your XML DOM tree, rather than expanding the entity references into plain text:

            using (var sr = new StreamReader(xml))
            using (var xtr = new XmlTextReader(sr))
            {
                xtr.EntityHandling = EntityHandling.ExpandCharEntities; // Expands character entities and returns general entities as System.Xml.XmlNodeType.EntityReference
                var oldDoc = new XmlDocument();
                oldDoc.Load(xtr);
                Debug.WriteLine(oldDoc.DocumentElement.OuterXml); // Outputs <sgml>&question;&signature;</sgml>
                Debug.Assert(oldDoc.DocumentElement.OuterXml.Contains("&question;")); // Verify that the entity references are still there - no assert
                Debug.Assert(oldDoc.DocumentElement.OuterXml.Contains("&signature;")); // Verify that the entity references are still there - no assert
            }
    

    the ChildNodes of each XmlEntityReference will have the text value of the general entity. If a general entity refers to other general entities, as one does in your case, the corresponding inner XmlEntityReference will be nested in the ChildNodes of the outer. You can then compare the old and new XML using the old XmlDocument API.

    Note you also need to use the old XmlTextReader and set EntityHandling = EntityHandling.ExpandCharEntities.

    0 讨论(0)
提交回复
热议问题