[英]How to get the specific innertext of my xmlnode in C#?
I have one Xml Node for my processing.The following one is my xml node. 我有一个用于处理的Xml节点。下一个是我的xml节点。
<w:p>
<w:r>
<w:t>
Text1
</w:t>
</w:r>
<w:r>
<w:pict>
<w:p>
<w:r>
<w:t>
text2
</w:t>
</w:r>
</w:p>
</w:pict>
</w:r>
<w:r>
<w:t>
Text3
</w:t>
</w:r>
<w:r>
</w:p>
Now i want to get the inner text from <w:p><w:r><w:t>
only and not from <w:p><w:r><w:pict><w:p><w:r><w:t>
. 现在我只想从<w:p><w:r><w:t>
获取内部文本,而不是从<w:p><w:r><w:pict><w:p><w:r><w:t>
。
So, my required output is Text1Text3
所以,我需要的输出是Text1Text3
My C# code is : 我的C#代码是:
XmlNodeList pNode = xDoc.GetElementsByTagName("w:p");
for (int i = 0; i < pNode.Count; i++)
{
if(i==0) //This is my criteria
{
XmlNode firstNode = pNode[i];
string innerText=firstNode.innerText.toString().Trim();
}
}
But it returns all the inner text like Text1Text2Text3
但是它返回所有内部文本,例如Text1Text2Text3
Please guide me to get out of this issue? 请指导我摆脱这个问题?
您可以使用XPath :(我认为以下方法适用于您)
w:p/w:r/w:t
You need to check for each element p
that none of its anchestors is a pict
element. 您需要检查每个元素p
的锚点都不是pict
元素。
var result = XElement.Load(@"path-to-your-xml")
.Descendants("t")
.Where(e => !e.AnchestorsAndSelf().Any(a => a.Name.LocalName == "pict"));
I recommend you use XDocument
(if you have .NET 3.5 or higher). 我建议您使用XDocument
(如果您具有.NET 3.5或更高版本)。 This code gets values of all elements that have pattern p/r/t
but don't have pict/p/r/t
: 该代码获取所有具有模式p/r/t
但没有pict/p/r/t
元素的值:
// Use this if you're loading XML from a string
XDocument doc = XDocument.Parse(inputString);
// Use this if you're loading XML from a file
//XDocument doc = XDocument.Load(<filepath>);
var pElements = doc.Root
.Descendants()
.Where(el => el.Name.LocalName == "p" && el.Parent.Name.LocalName != "pict");
List<string> innerTexts = new List<string>();
foreach(XElement p in pElements)
{
var rElements = p.Elements().Where(el => el.Name.LocalName == "r");
foreach(XElement r in rElements)
{
var tElements = r.Elements().Where(el => el.Name.LocalName == "t");
innerTexts.AddRange(tElements.Select(el => el.Value).ToArray());
}
}
I used LocalName
since no information on w
namespace was provided. 由于未提供有关w
名称空间的信息,因此我使用了LocalName
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.