2014-08-29 93 views
0

我有這樣的XML文件中:做這個添加重複元素的XML文件

<root> 
    <Friend_2> 
    <MESSAGE_BODY>hi</MESSAGE_BODY> 
    <MESSAGE_SENDER_ID>1</MESSAGE_SENDER_ID> 
    <MESSAGE_RECEIVER_ID>2</MESSAGE_RECEIVER_ID> 
    <MESSAGE_CREATION_DATE>2014-08-29T15:33:18.1191004+03:00</MESSAGE_CREATION_DATE> 
    </Friend_2> 
</root> 

我使用這個功能,將元素添加到該文件:

private void createNode(string body, string senderid, string receiverid,DateTime creationDate,string path1,string path2) 
{ 
    XDocument doc1 = XDocument.Load(path1); 
    XDocument doc2 = XDocument.Load(path2); 
    XElement root1 = new XElement("Friend_"+receiverid); 
    XElement root2 = new XElement("Friend_"+senderid); 

    root1.Add(new XElement("MESSAGE_BODY", body)); 
    root1.Add(new XElement("MESSAGE_SENDER_ID", senderid)); 
    root1.Add(new XElement("MESSAGE_RECEIVER_ID", receiverid)); 
    root1.Add(new XElement("MESSAGE_CREATION_DATE", creationDate)); 


    root2.Add(new XElement("MESSAGE_BODY", body)); 
    root2.Add(new XElement("MESSAGE_SENDER_ID", senderid)); 
    root2.Add(new XElement("MESSAGE_RECEIVER_ID", receiverid)); 
    root2.Add(new XElement("MESSAGE_CREATION_DATE", creationDate)); 

    if (doc1.Root.Element(root1.Name.LocalName) == null) 
    { 
     doc1.Element("root").Add(root1); 
    } 

    if (doc2.Root.Element(root2.Name.LocalName) == null) 
    { 
     doc2.Element("root").Add(root2); 
    } 

    doc1.Save(path1); 
    doc2.Save(path2); 
} 

但原來的元素被替換。我希望我的XML文件是這樣的:

<root> 
    <Friend_2> 
    <MESSAGE_BODY>hi</MESSAGE_BODY> 
    <MESSAGE_SENDER_ID>1</MESSAGE_SENDER_ID> 
    <MESSAGE_RECEIVER_ID>2</MESSAGE_RECEIVER_ID> 
    <MESSAGE_CREATION_DATE>2014-08-29T15:33:18.1191004+03:00</MESSAGE_CREATION_DATE> 
    <MESSAGE_BODY>how r you ?</MESSAGE_BODY> 
    <MESSAGE_SENDER_ID>1</MESSAGE_SENDER_ID> 
    <MESSAGE_RECEIVER_ID>2</MESSAGE_RECEIVER_ID> 
    <MESSAGE_CREATION_DATE>2014-10-29T15:33:18.1191004+03:00</MESSAGE_CREATION_DATE> 
    </Friend_2> 
</root> 

我的主要理由,這個模式是因爲我使用的解析文檔,並返回一個包含4列和2行

public static DataSet ConvertXMLToDataTable(string xmlString) 
{ 
    DataSet dataset = new DataSet(); 
    dataset.ReadXml(xmlString); 
    return dataset.Tables.Count > 0 ? dataset : null; 
} 

DataSet Chat_ds=Convertor.ConvertXMLToDataTable(path); 
      if (Chat_ds.Tables.Count > 0) 
      { 
       DataTable Chat_dt = Chat_ds.Tables["Friend_" + FriendID]; 

................... 
一個DataTable的功能

我不能讓我的XML看起來像這樣:

<root> 
    <Friend_2> 
    <message> 
    <MESSAGE_BODY>hi</MESSAGE_BODY> 
    <MESSAGE_SENDER_ID>1</MESSAGE_SENDER_ID> 
    <MESSAGE_RECEIVER_ID>2</MESSAGE_RECEIVER_ID> 
    <MESSAGE_CREATION_DATE>2014-08-29T15:33:18.1191004+03:00</MESSAGE_CREATION_DATE> 
    </message> 

    <message> 
    <MESSAGE_BODY>how r you ?</MESSAGE_BODY> 
    <MESSAGE_SENDER_ID>1</MESSAGE_SENDER_ID> 
    <MESSAGE_RECEIVER_ID>2</MESSAGE_RECEIVER_ID> 
    <MESSAGE_CREATION_DATE>2014-10-29T15:33:18.1191004+03:00</MESSAGE_CREATION_DATE> 
    </message> 

    </Friend_2> 
    </root> 
+0

如果我明白,你實際上是重新創建Friend_2節點,所以是的,我希望它的內容被「替換」。 – Kilazur 2014-08-29 13:15:09

+0

我該如何解決這個問題? – Sora 2014-08-29 13:20:31

回答

1

考慮做一些檢查您Friend_n節點的存在。現在,你只需創建一個全新的Friend_n節點,所以舊內容不存在了。

private void createNode(string body, string senderid, string receiverid,DateTime creationDate,string path1,string path2) 
{ 
    XDocument doc1 = XDocument.Load(path1); 
    XDocument doc2 = XDocument.Load(path2); 

    string receiver = "Friend_"+receiverid; 
    string sender = "Friend_"+senderid; 
    XElement root1 = doc1.Element(receiver); 
    if (root1 == null) 
    { 
     root1 = new XElement(receiver); 
     doc1.Root.Add(root1); 
    } 
    XElement root2 = doc1.Element(sender); 
    if (root2 == null) 
    { 
     root2 = new XElement(sender); 
     doc2.Root.Add(root2); 
    } 

    root1.Add(new XElement("MESSAGE_BODY", body)); 
    root1.Add(new XElement("MESSAGE_SENDER_ID", senderid)); 
    root1.Add(new XElement("MESSAGE_RECEIVER_ID", receiverid)); 
    root1.Add(new XElement("MESSAGE_CREATION_DATE", creationDate)); 

    root2.Add(new XElement("MESSAGE_BODY", body)); 
    root2.Add(new XElement("MESSAGE_SENDER_ID", senderid)); 
    root2.Add(new XElement("MESSAGE_RECEIVER_ID", receiverid)); 
    root2.Add(new XElement("MESSAGE_CREATION_DATE", creationDate)); 

    doc1.Save(path1); 
    doc2.Save(path2); 
} 
+0

注意:這主要是由內存完成的,所以它可能無法正常工作。如果是這種情況,我會盡力解決,但重要的部分是開始時的檢查。 – Kilazur 2014-08-29 13:24:39

+0

我有一個例外'此操作將在此行'doc1.Add(目錄root1)創建一個不正確的結構document';' – Sora 2014-08-29 13:29:31

+0

我替換它:'doc1.Root.Add(目錄root1)'和現在一切是確定 – Sora 2014-08-29 13:36:16