简体   繁体   English

将XElement追加到现有XDocument

[英]Appending XElement to existing XDocument

I have the following XML string in database. 我在数据库中有以下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>

I need to add/appened the following element inside 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>

The c# code is given below where i am creating the document 我在创建文档的下面给出了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);

I simply convert the above document to string with the help of the following method and insert it to database as string. 我只是借助以下方法将上述文档转换为字符串,然后将其作为字符串插入数据库。

    public string GetXMLAsString(XDocument myxml)
    {

        StringWriter sw = new StringWriter();
        XmlTextWriter tx = new XmlTextWriter(sw);
        myxml.WriteTo(tx);

        string str = sw.ToString(); 
        return str;
    }

Assuming that you're able to get the XML string back from database and store it in a string variable xml , then you can load it to XDocument as follow : 假设您能够从数据库取回XML字符串并将其存储在字符串变量xml ,则可以按以下方式将其加载到XDocument

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

And assuming that you can construct the new <Service> node as an XElement object referenced by variable newService , you can easily append it to the doc using XElement.Add() method : 并且假设您可以将新的<Service>节点构造为变量newService引用的XElement对象,则可以使用XElement.Add()方法轻松地将其附加到doc

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

Then you need to replace the XML in your database with updated XML : 然后,您需要用更新的XML替换数据库中的XML:

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

You can easily get XML content by calling ToString() on the XDocument object, as shown in above example. 您可以通过在XDocument对象上调用ToString()轻松获得XML内容,如上面的示例所示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM