2014-01-23 54 views
3

我有一個程序可以創建XML日誌。首先,我檢查,如果XML存在,如果不是我用下面的代碼來創建它:將新行添加到XML文件

XmlWriterSettings settings = new XmlWriterSettings(); 
settings.Indent = true; 

using (XmlWriter writer = XmlWriter.Create(myXMLLog, settings)) 
        { 
        writer.WriteStartDocument(); 
        writer.WriteComment("Comment here."); 
        writer.WriteStartElement("Batch"); 
        writer.WriteAttributeString("ID", BatchID); 
        writer.WriteAttributeString("Date", now); 
        writer.WriteStartElement("Step"); 
        writer.WriteElementString("Name", step); 
        writer.WriteElementString("Success", success); 
        writer.WriteElementString("Message", message); 
        writer.WriteStartElement("Transaction"); 
        writer.WriteElementString("ID", transID); 
        writer.WriteElementString("Details", details); 

        writer.WriteEndElement(); 
        writer.WriteEndDocument(); 
       } 

可正常工作,並創建文件:

<!--This log was created by the Application.--> 
<Batch Date="1/22/2014 10:01:11 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d59a93"> 
<Step> 
    <Name>Start Batch</Name> 
    <Success>1</Success> 
    <Message>Processing Batch</Message> 
<Transaction> 
    <ID>N/A</ID> 
    <Details>N/A</Details> 
</Transaction> 
</Step> 

所以第二次檢查XML是否存在,它確實存在,所以不要創建它,而應該添加它。我使用下面的代碼是:

   XDocument xmlDoc = XDocument.Load(myXMLLog); 
       XElement root = new XElement("Batch"); 

       root.Add(new XAttribute("ID", BatchID)); 
       root.Add(new XAttribute("Date", now)); 
       root.Add(new XElement("Step")); 
       root.Add(new XAttribute("Name", step)); 
       root.Add(new XAttribute("Success", success)); 
       root.Add(new XAttribute("Message", message)); 
       root.Add(new XElement("Transaction")); 
       root.Add(new XAttribute("ID", transID)); 
       root.Add(new XAttribute("Details", details)); 

       xmlDoc.Element("Batch").Add(root); 
       xmlDoc.Save(myXMLLog); 

所以現在我期待的XML文件看起來像這樣:

<!--This log was created by the Application.--> 
<Batch Date="1/22/2014 10:01:11 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d59a93"> 
<Step> 
    <Name>Start Batch</Name> 
    <Success>1</Success> 
    <Message>Processing Batch</Message> 
<Transaction> 
    <ID>N/A</ID> 
    <Details>N/A</Details> 
</Transaction> 
</Step> 
</Batch> 
<Batch Date="1/22/2014 10:20:00 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d5aaa"> 
<Step> 
    <Name>Start Batch</Name> 
    <Success>1</Success> 
    <Message>Processing Batch</Message> 
<Transaction> 
    <ID>N/A</ID> 
    <Details>N/A</Details> 
</Transaction> 
</Step> 
</Batch> 

而是我得到一個異常「的重複屬性」。

我在做什麼錯?

謝謝!

編輯:

XML文檔確實有在它的頂部。有效的XML會非常好,但不是最重要的。

編輯編輯:

這是我的代碼現在的樣子:現在

  if (File.Exists(myXMLLog)) 
      {    
       XDocument xmlDoc = XDocument.Load(myXMLLog); 
       XElement root = new XElement("Batch", 
        new XAttribute("ID", BatchID), 
        new XAttribute("Date", now), 
        new XElement("Step", 
         new XElement("Name", step), 
         new XElement("Success", success), 
         new XElement("Message", message), 
         new XElement("Transaction", 
          new XAttribute("IDTrans", transID), 
          new XAttribute("Details", details)))); 
       xmlDoc.Root.Add(root); 
       xmlDoc.Save(myXMLLog); 
      } 
      else 
      { 
       using (XmlWriter writer = XmlWriter.Create(myXMLLog, settings)) 
       { 
        writer.WriteStartDocument(); 

        writer.WriteComment("This log was created by the Application."); 
        writer.WriteStartElement("Batch"); 
        writer.WriteAttributeString("ID", BatchID); 
        writer.WriteAttributeString("Date", now); 
        writer.WriteStartElement("Step"); 
        writer.WriteElementString("Name", step); 
        writer.WriteElementString("Success", success); 
        writer.WriteElementString("Message", message); 
        writer.WriteStartElement("Transaction"); 
        writer.WriteElementString("IDTrans", transID); 
        writer.WriteElementString("Details", details); 

        writer.WriteEndElement(); 
        writer.WriteEndDocument(); 
       } 
      } 

我確定與XML的格式,我的問題是,在做內部的批首批

<Batch Date="1/22/2014 11:49:17 PM" ID="6966578b-b326-4f16-a315-1b4228a9fa42"> 
<Step><Name>Create DataTable</Name> 
<Success>1</Success> 
<Message>DataTable was Created and Populated</Message> 
<Transaction> 
<IDTrans>N/A</IDTrans> 
<Details>0</Details> 
</Transaction> 
</Step> 
<Batch Date="1/22/2014 11:49:17 PM" ID="6966578b-b326-4f16-a315-1b4228a9fa42"> 
<Step> 
<Name>Send Email</Name> 
<Success>1</Success> 
<Message>Template Seleted and Filled</Message> 
<Transaction Details="Email To: [email protected] Link: " IDTrans="243b0a3c-d8b7-49c3-b1d0-asdfsdsdfsdf"/> 
</Step> 
</Batch>..... 

編輯FINAL

工作守則,所有的答案的組合:

  if (File.Exists(myXMLLog)) 
      {    
       XDocument xmlDoc = XDocument.Load(myXMLLog); 
       XElement root = new XElement("Batch", 
        new XAttribute("ID", BatchID), 
        new XAttribute("Date", now), 
        new XElement("Step", 
         new XElement("Name", step), 
         new XElement("Success", success), 
         new XElement("Message", message), 
         new XElement("Transaction", 
          new XAttribute("IDTrans", transID), 
          new XAttribute("Details", details)))); 
       xmlDoc.Root.Add(root); 
       xmlDoc.Save(myXMLLog); 
      } 
      else 
      { 
       using (XmlWriter writer = XmlWriter.Create(myXMLLog, settings)) 
       { 
        writer.WriteStartDocument(); 
        writer.WriteComment("This log was created by the Application."); 
        writer.WriteStartElement("Root"); 
        writer.WriteStartElement("Batch"); 
        writer.WriteAttributeString("ID", BatchID); 
        writer.WriteAttributeString("Date", now); 
        writer.WriteStartElement("Step"); 
        writer.WriteElementString("Name", step); 
        writer.WriteElementString("Success", success); 
        writer.WriteElementString("Message", message); 
        writer.WriteStartElement("Transaction"); 
        writer.WriteElementString("IDTrans", transID); 
        writer.WriteElementString("Details", details); 
        writer.WriteEndElement(); 
        writer.Flush(); 
        writer.WriteEndDocument(); 
       } 
      } 

我很滿意的結果。如果任何人有關於如何更好地格式化XML的建議,請這樣做。感謝大家。

+0

旁註:你想製造出任何不XML,它是XML片段的集合(因爲它缺乏袋鼠t節點)。它不能用默認的XML閱讀器讀取,但如果需要的話,你可以配置.Net'XmlReader'來期望「XML片段」而不是文檔。一些記錄器使用的類似附件友好的XML片段格式... –

+0

考慮更新您的問題,無論您想要的是有效的XML還是您當前詢問的格式。 –

回答

3

您當前的代碼將所有這些元素和屬性,而不是增加一些屬性,新創建的元素root, 。

元素創作應該看起來像:

XElement root = new XElement("Batch", 
    new XAttribute("ID", BatchID), 
    new XAttribute("Date", now), 
    new XElement("Step", 
     new XElement("Name", step), 
     new XElement("Success", success), 
     new XElement("Message", message), 
     new XElement("Transaction", 
      new XElement("ID", transID), 
      new XElement("Details", details)))); 
+2

我認爲名字,成功和消息應該是XElement –

+0

@ Selman22你是對的。我確定了我的答案。 – MarcinJuraszek

2

您試圖將兩個ID屬性添加到同一個批處理元素。

root.Add(new XAttribute("ID", BatchID)); 
    root.Add(new XAttribute("ID", transID)); 

我想你需要你的Transaction元件內的IDDetails元件,以便做到這一點,當你將你的交易要素:

root.Add(new XElement("Transaction", 
      new XElement("ID", transID), 
      new XElement("Details",details)); 

你也可以做所有這些在一個聲明:

XElement root = new XElement("Batch", 
      new XAttribute("ID", BatchID), 
      new XAttribute("Date", now), 
      new XElement("Step", 
       new XElement("Success", success), 
       new XElement("Message", message), 
       new XElement("Transaction", 
        new XElement("ID", transID), 
        new XElement("Details", details)))); 

注意:由於MarcinJuraszek提到了他的答案,所以將所有屬性直接添加到根元素中。For例如:

root.Add(new XElement("Step")); 
root.Add(new XAttribute("Name", step)); 
root.Add(new XAttribute("Success", success)); 

代碼不添加NameSuccess屬性您Step元素,它增加了這兩個屬性,你的根element.To解決這個問題,你需要將這些屬性添加到您的Step元素,當你創造它:

XElement stepElement = new XElement("Step", 
          new XAttribute("Name", step), 
          new XAttribute("Success", success)); 

然後,您可以將此元素添加到根:

root.Add(stepElement); 

和鰭人注意你應該使用XElement添加新元素,根據您的XML文檔,Name,Success,Message,Id (transaction)Details應該是XElementXAttribute.

1

首先,你有兩個「ID」的屬性,請刪除其中一方:

root.Add(new XAttribute("ID", BatchID)); 
…………………… 
root.Add(new XAttribute("ID", transID)); 

然後,你不能這樣做,因爲你的XML的缺乏頭根。我的意思是你應該加入一個「根」改變你的xml文件像這樣爲你的XML的根頭

<!--This log was created by the BaswareVendorRegistrationCA Application.--> 
<Root> 
    <Batch Date="1/22/2014 10:01:11 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d59a93"> 
    <Step> 
     <Name>Start Batch</Name> 
     <Success>1</Success> 
     <Message>Processing Batch</Message> 
     <Transaction> 
     <ID>N/A</ID> 
     <Details>N/A</Details> 
     </Transaction> 
    </Step> 
    </Batch> 
</Root> 

然後做附加:

XDocument xmlDoc = XDocument.Load("XMLFile1.xml"); 
      XElement root = new XElement("Batch"); 

      root.Add(new XAttribute("ID", 1)); 
      root.Add(new XAttribute("Date", 2)); 
      root.Add(new XElement("Step")); 
      root.Add(new XAttribute("Name", 3)); 
      root.Add(new XAttribute("Success", 4)); 
      root.Add(new XAttribute("Message", 5)); 
      root.Add(new XElement("Transaction")); 
      root.Add(new XAttribute("Details", 7)); 

      xmlDoc.Root.Add(root); 
      xmlDoc.Save("c:\\try.xml"); 

PS:如果你的XML不有一個根元素,VS會告訴你這是一個無效的XML:

enter image description here