2012-07-12 37 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

相關問題