XDocumetn.Load from document with version 1.1 in head cause XmlException

81 views Asked by At

I have xml in string var "xml"

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

I try get 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. Not allowed version number 1.1.

1

There are 1 answers

1
dbasnett On

Unless there is something specific to 1.1 in the XML you could read it as a string, change the version to 1.0, and then process it normally. You can do a web search to find out what the differences are.

In VB .Net this worked

    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 - the xml you posted had some errors

<?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>