How to handle external character names when using XmlReader?

I want to link to a URL with character objects using C # /. NET on an XmlReader instance , for example this set of w3c objects that defines  other characters as well.

If I had done this in pure XML, it would be something like this or a variation:
<!ENTITY foo SYSTEM "http://example.org/myent.ent">

I really read XHTML source fragments (containing named objects) and therefore need XML 1.0 / HTML 4 called Entity Definitions / Recognitions defined by w3c .
(I ask how to programmatically reference them on the fly by setting up the XmlReader and its parameters to read snippets, however I am open to the parameters).

In any case, if I do not include these named objects, the reader will cough and produce .NET errors, such as the following XmlException for &nbsp;and other non-numeric objects:

Test error "Xml_Tester.Test_Reading": System.Xml.XmlException: reference to undeclared entity 'nbsp'. Line 6, position 393.

Note. I have successfully referenced the XHTML schema using XmlReaderSettings.Schemas , and suppose there should be an equally easy way to invoke external references to objects without changing the XML source, but it is evading me.


Etc:

I met with the following important information bits when looking for an answer - they are probably useful here ...


, DTD. . 1.5 DTD XML Schema. - http://www.w3.org/TR/xhtml1-schema/#diffs

1.5. DTD XML Schema
DTD XML . DTD (, ), XML-. - http://www.w3.org/TR/xhtml1-schema/#together

XML- XInclude
DTD . Pandora, , , , Doctype , DTD .
- http://msdn.microsoft.com/en-us/library/aa302291.aspx#xinc_topic1

+3
1

XMLReader XHTML, , &nbsp;, XmlException

XML W3C: XHTML 1.0 XML-, 1.5. DTD XML Schema :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"[
<!ATTLIST html
    xmlns:xsi CDATA #FIXED "http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation CDATA #IMPLIED
>
]>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.w3.org/1999/xhtml
                          http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd">
  ...
</html>

am , XHTML, . <body><div><b>xhtml stuff</b></div></body> ... .

DTD ( ) . XMLReader XMLExeption, .
!


#.NET,

using System;
using System.IO;
using System.Xml;

. . . , .

XmlReaderSettings settingsXRdr = new XmlReaderSettings();
settingsXRdr.ProhibitDtd = false;
settingsXRdr.CheckCharacters = true;
settingsXRdr.ConformanceLevel = ConformanceLevel.Document;
settingsXRdr.IgnoreProcessingInstructions = false;
settingsXRdr.IgnoreComments = false;
settingsXRdr.XmlResolver = new CustomXmlResolver();
settingsXRdr.ValidationType = ValidationType.DTD;

// This is a format string; notice the placeholder {0} where the fragment will be injected:

string mixFmtString1 = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd""[
<!ATTLIST html
xmlns:xsi CDATA #FIXED ""http://www.w3.org/2001/XMLSchema-instance""
xsi:schemaLocation CDATA #IMPLIED
>
]>
<html xmlns=""http://www.w3.org/1999/xhtml"" lang=""en"" xml:lang=""en""
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xsi:schemaLocation=""http://www.w3.org/1999/xhtml
              http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd"">
<head><title></title></head>
<body>
<div>{0}</div>
</body>
</html>";


// Inject any well-formed fragment via the second argument
string xhtml = string.Format(mixFmtString1, "<b>Xhtml fragment w/named entity: &nbsp;</b>");

// Creates a validating reader (derived type) because of the above settings)
XmlReader rdr = XmlReader.Create(new StringReader(xhtml), settingsXRdr);

// Reads the entire XHTML document (validating it along the way).
while (rdr.Read()) {

    // Do whatever you want here for each piece processed.
    var dummy = rdr.NodeType.ToString();  // Access a string value for fun.
    // If you just want validation to occur then leave this an empty code block. 

}

. Strict XHTML, , <center>, . , , XHTML.

/ :

+1

Source: https://habr.com/ru/post/1763383/


All Articles