c# - Are parameter entity references in sgml/xml parsible using .NET? -


when try , parse data below xdocument getting following error:

"xmlexception: parameter entity reference not allowed in internal markup"

here example data trying parse:

<!doctype sgml [   <!element sgml any>   <!entity % std       "standard sgml">   <!entity % signature " &#x2014; &author;.">   <!entity % question  "why couldn&#x2019;t publish books directly in %std;?">   <!entity % author    "william shakespeare"> ]> <sgml>&question;&signature;</sgml> 

here code trying parse file above:

string cafile = @"pathtofile"; using (var castream = file.open(cafile, filemode.open, fileaccess.read)) {     var cadoc = xdocument.load(castream); // exception thrown here! } 

is there way built-in .net xml parsing libraries handle entity references, or @ least ignore embedded !doctype , parse root element?

note: working under assumption parameter entity references valid inside xml. (see here)

there few issues here, appears should using general entities instead:

  1. you defining entities parameter entities. these macros use only inside dtd itself. xml specification:

    parameter-entity references must not appear outside dtd.

    and xml in nutshell 2nd edition:

    it preferable define constant can hold common parts of content specification 5 kinds of listings , refer constant inside content specification of each element. ...

    an entity reference obvious candidate here. however, general entity references not allowed provide replacement text content specification or attribute list, parts of dtd included in xml document itself. instead, xml provides new construct exclusively use inside dtds, parameter entity, referred parameter entity reference. parameter entities behave , declared general entity. however, use % instead of &, , can used in dtd while general entities can used in document content.

    your xml, however, referring entities in document content. suggests should using general entities rather parameter entities.

  2. one of parameter entities, %question, embeds reference parameter entity, %std;, in replacement text. explicitly disallowed xml specification:

    in internal dtd subset, parameter-entity references must not occur within markup declarations; may occur markup declarations can occur. (this not apply references occur in external parameter entities or external subset.)

    again appears should using general entities not parameter entities, since former can used "inside dtd in places included in body of xml document, instance ... in replacement text of entity."

  3. you need enable dtd processing setting xmlreadersettings.prohibitdtd = false (.net 3.5) or xmlreadersettings.dtdprocessing = dtdprocessing.parse (later versions).

putting together, following code:

    string xmlgood = @"<!doctype sgml [   <!element sgml any>   <!entity std       ""standard sgml"">   <!entity signature "" &#x2014; &author;."">   <!entity question  ""why couldn&#x2019;t publish books directly in &std;?"">   <!entity author    ""william shakespeare""> ]> <sgml>&question;&signature;</sgml>";      var settings = new xmlreadersettings { dtdprocessing = dtdprocessing.parse };      using (var sr = new stringreader(xmlgood))     using (var xmlreader = xmlreader.create(sr, settings))     {         var doc = xdocument.load(xmlreader);         console.writeline(doc);     }                

produces following output:

<!doctype sgml [   <!element sgml any>   <!entity std       "standard sgml">   <!entity signature " — &author;.">   <!entity question  "why couldn’t publish books directly in &std;?">   <!entity author    "william shakespeare"> ]> <sgml>why couldn’t publish books directly in standard sgml? — william shakespeare.</sgml> 

and see general entities parsed , expanded.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -