【发布时间】:2012-07-13 04:22:24
【问题描述】:
我开发了一个使用 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";
}
【问题讨论】:
-
是
<?xml version="1.0" encoding="utf-8" ?> -
是否有任何编辑器可以打开这个文件,这样就没有奇怪的字符(可能是 UltraEdit)?
-
@Ivan__83 我下载了 ultraedit 并用它打开了文件。它以相同的方式显示:-(
-
假设您的数据通常不是一堆设备和包名称是否安全?如果这与您的应用程序无关并且您的本地驱动器出现问题,我不会感到惊讶。运行 chkdsk。