Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ASCII编码保存xml文件 我有一个程序,使用OpenCV来读取C++程序中的XML文件。我想使用C#程序更改xml文件。当我使用XDocument类的Save函数时,我得到以下错误:“流中的字符无效”_C#_Xml_Encoding_Linq To Xml_Ascii - Fatal编程技术网

使用ASCII编码保存xml文件 我有一个程序,使用OpenCV来读取C++程序中的XML文件。我想使用C#程序更改xml文件。当我使用XDocument类的Save函数时,我得到以下错误:“流中的字符无效”

使用ASCII编码保存xml文件 我有一个程序,使用OpenCV来读取C++程序中的XML文件。我想使用C#程序更改xml文件。当我使用XDocument类的Save函数时,我得到以下错误:“流中的字符无效”,c#,xml,encoding,linq-to-xml,ascii,C#,Xml,Encoding,Linq To Xml,Ascii,当我检查流时,我包含以下字符串: <?xml version="1.0" encoding="utf-8"?> ï» 我认为起始字符是因为utf-8,所以我用ASCII格式保存xml。我的xml文件的编码更改为ascii,但xml文件中写入了Encoding=“us ascii”。我的问题是OpenCv检查编码是否为ASCII,因为它是用usascii编写的,所以会抛出异常。有没有办法在XDocument保存的xml中为编码属性写入ASCII?您可以通过创建一个新的enco

当我检查流时,我包含以下字符串:

<?xml version="1.0" encoding="utf-8"?>
ï»

我认为起始字符是因为
utf-8
,所以我用
ASCII
格式保存xml。我的xml文件的编码更改为ascii,但xml文件中写入了
Encoding=“us ascii”
。我的问题是OpenCv检查编码是否为
ASCII
,因为它是用
usascii
编写的,所以会抛出异常。有没有办法在XDocument保存的xml中为编码属性写入
ASCII

您可以通过创建一个新的
encoding
类并覆盖
WebName
来实现这一点,如下所示:

    public class NonUsAsciiEncoding : ASCIIEncoding
    {
        public override string WebName
        {
            get
            {
                return "ascii";
            }
        }
    }

    private void CreateXml()
    {       
        XmlTextWriter xmlwriter = new XmlTextWriter("c:\\test.xml", new NonUsAsciiEncoding());        

        XDocument xdoc = new XDocument(
          new XElement("Test")
        );

        xdoc.Save(xmlwriter);
        xmlwriter.Close();
    }