从WebService方法编辑XML文档

时间:2015-04-15 13:42:02

标签: c# xml web-services

我有一个使用XML文档的Web服务,我正在尝试添加一个向XML文档添加新节点的函数。它运行良好并且不会中断,但保存功能似乎不起作用?这是代码;

[WebMethod]
public void AddNodeTEST()
{
    XmlDocument xmlUpdateCfg = new XmlDocument();

    try
    {
        xmlUpdateCfg.Load(Context.Request.MapPath("Updates.xml"));
    }
    catch (Exception ex)
    {

    }

    XmlNode updateInfo = xmlUpdateCfg.SelectSingleNode("updateinfo");

    /* Create the downloadmodule node */
    XmlNode newDownloadModule = xmlUpdateCfg.CreateNode(XmlNodeType.Element, "downloadmodule", null);
    newDownloadModule.InnerText = "download/test.CAB";
    XmlAttribute downloadModuleName = xmlUpdateCfg.CreateAttribute("name");
    downloadModuleName.Value = "Test";
    newDownloadModule.Attributes.Append(downloadModuleName);

    /* Create the version node */
    XmlNode newVersion = xmlUpdateCfg.CreateNode(XmlNodeType.Element, "version", null);
    XmlAttribute versionMaj = xmlUpdateCfg.CreateAttribute("maj");
    versionMaj.Value = "1";
    XmlAttribute versionMin = xmlUpdateCfg.CreateAttribute("min");
    versionMin.Value = "2";
    XmlAttribute versionBld = xmlUpdateCfg.CreateAttribute("bld");
    versionBld.Value = "3";
    XmlAttribute versionRev = xmlUpdateCfg.CreateAttribute("rev");
    versionRev.Value = "4";
    newVersion.Attributes.Append(versionMaj);
    newVersion.Attributes.Append(versionMin);
    newVersion.Attributes.Append(versionBld);
    newVersion.Attributes.Append(versionRev);

    /* Add the newVersion node to the newDownloadModule node */
    newDownloadModule.AppendChild(newVersion);

    /* Add the newDownloadModule to the updateinfo Node*/
    updateInfo.AppendChild(newDownloadModule);

    xmlUpdateCfg.Save("Updates.xml");
}

这是XML结构;

<?xml version="1.0" encoding="utf-8" ?>
<updateinfo>
  <downloadmodule name="test">
    <version maj="1" min="0" bld="4" rev="0"/>
    Download/cabfile.CAB
  </downloadmodule>
</updateinfo>

感谢任何帮助,谢谢!。

1 个答案:

答案 0 :(得分:0)

你不应该调用xmlUpdateCfg.Save(Context.Request.MapPath(“Updates.xml”)),以便将它保存到它读取的同一位置吗?