XML隐藏字符,如何清除Bom [英] XML hidden characters, how to remove Bom
问题描述
我想通过代码创建一个XML文件。文件正在成功创建,但我的系统需要的文件只读取UTF-8。该文件未成功加载,因为它们是一些隐藏的字符,如下所示。
I am trying to create an XML file through code. The file is being created successfully but my system needed for the file only reads UTF-8. The file is not being loaded successfully as their are some hidden characters as shown below.
http://imageshack.us/photo/my-images/841/c1u.gif/
此隐藏字符生成错误。我想我需要设置bom一个假,但没有任何想法如何。下面你可以找到我的代码用于生成我的文件。
This hidden characters are generated an error. I am thinking that I need to set the bom a false but dont have any idea how. below you can find my code used for generating my file.
XElement xElement = new XElement("Transaction",
new XElement("TransactionNumber", counter),
new XElement("TransactionReferenceNumber", "CRE" + payItem.ID),
new XElement("TransactionDate", DateTime.Today.Date.ToString("yyyy-MM-dd")),
new XElement("BankID", "99"),
new XElement("SourceAccountNumber", currentAccount.AccountNumber),
new XElement("BeneficiaryName", RemoveSpecialCharacters(payItem.WIRE_BENEF_NAME)),
new XElement("PaymentDetails1", details),
new XElement(checkForAccountDetails(payItem.WIRE_BENEF_BANK_IBAN), getIBANorNumber(payItem)),
new XElement("POCurrency", payItem.Currency),
new XElement("POAmount", payItem.NET_DEPOSIT),
new XElement("BeneficiaryType", "FINANCIAL"),
new XElement("Residence", "NON-RESIDENT"),
new XElement("IncurredChargesBy", "SHARED"),
new XElement("ExchangeControlClassification", "OTHER PAYMENTS"),
new XElement("TypeofPayment", "T3"),
new XElement("SectorCode", "COS"),
new XElement("Priority", getPaymentPriority(payItem.Currency)),
new XElement("SwiftAddress", payItem.WIRE_BENEF_BANK_SWIFT_CDE),
new XElement("BankCountry", currentBank.Country.ToUpper())
);
xDetail.Add(xElement);
基本上我使用C#和XElement类,它通过传递名称标签和数据来创建XML参数。
Basically I am using C# and the XElement class, which creates XML by passing the name tag and data in parameters.
最后,我将所有XElements存储在一个XDocument中,如下所示创建一个XML样式文档并将该文件保存为.XPO
Finally I am storing all the XElements in an XDocument as shown below to created an XML style document and saving the file as .XPO
XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"));
xdoc.Save(pathDesktop + "\\22CRE002.XPO");
推荐答案
如果你想要的是避免创建BOM,这很容易 - 只需创建一个 UTF8Encoding
它不使用一个和 XmlWriterSettings
与该编码:
If all you want is to avoid the creation of a BOM, that's easy - just create a UTF8Encoding
which doesn't use one and an XmlWriterSettings
with that encoding:
var path = Path.Combine(pathDesktop, "\\22CRE002.XPO");
var settings = new XmlWriterSettings {
Encoding = new UTF8Encoding(false),
Indent = true
};
using (var writer = XmlWriter.Create(path, settings))
{
doc.Save(writer);
}
或者只需创建一个合适的 TextWriter
:
Or just create an appropriate TextWriter
:
var path = Path.Combine(pathDesktop, "\\22CRE002.XPO");
using (var writer = new StreamWriter(path, false, new UTF8Encoding(false)))
{
doc.Save(writer);
}
这篇关于XML隐藏字符,如何清除Bom的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!