2015-10-15 37 views
0

我想創建XML文件的錯誤,當數據庫不access.when數據庫不能訪問然後我們創建日誌在XML文件。我想當用戶登錄到網站如果數據庫不訪問然後創建log.if數據庫訪問然後XML日誌文件負載和插入我的表中的登錄我database.my代碼:如何創建日誌文件到錯誤?

if (!DatabaseIsConnected()) 
      { 
       BLLLog BLTemp = new BLLLog(); 
       BLTemp.DateTime = DateTime.Now; 
       BLTemp.Event = "DB Not Access"; 
       BLTemp.EventCode = (int)LogValues.DataBaseNotAccess; 
       BLTemp.ID = 0; 
       BLTemp.IpAddress = Core.GetIPAddress(); 
       XmlSerializer serializer = new XmlSerializer(typeof(BLLLog)); 
       StreamWriter xmlError = new StreamWriter(string.Concat(Server.MapPath("/"), "\\Sitemap\\XMLErrors.xml"),true); 
       serializer.Serialize(xmlError,BLTemp); 
       xmlError.Close(); 
       throw new Exception("Db not access...."); 
      } 
     //when loging 
     //write xmlError file to log table 
        StreamReader xmlError = new StreamReader(string.Concat(Server.MapPath("/"), "\\Sitemap\\XMLErrors.xml")); 
        XmlSerializer serializer = new XmlSerializer(typeof(BLLLog)); 
        List<BLLLog> listLog = (List<BLLLog>)serializer.Deserialize(xmlError); 
        xmlError.Close(); 
        HttpContext.Current.Response.Redirect(returnUrl); 

當數據庫沒有幾次xml文件訪問是:

  <BLLLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <ID>0</ID> 
     <DateTime>2015-10-15T12:00:22.0122383+03:30</DateTime> 
     <UserID>0</UserID> 
     <Event>DB Not Access</Event> 
     <EventCode>1000</EventCode> 
     <IpAddress>::1</IpAddress> 
     </BLLLog><?xml version="1.0" encoding="utf-8"?> 
     <BLLLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <ID>0</ID> 
     <DateTime>2015-10-15T12:00:24.1353597+03:30</DateTime> 
     <UserID>0</UserID> 
     <Event>DB Not Access</Event> 
     <EventCode>1000</EventCode> 
     <IpAddress>::1</IpAddress> 
     </BLLLog><?xml version="1.0" encoding="utf-8"?> 
     <BLLLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <ID>0</ID> 
     <DateTime>2015-10-15T12:00:25.8074554+03:30</DateTime> 
     <UserID>0</UserID> 
     <Event>DB Not Access</Event> 
     <EventCode>1000</EventCode> 
     <IpAddress>::1</IpAddress> 
     </BLLLog><?xml version="1.0" encoding="utf-8"?> 
     <BLLLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
     <ID>0</ID> 
     <DateTime>2015-10-15T12:00:26.8995178+03:30</DateTime> 
     <UserID>0</UserID> 
     <Event>DB Not Access</Event> 
     <EventCode>1000</EventCode> 
     <IpAddress>::1</IpAddress> 
     </BLLLog> 

錯誤是「有XML文檔中的錯誤(9,12)「。在Deserialize.how中解決它?

+0

如果是確切的xml文件,你錯過了一個根節點,所以這不是有效的xml。 – Milen

+0

如何解決它? – shahroz

回答

0

添加一個父類,幷包括List<BLLLog>作爲屬性

public class MyErrorLog 
{ 
    public List<BLLLog> Logs{get;set;} 
} 

var BLTemp = new MyErrorLog(); 
BLTemp.Logs = new List<BLLLog>(); 
var log = new BLLLog(); 
log.DateTime = DateTime.Now; 
log.Event = "DB Not Access"; 
log.EventCode = (int)LogValues.DataBaseNotAccess; 
log.ID = 0; 
log.IpAddress = Core.GetIPAddress(); 
BLTemp.Logs.Add(log); 

XmlSerializer serializer = new XmlSerializer(typeof(MyErrorLog)); 
       StreamWriter xmlError = new StreamWriter(string.Concat(Server.MapPath("/"), "\\Sitemap\\XMLErrors.xml"),true); 
       serializer.Serialize(xmlError,BLTemp); 
       xmlError.Close(); 


StreamReader xmlError = new StreamReader(string.Concat(Server.MapPath("/"), "\\Sitemap\\XMLErrors.xml")); 
        XmlSerializer serializer = new XmlSerializer(typeof(MyErrorLog)); 
        MyErrorLog listLog = (MyErrorLog)serializer.Deserialize(xmlError); 
        xmlError.Close(); 

這個頁面有一些有用的建議:Efficient Techniques for Modifying XML Files

+0

錯誤:生成XML文檔 – shahroz

+0

時出錯........................... – shahroz