【问题标题】:XDocumetn.Load from document with version 1.1 in head cause XmlExceptionXDocument.Load from document with version 1.1 in head 导致 XmlException
【发布时间】:2017-05-02 09:57:01
【问题描述】:

我在字符串 var "xml" 中有 xml

<?xml version=""1.1"" encoding=""UTF - 8"" ?>
< insertCommand table=""Colors"">
<parameter name=""Color"">Red</parameter>
<parameter name=""Code"">#FF0000</parameter>
</insertCommand>

我尝试获取 XDocument。

        using (var inputStream = new StringReader(xml))
        {                
            //var doc = new XPathDocument(inputStream);// It`s a same result
            var doc = XDocument.Load(inputStream); // the exception occurs               
            using (var outputStream = doc.CreateWriter())
            {
               ....
            }
        }

System.Xml.XmlException。不允许版本号 1.1。

【问题讨论】:

    标签: load linq-to-xml version xmldocument xmlexception


    【解决方案1】:

    除非 XML 中有特定于 1.1 的内容,否则您可以将其作为字符串读取,将版本更改为 1.0,然后正常处理它。您可以进行网络搜索以找出不同之处。

    在 VB .Net 中这行得通

        Dim path As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
        path = IO.Path.Combine(path, "test.xml")
    
        Dim myXML As String = IO.File.ReadAllText(path)
        Dim repl As String = "<?xml version=""1.1"""
        Dim replwith As String = "<?xml version=""1.0"""
        myXML = myXML.Replace(repl, replwith)
    
        Dim xe As XElement = XElement.Parse(myXML)
    

    FWIW - 您发布的 xml 有一些错误

    <?xml version=""1.1"" encoding="utf-8" ?> encoding attribute
    <insertCommand table=""Colors""> spce before tag name
    <parameter name=""Color"">Red</parameter>
    <parameter name=""Code"">#FF0000</parameter>
    </insertCommand>
    

    【讨论】:

    • Thats exactly what i did. I dont 找到另一个更合适的方法。
    猜你喜欢
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 2017-04-24
    • 2022-12-28
    • 2020-04-18
    相关资源
    最近更新 更多