将XElement附加到现有XDocument

本文关键字:XDocument XElement | 更新日期: 2023-09-27 18:21:59

我在数据库中有以下XML字符串。

<?xml version="1.0" encoding="utf-16"?>
<ServiceList>
   <Service>
    <COMPAT>2</COMPAT>
    <EQUIPID>0</EQUIPID>
    <TITLE>Collect Call and SMS</TITLE>
    <SMSCOMMAND>0</SMSCOMMAND>
    <DIALCOMMAND>123</DIALCOMMAND>
    <DEACTIVATIONCOMMAND>0</DEACTIVATIONCOMMAND>
    <MODE>Dial</MODE>
    <DETAIL>Here you go.</DETAIL>
    <IMAGE>2014-16-9--16-28-25</IMAGE>
    <LONGDESC>
        <![CDATA[<p>P<br />P<br />P&nbsp;</p>]]>
    </LONGDESC>
    <Mechanism>
        <Title>Mech Title</Title>
        <Description>Here you go.</Description>
        <Trigger>Mech Trigger</Trigger>
        <Controls>1</Controls>
    </Mechanism>
   </Service>
 </ServiceList>

我需要在ServiceList 中添加/添加以下元素

<Service>
 <COMPAT>2</COMPAT>
 <EQUIPID>0</EQUIPID>
 <TITLE>Collect Call and SMS</TITLE>
 <SMSCOMMAND>0</SMSCOMMAND>
 <DIALCOMMAND>123</DIALCOMMAND>
 <DEACTIVATIONCOMMAND>0</DEACTIVATIONCOMMAND>
 <MODE>Dial</MODE>
 <DETAIL>Here you go.</DETAIL>
 <IMAGE>2014-16-9--16-28-25</IMAGE>
 <LONGDESC><![CDATA[<p>P<br />P<br />P&nbsp;</p>]]></LONGDESC>
 <Mechanism>
    <Title>Mech Title</Title>
    <Description>Here you go.</Description>
    <Trigger>Mech Trigger</Trigger>
    <Controls>1</Controls>
 </Mechanism>
</Service>

下面给出了我创建文档的c#代码

            XElement ServiceList =
                new XElement("ServiceList",
                    new XElement("Service",
                        new XElement("COMPAT", "2"),
                        new XElement("EQUIPID", equipId),
                        new XElement("TITLE", form["Title"]),
                        new XElement("SMSCOMMAND", smscommand),
                        new XElement("DIALCOMMAND",dialcommand),
                        new XElement("DEACTIVATIONCOMMAND", smsdecactivationcommand),
                        new XElement("MODE", mode),
                        new XElement("DETAIL", form["Detail"]),
                        new XElement("IMAGE", Datetime),
                        new XElement("LONGDESC", new XCData(htmlstring)),
                        new XElement("Mechanism",
                                new XElement("Title", form["Mechanism.Title"]),
                                new XElement("Description", form["Mechanism.Description"]),
                                new XElement("Trigger", form["Mechanism.Triger"]),
                                new XElement("Controls", form["Mechanism.Controls"])
                            )
                        ));
            XDocument xml = new XDocument(ServiceList);

我只是在以下方法的帮助下将上面的文档转换为字符串,并将其作为字符串插入数据库。

    public string GetXMLAsString(XDocument myxml)
    {
        StringWriter sw = new StringWriter();
        XmlTextWriter tx = new XmlTextWriter(sw);
        myxml.WriteTo(tx);
        string str = sw.ToString(); 
        return str;
    }

将XElement附加到现有XDocument

假设您能够从数据库中取回XML字符串并将其存储在字符串变量xml中,那么您可以将其加载到XDocument,如下所示:

string xml;
.....
XDocument doc = XDocument.Parse(xml);

假设您可以将新的<Service>节点构造为变量newService引用的XElement对象,则可以使用XElement.Add()方法轻松地将其附加到doc

XElement newService;
.....
doc.Root.Add(newService);

然后,您需要用更新的XML:替换数据库中的XML

string updatedXml = doc.ToString();
//save updatedXml to your database

您可以通过在XDocument对象上调用ToString()来轻松获取XML内容,如上例所示。