在XML文件中添加重复元素

时间:2014-08-29 13:04:20

标签: c# asp.net 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行的dataTable的函数

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];

...................

我的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>

1 个答案:

答案 0 :(得分: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);
}