帮助我改进 OpenXML linq-to-xml 查询!
我猜人们需要一些 Linq-to-xml 方面的经验以及 openXML Word 文档构建的知识。
我有这个 Linq to XML 查询,应该找到内容控件。它在大多数情况下都有效,但我认为它仍然只是临时操纵的而不是正确的。
如果我理解正确的话,它的工作原理是它检查 StdRuns 并查找它的属性是否包含一个名为 Tag 的属性。
问题是内容控制可能不一定是 RUN 的一部分。例如,如果将其添加到一行中的第一个。我不想以后再遇到问题,所以我想知道是否有更好的方法使用 linq 来访问所有内容控件。
这就是 Linq 查询现在的样子:
var cont = from sdt in document.MainDocumentPart.RootElement.Descendants<SdtRun>()
let sdtPr = sdt.GetFirstChild<SdtProperties>()
let tag = (sdtPr == null ? null : sdtPr.GetFirstChild<Tag>())
where tag != null
select new
{
SdtProps = sdtPr,
TagName = tag.GetAttribute("val", sdt.NamespaceUri).Value
};
提前致谢。
Guess people need some experience with Linq-to-xml and knowledge of the build of a openXML word document.
I have this Linq to XML query that's supposed to find Content Controls. It works most of the time but I think it's still just juryrigged and not proper.
How it works, if I understand correctly, is that it checks StdRuns and finds if it's properties include one named Tag.
Problem is that Content Controls are perhaps not necceserily a part of a RUN. For example if it's added first in a line. I don't wan't to hit problems later on so I wonder if there is any better way of hitting all Content Controls using linq.
This is hwo the Linq query is now:
var cont = from sdt in document.MainDocumentPart.RootElement.Descendants<SdtRun>()
let sdtPr = sdt.GetFirstChild<SdtProperties>()
let tag = (sdtPr == null ? null : sdtPr.GetFirstChild<Tag>())
where tag != null
select new
{
SdtProps = sdtPr,
TagName = tag.GetAttribute("val", sdt.NamespaceUri).Value
};
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 Eric White 的博客。他的整个网站非常适合学习使用 Open XML SDK 进行函数式编程。从他的网站:
Check out Eric White's blog. His whole site is really good to learn functional programming with the Open XML SDK. From his site: