How do I create an XML document with an <!ENTITY> and a <!NOTATION> that XmlDocument.Load() can parse
I am trying to serialize Xml to Bson. This means supporting 开发者_运维技巧all child classes of System.Xml.XmlNode. However, I cannot figure out now to implement an xml document that has an XmlEntity or an XmlNotation in it.
I tried parsing the html 4.0.1 strict dtd with XmlDocument.Load, but I got the following error:
System.Xml.XmlException : 'ENTITY' is an unexpected token. The expected token is 'DOCTYPE'. Line 78, position 3.
Is there another way to end up with an XmlDocument that will have XmlEntity and XmlNotation child nodes?
As a suggestion if you can't find what you are looking for: I have been playing around with SgmlReader, which has the built-in functionality with SgmlDtd to read in an SGML DTD, which is not the same as an XML DTD.
It includes an HTML .dtd modeled after 4.01 transitional, but made to account for more errors, which can parse even very poorly formed HTML. ("Worse" than 4.01 transitional, I believe the example they give is a tag in a element.)
I digress; my actual point is that the HTML DTD is an SGML DTD which differs significantly from an XML dtd, so perhaps that's the issue? If so, I believe you should be able to load the XML DTD's .dtd, into the SgmlReader, which then should allow you to read in any Xml .dtd into the backing store that SgmlReader uses: an XmlDocument. [edit: I found this XmlSpec document from Oct 1, 2005 from www.w3.org; it's the XML DTD; looking to see if I can figure out where the DTD .dtd is, or if it's somehow related...]
The primary intended use for it seems to be for fixing up ill-formed Html to go into Xml, but I don't see any reason why this shouldn't work as well. Let me know if you have issues the code is a little dense in places but it was fairly simple once I got to know it better. [had some coffee, little wine, y'know...] :P
Good luck!
Problem, which Shellybutterfly began to point out with her suggestion to use SgmlReader, is that I was using a SGML dtd, not an xml dtd. I figured out the format of an xml dtd from w3schools.
I discovered you need to create an xml document, with a root element and declare the dtd in the DOCTYPE. to be able to load it with XmlDocument.Load(). For my test case I used:
<!DOCTYPE contact
[
<!NOTATION PublicNotation PUBLIC 'PublicId' >
<!NOTATION SystemNotation SYSTEM 'SystemId' >
]>
<contact>
</contact>
I was able to use that in my tests project.
精彩评论