我有一个XMLNode,其正文如下所示:(通过OpenCalais)
<SocialTag importance="2">Signal processing
<originalValue>Signal processing</originalValue>
</SocialTag>
当我打电话给XMLMNode.InnerText
时,我会回来:
SignalprocessingSignalprocessing
但是,我只想要标签本身的InnerText,而不是孩子的原始文本&#39;原始值&#39;节点
当我调用XMLNode.Value
时,它会返回null。
如何在不连接其他子节点的所有InnerTexts的情况下获取此节点的InnerText?
答案 0 :(得分:8)
答案 1 :(得分:1)
来自docs,XmlElement.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。
答案 2 :(得分: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;
}
}
它应该包含主节点内的所有文本节点并忽略子元素
答案 3 :(得分:0)
所以有一些事情: