【问题标题】:Update or inserting a node in an XML doc在 XML 文档中更新或插入节点
【发布时间】:2009-05-14 11:59:51
【问题描述】:

我是 C# 中 XML 和 XPath 的初学者。这是我的 XML 文档的示例:

 <root>
   <folder1>
   ...
   <folderN>
      ...
      <nodeMustExist>...
      <nodeToBeUpdated>some value</nodeToBeUpdated>
  ....
 </root>

如果节点存在,我需要更新 nodeToBeUdpated 的值,或者如果 nodeToBeUpdated 不存在,则在 nodeMustExist 之后添加此节点。函数的原型是这样的:

void UpdateNode(
                  xmlDocument xml, 
                  string nodeMustExist, 
                  string nodeToBeUpdte, 
                  string newVal
               )
{
   /*

   search for XMLNode with name = nodeToBeUpdate in xml 
   to XmlNodeToBeUpdated (XmlNode type?)
   if (xmlNodeToBeUpdated != null)
   {
      xmlNodeToBeUpdated.value(?) = newVal;
   }
   else
   {
      search for nodeMustExist in xml to xmlNodeMustExist obj
      if ( xmlNodeMustExist != null )
      {
          add xmlNodeToBeUpdated as next node
          xmlNodeToBeUpdte.value = newVal;
       }
    }

   */
}

也许还有其他更好、更简化的方法可以做到这一点。有什么建议吗?

顺便说一句,如果nodeToBeUpdated在其他地方出现了不止一次,我只想更新第一个。

【问题讨论】:

  • 我更改了您方法的签名格式以摆脱水平滚动。我讨厌水平滚动。

标签: c# xml xpath


【解决方案1】:

这是更新文件夹中的所有节点:

public void UpdateNodes(XmlDocument doc, string newVal)
        {
            XmlNodeList folderNodes = doc.SelectNodes("folder");

            if (folderNodes.Count > 0)
            foreach (XmlNode folderNode in folderNodes)
            {
                XmlNode updateNode = folderNode.SelectSingleNode("nodeToBeUpdated");
                XmlNode mustExistNode = folderNode.SelectSingleNode("nodeMustExist"); ;
                if (updateNode != null)
                { 
                    updateNode.InnerText = newVal;
                }
                else if (mustExistNode != null)
                {
                    XmlNode node = folderNode.OwnerDocument.CreateNode(XmlNodeType.Element, "nodeToBeUpdated", null);
                    node.InnerText = newVal;
                    folderNode.AppendChild(node);
                }

            }
        }

如果要更新特定节点,则不能传递字符串 nodeToBeUpdte,但必须传递 XmlDocument 的 XmlNode。 我省略了函数中节点名称的传递,因为节点名称不太可能更改并且可以硬编码。但是,您可以将这些传递给函数并使用字符串而不是硬编码的节点名称。

【讨论】:

    【解决方案2】:

    选择&lt;nodeToBeUpdated&gt; 的所有实例的 XPath 表达式如下:

    /root/folder[nodeMustExist]/nodeToBeUpdated

    或者,以更通用的形式:

    /root/文件夹[*[name() = 'nodeMustExist']]/*[name() = 'nodeToBeUpdated']

    适合:

    void UpdateNode(xmlDocument xml, 
                    string nodeMustExist, 
                    string nodeToBeUpdte, 
                    string newVal)
    {
      string xPath = "/root/folder[*[name() = '{0}']]/*[name() = '{1}']";
      xPath = String.Format(xPath, nodeMustExist, nodeToBeUpdte);
    
      foreach (XmlNode n in xml.SelectNodes(xPath))
      {
        n.Value = newVal;
      }
    }
    

    【讨论】:

      【解决方案3】:

      看看SelectSingleNode方法MSDN Doc

      你的 xpath 想要像 "//YourNodeNameHere" ;

      找到该节点后,您可以向后遍历树以到达“nodeMustExist”节点:

      XmlNode nodeMustExistNode = yourNode.Parent["nodeMustExist];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-24
        • 1970-01-01
        • 1970-01-01
        • 2017-03-07
        相关资源
        最近更新 更多