Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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
C# 新的utf8编码(错误)仍在写入utf8 BOM_C#_Xml_Utf 8_Byte Order Mark - Fatal编程技术网

C# 新的utf8编码(错误)仍在写入utf8 BOM

C# 新的utf8编码(错误)仍在写入utf8 BOM,c#,xml,utf-8,byte-order-mark,C#,Xml,Utf 8,Byte Order Mark,我在代码中关闭了BOM表,但它仍在xml文档中打印它。我不明白。我已经查阅了很多资料,但仍然一无所获,应该从 我的代码是通用的 XDocument xmlDoc = XDocument.Load(CompDir + File.Name); AppendToFile(xmlDoc, aDataRow); using (var writer = new XmlTextWriter (FilePrep.CompletedDirectory + File.Name, new UTF8Enco

我在代码中关闭了BOM表,但它仍在xml文档中打印它。我不明白。我已经查阅了很多资料,但仍然一无所获,应该从

我的代码是通用的

XDocument xmlDoc = XDocument.Load(CompDir + File.Name);

AppendToFile(xmlDoc, aDataRow);

using (var writer = new XmlTextWriter
    (FilePrep.CompletedDirectory + File.Name, new UTF8Encoding(false))) 
    { 
    xmlDoc.Save(writer);
    writer.Close();
    }
break;
附加到文件如下所示

private void AppendToFile(XDocument xmlDoc, DataRow aDataRow)
{
    //String StrName = aDataRow.ItemArray.GetValue(5).ToString() + ' ' + aDataRow.ItemArray.GetValue(6).ToString();

    try
    {
        XElement foundEl = xmlDoc.Descendants("CreditScoreInfo").First();
        foundEl.Add(new XElement("CreditScore", aDataRow.ItemArray.GetValue(2)),
                    new XElement("CreditScoreDt", aDataRow.ItemArray.GetValue(1)));
                    //,new XElement("ScoredName", StrName)); 
    }
    catch
    {
       XElement persPolicy = xmlDoc.Descendants("PersPolicy").First();
                persPolicy.Add(new XElement("CreditScoreInfo",
                    new XElement("CreditScore", aDataRow.ItemArray.GetValue(2)),
                    new XElement("CreditScoreDt", aDataRow.ItemArray.GetValue(1))));
                    //,new XElement("ScoredName", StrName)));
    }
}

你能试试这个作为替代方法吗:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UTF8Encoding(false);

using (XmlWriter writer = XmlWriter.Create(FilePrep.CompletedDirectory + File.Name, settings))
{
    xmlDoc.Save(writer);
    writer.Close();
}

保存XML的代码似乎很好。请确保它是唯一写入XML的代码,无论您使用什么来验证第一个字节是否正确地显示文件(即,我认为VS可能无法在二进制模式下正确重新加载文件)'XmlWriterSettings xws=new XmlWriterSettings();xws.OmitXmlDeclaration=true;xws.Indent=true;xws.Encoding=新的UTF8Encoding(false);'非常感谢你。代码让我开始了,链接给了我获取其余部分的方向。