2012-07-12 18 views
4

获得只是当前节点的innerText我有一个XmlNode的尸体看起来是这样的:(通过OpenCalais)与XmlNode的

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

当我打电话就可以了XMLMNode.InnerText,我回来:

SignalprocessingSignalprocessing 

但是,我只想从标签本身获取InnerText,而不是孩子'原始值'节点的InnerText。

当我打电话给XMLNode.Value时,它返回null。

我怎样才能得到这个节点的InnerText,而不是连接其他子节点的所有InnerTexts?

+0

请张贴相关的代码 – oleksii 2012-07-12 22:11:39

回答

8

XmlNode内的文字实际上是文字类型的另一个XmlNode。这应该工作:

socialTagNode.ChildNodes[0].Value 
+0

这工作!真棒! – Slaggg 2012-07-12 22:19:55

0

你可以尝试以下方法,用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; 
    } 
} 

应该cncatenate所有textnodes你的主节点内和忽视儿童的元素

0

所以有几件事情:

  1. InnerText顾名思义,给y ou所有子节点的文本。要求“只有这个节点的InnerText”对于api给你的工具是没有意义的。
  2. 你要找的是一个Text类型的子节点(或者可能是CDATA,取决于你的情况)。大多数(所有?)次这将是第一个ChildNode。
1

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