OOXML-Linq提取带有特定标题的单词表 [英] OOXML - Linq to Extract Word Tables With a Certain Caption
本文介绍了OOXML-Linq提取带有特定标题的单词表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Open XML SDK 2.6尝试&更新Word文档.我可以使用< w:tblCaption>用来识别表格的元素.我的问题是,我需要使用什么Linq查询来获得对整个< w:tbl>的引用.给定具有特定< w:tblCaption>的表格的结构w:val属性?
I'm using the Open XML SDK 2.6 to try & update a Word Document. I can use the <w:tblCaption> element to identify a table. My question is, what Linq query do I need to use to get a reference to entire <w:tbl> structure, given a table with a certain <w:tblCaption> w:val attribute?
DocumentFormat.OpenXml.Packaging.WordprocessingDocument doc = WordprocessingDocument.Open(@"D:\dev\openxml\table.docx", false);
var tables = doc.MainDocumentPart.Document.Descendants<Table>().ToList();
//WHAT LINQ STATEMENT DO I USE?
DOCX TBL XML
<w:tbl>
<w:tblPr>
<w:tblW w:w="4814" w:type="dxa"/>
<w:tblInd w:w="247" w:type="dxa"/>
<w:tblBorders>
<w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:left w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
<w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/>
</w:tblBorders>
<w:tblLayout w:type="fixed"/>
<w:tblLook w:val="0000" w:firstRow="0" w:lastRow="0" w:firstColumn="0" w:lastColumn="0" w:noHBand="0" w:noVBand="0"/>
<w:tblCaption w:val="TBL_TEST"/>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w="1468"/>
<w:gridCol w:w="1444"/>
<w:gridCol w:w="1902"/>
</w:tblGrid>
<w:tr>
</w:tr>
</w:tbl>
推荐答案
您可以尝试执行以下操作:
You can try something like this:
XDocument doc;
XNamespace ns =
XNamespace.Get(@"http://schemas.openxmlformats.org/wordprocessingml/2006/main");
using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true))
{
using (StreamReader sr = new StreamReader(doc.MainDocumentPart.GetStream()))
using (XmlReader xr = XmlReader.Create(sr))
xdoc = XDocument.Load(xr);
XElement table;
//Descendants() gets all children,grandchildren etc.
//First get document -> body ->
XElement tablecapt = xdoc.Elements().First().Elements().First()
.Descendants().Where(d => d.Name == XName.Get("tblCaption", ns)
&& d.Value == "TBL_TEST").FirstOrDefault();
if (tablecapt != null)
table = tablecapt.Parent.Parent;
}
我目前无法对其进行测试,但我认为它应该相距不远.
I can't test it at the moment, but I think it shouldn't be too far off.
这篇关于OOXML-Linq提取带有特定标题的单词表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文