本文介绍了使用打开的xml检索基于内容的段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我正在使用c#4.0并打开xml sdk 2.0来访问Word文件.为此,现在我想根据给定的文本检索一个段落.如果该段落包含我的文本,则检索包含该文本的段落...
例如:
给定的单词是:TEST
检索包含单词"TEST"的段落
该怎么做...?
请帮忙得到这个...
感谢$问候,
P.SARAVANAN
Hi
I am using c#4.0 and open xml sdk 2.0 for accessing Word file.For that, Now i want to Retrieve a paragraph based on the given text.If the paragraph contains my text then retrieve the paragraph containing that text...
FOR EXAMPLE:
Given Word is: TEST
Retrieve the paragraphs that containing the word "TEST"
How to do that...?
Please help to get this...
thanks $ regards,
P.SARAVANAN
推荐答案
static void Main(string[] args)
{
byte[] docByteArray = File.ReadAllBytes("Test.docx");
using (MemoryStream memoryStream = new MemoryStream())
{
memoryStream.Write(docByteArray, 0, docByteArray.Length);
using (WordprocessingDocument doc =
WordprocessingDocument.Open(memoryStream, true))
{
RevisionAccepter.AcceptRevisions(doc);
XElement root = doc.MainDocumentPart.GetXDocument().Root;
XElement body = root.LogicalChildrenContent().First();
foreach (XElement blockLevelContentElement in body.LogicalChildrenContent())
{
if (blockLevelContentElement.Name == W.p)
{
var text = blockLevelContentElement
.LogicalChildrenContent()
.Where(e => e.Name == W.r)
.LogicalChildrenContent()
.Where(e => e.Name == W.t)
.Select(t => (string)t)
.StringConcatenate();
Console.WriteLine("Paragraph text >{0}<", text);
continue;
}
// If element is not a paragraph, it must be a table.
Console.WriteLine("Table");
}
}
}
}
这篇关于使用打开的xml检索基于内容的段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!