Retrieving XML encoding from an XML declaration fragment: XmlDeclaration is not supported for partial content parsing

I am working on some code to read an XML fragment that contains an XML declaration, for example. <?xml version="1.0" encoding="utf-8"?>and analyze the encoding. From MSDN, I have to do it as follows:

var nt = new NameTable();
var mgr = new XmlNamespaceManager(nt);
var context = new XmlParserContext(null, mgr, null, XmlSpace.None);

var reader = new System.Xml.XmlTextReader(@"<?xml version=""1.0"" encoding=""UTF-8""?>", 
    System.Xml.XmlNodeType.XmlDeclaration, context);

However, I get System.Xml.XmlExceptionwhen calling the constructor System.Xml.XmlTextReaderwith an error message:

XmlNodeType XmlDeclaration is not supported for partial content parsing.

I searched for this error in quotation marks - it’s for sure that zero results were found (edit: now there is one result: this post) - and without quotation marks, which does not bring anything useful. I also looked at the MSDN for XmlNodeType and it says nothing that it is not supported.

What am I missing here? How can I get an instance XmlTextReaderfrom an XML declaration fragment ?

Notice my goal here is simply to determine the encoding of a partially constructed XML document , where I assume that it at least contains a node declaration; so I'm trying to get one reader.Encoding. If there is another way to do this, I am open to this.

I am currently processing the ad manually using regular expression, which is not the best approach.

+4
4

: XML XML:

, XmlReader.Create.

GetXmlEncoding ( xmlString) {   if (string.IsNullOrWhiteSpace(xmlString)) ArgumentException ( " null ." );

using (var stringReader = new StringReader(xmlString))
{
    var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };

    using (var xmlReader = XmlReader.Create(stringReader, settings))
    {
        if (!xmlReader.Read()) throw new ArgumentException(
            "The provided XML string does not contain enough data to be valid XML (see https://msdn.microsoft.com/en-us/library/system.xml.xmlreader.read)");

        var result = xmlReader.GetAttribute("encoding");
        return result;
    }
}

XML:

XML coding with XmlReader.Create

System.Text.Encoding, , :

    private static Encoding GetXmlEncoding(string xmlString)
    {
        using (StringReader stringReader = new StringReader(xmlString))
        {
            var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };

            var reader = XmlReader.Create(stringReader, settings);
            reader.Read();

            var encoding = reader.GetAttribute("encoding");

            var result = Encoding.GetEncoding(encoding);
            return result;
        }
    }

:

, XmlTextReader Encoding -property .

, , , :

class Program
{
    static void Main(string[] args)
    {
        var asciiXML = @"<?xml version=""1.0"" encoding=""ASCII""?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";
        var utf8XML = @"<?xml version=""1.0"" encoding=""UTF-8""?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>";

        var asciiResult = GetXmlEncoding(asciiXML);
        var utfResult = GetXmlEncoding(utf8XML);

        Console.WriteLine(asciiResult);
        Console.WriteLine(utfResult);

        Console.ReadLine();
    }
    private static Encoding GetXmlEncoding(string s)
    {
        var stream = new MemoryStream(Encoding.UTF8.GetBytes(s));

        using (var xmlreader = new XmlTextReader(stream))
        {
            xmlreader.MoveToContent();
            var encoding = xmlreader.Encoding;

            return encoding;
        }
    }
}

:

XML encoding output

, XML , , ? , :

        var fragmentResult = GetXmlEncoding(xmlFragment + "<root/>");

XML Fragment

+4

, System.Text.Encoding . .

class Program
{
    static void Main(string[] args)
    {
        var line = File.ReadLines(YourFileName).First();
        var correctXml = line + "<Root></Root>";
        var xml = XDocument.Parse(correctXml);
        var stringEncoding = xml.Declaration.Encoding;
        var encoding = System.Text.Encoding.GetEncoding(stringEncoding);
    }
}
+3

, , XmlDocument

    static string getEncoding(XmlDocument xml)
    {
        if (xml.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
        {
            return (xml.FirstChild as XmlDeclaration).Encoding;
        }
        return "utf-8";
    }
0

, - :

private Encoding getEncoding(byte[] data)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.DtdProcessing = DtdProcessing.Ignore;
            XmlDocument doc = new XmlDocument();
            MemoryStream ms = new MemoryStream(data);
            XmlReader reader = XmlReader.Create(ms, settings);
            doc.Load(reader);
            XmlDeclaration declaration = doc.ChildNodes.OfType<XmlDeclaration>().FirstOrDefault();
            return Encoding.GetEncoding(declaration.Encoding);
        }
0

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


All Articles