2012-07-12 57 views
0

我开发了一个使用XML作为数据库文件的WPF应用程序。昨天,该计划停止工作。经过一番检查后,我看到Transaction.xml文件出现问题。我试图在IE中打开相同,但得到这个错误XML数据文件没有打开并正常工作

的XML页面无法显示

使用样式表无法查看XML输入。请更正错误,然后点击刷新按钮,或稍后再试。


在文本内容中发现无效字符。错误处理资源'file:/// C:/RegisterMaintenance/Transaction.xml

然后,我试图打开记事本中的文件,它显示了怪异的字符(屏幕截图如下)。

enter image description here

在最后,其显示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"; 
    } 
+0

什么是文件编码? – 2012-07-12 07:51:18

+0

它是'<?xml version =“1.0”encoding =“utf-8”?>' – 2012-07-12 07:55:45

+0

是否有任何编辑器打开此文件,以便没有奇怪的字符(可能是UltraEdit)? – 2012-07-12 08:17:29

回答

1

C#对象编程东方(OOP)语言,也许你应该使用一些对象!你怎么可能测试你的代码的准确性?

你应该分离出的责任,一个例子:

public class Vat 
{ 
    XElement self; 
    public Vat(XElement parent) 
    { 
     self = parent.Element("Vat"); 
     if (null == self) 
     { 
      parent.Add(self = new XElement("Vat")); 
      // Initialize values 
      Amount = 0; 
      Rate = 0; 
     } 
    } 

    public XElement Element { get { return self; } } 

    public decimal Amount 
    { 
     get { return (decimal)self.Attribute("Amount"); } 
     set 
     { 
      XAttribute a = self.Attribute("Amount"); 
      if (null == a) 
       self.Add(new XAttribute("Amount", value)); 
      else 
       a.Value = value.ToString(); 
     } 
    } 

    public decimal Rate 
    { 
     get { return (decimal)self.Attribute("Rate"); } 
     set 
     { 
      XAttribute a = self.Attribute("Rate"); 
      if (null == a) 
       self.Add(new XAttribute("Rate", value)); 
      else 
       a.Value = value.ToString(); 
     } 
    } 
} 

所有还原数据将在一个节点,它的所有的访问将是一个可测试的类。

你上面的foreach看起来更象:

foreach(XElement r in record) 
{ 
    XElement invoice = r.Add("Invoice"); 
    ... 
    Vat vat = new Vat(invoice); 
    vat.Amount = this.VatAmount; 
    vat.Rate = this.VatRate; 
} 

也就是说可读!从你的代码中,我一眼就看不出发票是否是增值税的父母,但我现在可以!

注意:这并不是说你的代码有问题,它可能是一个硬盘驱动器错误,因为这就是它对我来说。但是如果你想让人们仔细阅读你的代码,就要让它可读和可测试!从现在开始,如果您或其他人不得不更改您的代码,如果它不可读,则无用。

也许从这次事件中你学到了两件事情

  1. 阅读能力和测试能力。
  2. 备份! (我所有的有价值的XML文件是在SVN(TortoiseSVN的),所以我可以比较有什么变化,以及保持良好的备份。该SVN是备份到在线存储。)

理想的下一步是采取代码在属性setter和重构了这一点静态功能扩展,既检验的和可再现:

public static class XAttributeExtensions 
{ 
    public static XAttribute SetAttribute(this XElement self, string name, object value) 
    { 
     // test for correct arguments 
     if (null == self) 
      throw new ArgumentNullException("XElement to SetAttribute method cannot be null!"); 
     if (string.IsNullOrEmpty(name)) 
      throw new ArgumentNullException("Attribute name cannot be null or empty to SetAttribute method!"); 
     if (null == value) // how to handle? 
      value = ""; // or can throw an exception like one of the above. 

     // Now to the good stuff 
     XAttribute a = self.Attribute(name); 
     if (null == a) 
      self.Add(a = new XAttribute(name, value)); 
     else 
      a.Value = value.ToString(); 
     return a; 
    } 
} 

也就是说容易测试,非常具有可读性,最好是可以一遍又一遍使用获得相同的结果!

例如,该量属性可以大大简化用:

public decimal Amount 
{ 
    get { return (decimal)self.Attribute("Amount"); } 
    set { self.SetAttribute("Amount", value); } 
} 

我知道这是一个很大的锅炉板代码,但我觉得可读,可扩展的和最佳的所有测试,能够。如果我想为Vat添加另一个值,我可以修改该类,而不必担心是否将它添加到了正确的位置。如果增值税有孩子,我会再增加一个瓦特有财产的班级。

1

的.XML显然是畸形的其它相似类型的代码文件。没有浏览器或其他程序读取XML文件将能够做任何事情。没关系,xml在一些行之后开始正确。

所以这个错误肯定是它创建和/或编辑你的XML文件。你应该看看那里。也许编码是错误的。最常用的编码是UTF-8。另外,作为一个附注,XML对于大型数据库来说并不是最好的格式(开销太大),所以切换到二进制格式将是最好的。即使切换到JSON也会带来好处。

+0

它是'<?xml version =“1.0”encoding =“utf-8”?>'。现在可以做什么??我能恢复到正常状态 – 2012-07-12 07:57:18

+0

不, t意味着xml语句是一个xml文件的开始(这是btw相当可选的),我的意思是编写xml的程序在第一个地方可能会产生这个搞砸的xml开头 – TheSHEEEP 2012-07-12 10:44:17

+0

I file is read and后端语言是c# – 2012-07-12 12:50:19