【问题标题】:Obtaining InnerText of just the current node with XmlNode使用 XmlNode 获取当前节点的 InnerText
【发布时间】:2012-07-12 22:01:10
【问题描述】:

我有一个 XMLNode,其主体如下所示:(通过 OpenCalais)

    <SocialTag importance="2">Signal processing
<originalValue>Signal processing</originalValue>
</SocialTag>

当我调用XMLMNode.InnerText 时,我会回来:

SignalprocessingSignalprocessing

但是,我只想要标签本身的 InnerText,而不是子“原始值”节点的 InnerText。

当我调用XMLNode.Value 时,它返回null。

如何只获取该节点的 InnerText,而不连接其他子节点的所有 InnerText?

【问题讨论】:

    标签: c# .net xml


    【解决方案1】:

    XmlNode 中的文本实际上是另一个文本类型的XmlNode。这应该有效:

    socialTagNode.ChildNodes[0].Value
    

    【讨论】:

      【解决方案2】:

      来自docsXmlElement.InnerText

      获取或设置节点及其所有子节点的连接值。

      虽然这句话并不完全清楚,但它暗示该属性下降到元素下的 DOM 层次结构,并将所有文本值连接到返回值中——您所看到的行为。

      扩展接受的答案,这里是改编自the reference source 的扩展方法,收集并返回给定节点的所有立即文本子节点:

      public static partial class XmlNodeExtensions
      {
          /// <summary>
          /// Returns all immediate text values of the given node, concatenated into a string
          /// </summary>
          /// <param name="node"></param>
          /// <returns></returns>
          public static string SelfInnerText(this XmlNode node)
          {
              // Adapted from http://referencesource.microsoft.com/#System.Xml/System/Xml/Dom/XmlNode.cs,66df5d2e6b0bf5ae,references
              if (node == null)
                  return null;
              else if (node is XmlProcessingInstruction || node is XmlDeclaration || node is XmlCharacterData)
              {
                  // These are overridden in the reference source.
                  return node.InnerText;
              }
              else
              {
                  var firstChild = node.FirstChild;
                  if (firstChild == null)
                      return string.Empty;
                  else if (firstChild.IsNonCommentText() && firstChild.NextSibling == null)
                      return firstChild.InnerText; // Optimization.
                  var builder = new StringBuilder();
                  for (var child = firstChild; child != null; child = child.NextSibling)
                  {
                      if (child.IsNonCommentText())
                          builder.Append(child.InnerText);
                  }
                  return builder.ToString();
              }
          }
      
          /// <summary>
          /// Enumerates all immediate text values of the given node.
          /// </summary>
          /// <param name="node"></param>
          /// <returns></returns>
          public static IEnumerable<string> SelfInnerTexts(this XmlNode node)
          {
              // Adapted from http://referencesource.microsoft.com/#System.Xml/System/Xml/Dom/XmlNode.cs,66df5d2e6b0bf5ae,references
              if (node == null)
                  yield break;
              else if (node is XmlProcessingInstruction || node is XmlDeclaration || node is XmlCharacterData)
              {
                  // These are overridden in the reference source.
                  yield return node.InnerText;
              }
              else
              {
                  var firstChild = node.FirstChild;
                  for (var child = firstChild; child != null; child = child.NextSibling)
                  {
                      if (child.IsNonCommentText())
                          yield return child.InnerText;
                  }
              }
          }
      
          public static bool IsNonCommentText(this XmlNode node)
          {
              return node != null &&
                  (node.NodeType == XmlNodeType.Text || node.NodeType == XmlNodeType.CDATA
                  || node.NodeType == XmlNodeType.Whitespace || node.NodeType == XmlNodeType.SignificantWhitespace);
          }
      }
      

      然后像这样使用它:

      var value = XMLMNode.SelfInnerText();
      

      示例fiddle

      【讨论】:

        【解决方案3】:

        您可以尝试以下操作,使用 node 您的标签:

        var result="";
        var nodes = node.childNodes
        for (var i=0,len=nodes.length; i<len; i++)
        {
           var node=nodes[i];
           if (node.nodeType==node.TEXT_NODE)
           {
               result += node.nodeValue;
           }
        }
        

        它应该连接主节点内的所有文本节点并忽略子元素

        【讨论】:

          【解决方案4】:

          所以有几点:

          1. InnerText 根据定义,为您提供所有子节点的文本。就 api 为您提供的工具而言,询问“[just] this node 的 InnerText”没有意义。
          2. 您正在寻找的是文本类型的子节点(或者可能是 CDATA,具体取决于您的情况)。大多数(全部?)时间这将是第一个 ChildNode。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-11-28
            • 1970-01-01
            • 2016-02-18
            • 1970-01-01
            • 1970-01-01
            • 2011-01-16
            • 1970-01-01
            • 2011-10-24
            相关资源
            最近更新 更多