C# 新的utf8编码(错误)仍在写入utf8 BOM
我在代码中关闭了BOM表,但它仍在xml文档中打印它。我不明白。我已经查阅了很多资料,但仍然一无所获,应该从 我的代码是通用的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
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();
}