2013-02-11 116 views
2

我試圖集合類轉換爲XML數據和XML數據追加到XML文件 這裏是我的功能」如何將XML數據追加到XML文件

public void XmlWriter(List<Email> Femails) 
    { 
     string ErrorXmlFile = @"../../Retries.xml"; 
     try 
     { 
      if (!File.Exists(ErrorXmlFile)) 
      { 
       XmlSerializer xSeriz = new XmlSerializer(typeof(List<Email>)); 
       FileStream fs = File.Open(ErrorXmlFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); 
       xSeriz.Serialize(fs, Femails); 

      } 
      else 
      { 
       foreach(Email email in Femails) 
       { 
       XmlDocument doc = new XmlDocument(); 
       doc.Load(ErrorXmlFile); 
       XmlNode xnode = doc.CreateNode(XmlNodeType.Element, "ArrayOfEmail", null); 
       XmlSerializer xSeriz = new XmlSerializer(typeof(Email)); 
       StringWriter sw = new StringWriter(); 
       xSeriz.Serialize(sw,email); 
       xnode.InnerXml = Convert.ToString(sw); 
       doc.DocumentElement.AppendChild(xnode); 
       doc.Save(ErrorXmlFile); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 

    } 

在上面的函數‘女人 - ’是List的參數,其中Email是userdefind自定義類。 第一次檢查文件是否存在。如果沒有,那麼它現在工作正常,但是當我需要追加數據到我現有的文件失敗。

按照我上面的代碼,它將單個電子郵件對象轉換爲xml數據與<?xml version="1.0" encoding="utf-16"?>與此標籤,我想在綁定之前刪除。

我從我的代碼得到的輸出是:

<?xml version="1.0"?> 
<ArrayOfEmail xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <Email> 
    <UtcDateTime>2013-01-23T10:09:48+05:30</UtcDateTime> 
    <From>Rajendra Bhalekar &lt;[email protected]&gt;</From> 
    <To>Mansha Suman &lt;[email protected]&gt;</To> 
    <Subject>Ye dil sun raha hai jo mere dil ki hai sada ye...socha tha na maine......</Subject> 
    <IDX>32a84ef7-31bf-4366-bcab-ec10b2f39bc6</IDX> 
    <Body> 
Hi Raj, 

Dil pe mat le yaar dil pe mat le.... 
idx:32a84ef7-31bf-4366-bcab-ec10b2f39bc6 

odhani chunariya tere naam ki - Pyar kiya to darna kya 

sona sona telephone dhun pe hasne wali - hidustaniREPLY ABOVE THIS LINEdil 
ibadat kar raha hai 


Thanks &amp; regards, 

Raj 

</Body> 
    <ExtractReplyText>Hi Raj,Dil pe mat le yaar dil pe mat le....idx:32a84ef7-31bf-4366-bcab-ec10b2f39bc6odhani chunariya tere naam ki - Pyar kiya to darna kya</ExtractReplyText> 
    </Email> 
    <ArrayOfEmail> 
    <?xml version="1.0" encoding="utf-16"?> 
<Email xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <UtcDateTime>2013-01-23T10:09:48+05:30</UtcDateTime> 
    <From>Rajendra Bhalekar &lt;[email protected]&gt;</From> 
    <To>Mansha Suman &lt;[email protected]&gt;</To> 
    <Subject>Ye dil sun raha hai jo mere dil ki hai sada ye...socha tha na maine......</Subject> 
    <IDX>32a84ef7-31bf-4366-bcab-ec10b2f39bc6</IDX> 
    <Body> 
Hi Raj, 

Dil pe mat le yaar dil pe mat le.... 
idx:32a84ef7-31bf-4366-bcab-ec10b2f39bc6 

odhani chunariya tere naam ki - Pyar kiya to darna kya 

sona sona telephone dhun pe hasne wali - hidustaniREPLY ABOVE THIS LINEdil 
ibadat kar raha hai 


Thanks &amp; regards, 

Raj 

</Body> 
    <ExtractReplyText>Hi Raj,Dil pe mat le yaar dil pe mat le....idx:32a84ef7-31bf-4366-bcab-ec10b2f39bc6odhani chunariya tere naam ki - Pyar kiya to darna kya</ExtractReplyText> 
</Email></ArrayOfEmail> 
</ArrayOfEmail> 

這是不是因爲我懷孕了。請建議我需要更正。

+0

你好,你的問題是有點不清楚。 「它失敗了」 - 它是如何失敗的,例外? 「不,我期待」 - 你期待什麼?謝謝 – RobJohnson 2013-02-11 12:42:04

+0

它失敗意味着它沒有給出適當的結果有效的XML文件。例如<?xml version =「1.0」encoding =「utf-16」?>它是我的原始xml文檔。當我綁定新的節點到我的XML文檔它的綁定按照給定下面<?xml version =「1.0」encoding =「utf-16」?><?xml version =「1.0」encoding =「utf- ?> Raj 2013-02-12 05:48:59

回答

2

我problam現在是解決這個代碼是爲我工作

public void XmlWriter(List<Email> Femails) 
{ 
    #region On Error Retry and retrycount with xml file 

    string ErrorXmlFile = @"../../Retries.xml"; 
    try 
    { 
     if (!File.Exists(ErrorXmlFile)) 
     { 
      XmlSerializer xSeriz = new XmlSerializer(typeof(List<Email>)); 
      FileStream fs = File.Open(ErrorXmlFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); 
      xSeriz.Serialize(fs, Femails); 
      foreach (Email email in Femails) 
      { 
       SaveAttachment(email); 
      } 

     } 
     else 
     { 
      XmlDocument doc = new XmlDocument(); 
      doc.Load(ErrorXmlFile); 

      foreach (Email email in Femails) 
      { 
       XmlNode xnode = doc.CreateNode(XmlNodeType.Element, "BLACKswastik", null); 
       XmlSerializer xSeriz = new XmlSerializer(typeof(Email)); 
       XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
       ns.Add("", ""); 
       XmlWriterSettings writtersetting = new XmlWriterSettings(); 
       writtersetting.OmitXmlDeclaration = true; 
       StringWriter stringwriter = new StringWriter(); 
       using (XmlWriter xmlwriter = System.Xml.XmlWriter.Create(stringwriter, writtersetting)) 
       { 
        xSeriz.Serialize(xmlwriter, email, ns); 
       } 
       xnode.InnerXml = stringwriter.ToString(); 
       XmlNode bindxnode = xnode.SelectSingleNode("Email"); 
       doc.DocumentElement.AppendChild(bindxnode); 
       SaveAttachment(email); 
      } 
      doc.Save(ErrorXmlFile); 
     } 
    } 
    catch (Exception ex) 
    { 
     throw ex; 
    } 

    #endregion 
}