通过WCF服务更改XML节点属性值

时间:2013-10-25 17:32:49

标签: c# .net xml wcf xmldocument

[ServiceContract]
public interface IParametersXMLService
{
    [OperationContract, XmlSerializerFormat]
    XmlNode GetCommonNode(string path);

    [OperationContract, XmlSerializerFormat]
    void SetCommonNode(string path, string value);
}

服务实施:

    public XmlNode GetCommonNode(string path)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(path);
        XmlNode node = (XmlNode)xml.DocumentElement;
        XmlNode commonNode = node.SelectSingleNode("/blabla/att);
        return commonNode;
    }

    public void SetCommonNode(string path, string value)
    {
        XmlDocument xml = new XmlDocument();
        xml.Load(path);
        XmlNode node = (XmlNode)xml.DocumentElement;
        XmlNode commonNode = node.SelectSingleNode("/blabla/att");
        commonNode.Attributes["List"].Value = value;
    }

GetCommonNode正常工作并返回我的价值。 SetCommonNode不会改变我的价值。 也许我需要在更改后保存此文件?

1 个答案:

答案 0 :(得分:2)

如上所示,您似乎缺少XML Document保存方法调用 您应该可以通过添加以下内容来解决此问题:

xml.Save(path);

以下链接提供了更多详细信息:
http://support.microsoft.com/kb/301233