使用xpath字符串的xml帮助

时间:2011-06-09 19:02:20

标签: c# xml xpath

找到以下代码:

XmlDocument cfgDoc = new XmlDocument();
        loadConfigDoc(cfgDoc);
        // retrieve the appSettings node 
        node = cfgDoc.SelectSingleNode("//ETicketMailboxSettings");

        if (node == null)
        {
            throw new System.InvalidOperationException("appSettings section not found");
        }

        try
        {
            // XPath select setting "add" element that contains this key    
            XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
            if (addElem != null)
            {
                addElem.SetAttribute("value", value);
            }
            // not found, so we need to add the element, key and value
            else
            {
                XmlElement entry = cfgDoc.CreateElement("add");
                entry.SetAttribute("key", key);
                entry.SetAttribute("value", value);
                node.AppendChild(entry);
            }
            //save it
            saveConfigDoc(cfgDoc, docName);
            return true;
        }
        catch
        {
            return false;
        }

双斜线告诉编译器什么?

.SelectSingleNode("//add[@key='"

2 个答案:

答案 0 :(得分:2)

查看XML文档中的任何位置。这是一种XML查询语言。

单个斜杠将从XML文档的根目录开始。

答案 1 :(得分:2)

在整个文档中选择所有节点(在本例中为具有特定属性add的名为key的节点)。

单个斜杠表示“从文档的根目录匹配”。