Does .Net 4.5 support XML 1.1 yet (for characters invalid in XML 1.0)?

浪尽此生 提交于 2019-11-27 08:15:55
Jon Skeet

No, it doesn't look like XmlReader (the core of much of the XML support in .NET) supports 1.1:

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

class Program
{
    static void Main(string[] args)
    {
        string xml = "<?xml version=\"1.1\" ?><tag>&#x1</tag>";
        var reader = XmlReader.Create(new StringReader(xml));
        while (reader.Read());
    }
}

Output:

Unhandled Exception: System.Xml.XmlException: Version number '1.1' is invalid.
Line 1, position 16.

I've looked at XmlReaderSettings to see if anything there would help, but I don't think it does. Basically I think you're stuck for the moment :(

EDIT: Reading around XML 1.1 a bit, it looks like it's not widely deployed or recommended, so I'm not particularly surprised that it's not supported in .NET 4.5. My guess is that it never will be, given that it's not a particularly new recommendation. The most recent version is the 2nd edition which dates back to 2006. If it's not supported 7 years later, I suspect there'd have to be some significant event to make it worth supporting in the future.

I4V

I am sure this is not the best option but if you download IKVM you can use java classes in your .Net code after referencing a few assemblies (really .Net code :) )

var fXmlFile = new java.io.File(xmlfile);

var dbFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
var dBuilder = dbFactory.newDocumentBuilder();

var doc = dBuilder.parse(fXmlFile);
var nList = doc.getElementsByTagName("controlcharacters");

var chars = nList.item(0).getTextContent().ToCharArray();

XML File:

<?xml version="1.1" ?>
<root>
    <controlcharacters>&#14;&#15;</controlcharacters>
</root>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!