将XElement附加到现有XDocument

时间:2014-09-16 11:59:45

标签: c# xml asp.net-mvc-4

我在数据库中有以下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;
    }

1 个答案:

答案 0 :(得分:0)

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

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

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

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

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

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

您可以通过调用XDocument对象上的XElement.Add()轻松获取XML内容,如上例所示。