2012-07-12 22 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