3

我在我的代码中关闭了 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)));
    }
}
4

1 回答 1

2

你可以试试这个作为另一种方式:

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

using (XmlWriter writer = XmlWriter.Create(FilePrep.CompletedDirectory + File.Name, settings))
{
    xmlDoc.Save(writer);
    writer.Close();
}
于 2012-09-04T23:59:16.400 回答