帮助我改进 OpenXML linq-to-xml 查询!

发布于 2024-09-13 21:41:08 字数 862 浏览 3 评论 0原文

我猜人们需要一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-09-20 21:41:08

查看 Eric White 的博客。他的整个网站非常适合学习使用 Open XML SDK 进行函数式编程。从他的网站:

private static void IterateContentControlsForPart(OpenXmlPart part)
{
    XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
    XDocument doc = part.GetXDocument();
    foreach (var sdt in doc.Descendants(w + "sdt"))
    {
        Console.WriteLine("Found content control");
        Console.WriteLine("=====================");
        Console.WriteLine(sdt.ToString());
        Console.WriteLine();
    }
}

Check out Eric White's blog. His whole site is really good to learn functional programming with the Open XML SDK. From his site:

private static void IterateContentControlsForPart(OpenXmlPart part)
{
    XNamespace w = "http://schemas.openxmlformats.org/wordprocessingml/2006/main";
    XDocument doc = part.GetXDocument();
    foreach (var sdt in doc.Descendants(w + "sdt"))
    {
        Console.WriteLine("Found content control");
        Console.WriteLine("=====================");
        Console.WriteLine(sdt.ToString());
        Console.WriteLine();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文