我使用 XML 作为数据库文件开发了一个 WPF 应用程序。昨天,该程序停止工作。经过一番检查,我发现Transaction.xml文件有问题。我尝试在 IE 中打开相同的内容,但出现此错误
XML 页面无法显示
无法使用样式表查看 XML 输入。请更正错误,然后单击“刷新”按钮,或稍后重试。
在文本内容中发现无效字符。错误处理资源'file:///C:/RegisterMaintenance/Transaction.xml
然后,我尝试在记事本中打开文件,它显示了奇怪的字符(下面的屏幕截图)。
最后,它显示了正确的xml结构。请告诉我出了什么问题以及为什么 xml 没有正确显示。怎样才能恢复到正常状态。我真的很担心,因为这是我唯一的数据文件。任何帮助或建议都会很棒。
编辑此文件的代码之一,还有其他类似类型的代码文件使用Transaction.xml
public string Add()
{
XDocument doc1 = XDocument.Load(@"Ledgers.xml");
XElement elem = (from r in doc1.Descendants("Ledger")
where r.Element("Name").Value == this.Buyer
select r).First();
this.TinNo = (string)elem.Element("TinNo");
this.PhoneNo = (string)elem.Element("PhoneNo");
this.CommissionAmount = (this.CommissionRate * this.Amount) / 100;
this.CommissionAmount = Math.Round((decimal)this.CommissionAmount);
this.VatAmount = (this.CommissionAmount + this.Amount) * this.VatRate / 100;
this.VatAmount = Math.Round((decimal)this.VatAmount);
this.InvoiceAmount = this.Amount + this.CommissionAmount + this.VatAmount;
XDocument doc2 = XDocument.Load(@"Transactions.xml");
var record = from r in doc2.Descendants("Transaction")
where (int)r.Element("Serial") == Serial
select r;
foreach (XElement r in record)
{
r.Element("Invoice").Add(new XElement("InvoiceNo", this.InvoiceNo), new XElement("InvoiceDate", this.InvoiceDate),
new XElement("TinNo", this.TinNo), new XElement("PhoneNo", this.PhoneNo), new XElement("TruckNo", this.TruckNo), new XElement("Source", this.Source),
new XElement("Destination", this.Destination), new XElement("InvoiceAmount", this.InvoiceAmount),
new XElement("CommissionRate", this.CommissionRate), new XElement("CommissionAmount", this.CommissionAmount),
new XElement("VatRate", this.VatRate), new XElement("VatAmount", this.VatAmount));
}
doc2.Save(@"Transactions.xml");
return "Invoice Created Successfully";
}