【问题标题】:Edit XML Document from WebService Method从 WebService 方法编辑 XML 文档
【发布时间】:2015-04-15 13:42:01
【问题描述】:

我有一个使用 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>

感谢任何帮助,谢谢!

【问题讨论】:

  • xmlUpdateCfg.Save(Context.Request.MapPath("Updates.xml"));

标签: c# xml web-services


【解决方案1】:

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

【讨论】:

  • 我也试过了,但它说我必须等待 6 分钟左右!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多