另一个 LINQ to XML(C# 到 VB.NET 转换)

发布于 2024-07-25 01:35:01 字数 2998 浏览 3 评论 0原文

这些的 VB.NET 语法是什么? 任何帮助转换将不胜感激。

        var defaultStyleName = (string)doc
            .MainDocumentPart
            .StyleDefinitionsPart
            .GetXDocument()
            .Root
            .Elements(w + "style")
            .Where(style =>
                (string)style.Attribute(w + "type") == "paragraph" &&
                (string)style.Attribute(w + "default") == "1")
            .First()
            .Attribute(w + "styleId");

        var q1 = doc
            .MainDocumentPart
            .GetXDocument()
            .Root
            .Element(w + "body")
            .Elements()
            .Select((p, i) =>
            {
                var styleNode = p
                    .Elements(w + "pPr")
                    .Elements(w + "pStyle")
                    .FirstOrDefault();
                var styleName = styleNode != null ?
                    (string)styleNode.Attribute(w + "val") :
                    defaultStyleName;
                return new
                {
                    Element = p,
                    Index = i,
                    StyleName = styleName
                };
            }
            );

        var q2 = q1
            .Select(i =>
            {
                string text = null;
                if (i.Element.Name == w + "p")
                    text = i.Element.Elements()
                        .Where(z => z.Name == r || z.Name == ins)
                        .Descendants(w + "t")
                        .StringConcatenate(element => (string)element);
                else
                    text = i.Element
                        .Descendants(w + "p")
                        .StringConcatenate(p => p
                            .Elements()
                            .Where(z => z.Name == r || z.Name == ins)
                            .Descendants(w + "t")
                            .StringConcatenate(element => (string)element),
                            Environment.NewLine
                        );

                return new
                {
                    Element = i.Element,
                    StyleName = i.StyleName,
                    Index = i.Index,
                    Text = text
                };
            }
            );

        var q3 = q2
            .Select(i =>
                new Commands.MatchInfo
                {
                    ElementNumber = i.Index + 1,
                    Content = i.Text,
                    Style = ContainsAnyStyles(GetAllStyleIdsAndNames(doc, i.StyleName).Distinct(), styleSearchString),
                    Pattern = ContainsAnyContent(i.Text, contentSearchString, regularExpressions, isRegularExpression, caseInsensitive),
                    IgnoreCase = caseInsensitive
                }
            )
            .Where(i => (styleSearchString == null || i.Style != null) && (contentSearchString == null || i.Pattern != null));
        return q3.ToArray();

What is the VB.NET syntax for these? Any help converting would be greatly appreciated.

        var defaultStyleName = (string)doc
            .MainDocumentPart
            .StyleDefinitionsPart
            .GetXDocument()
            .Root
            .Elements(w + "style")
            .Where(style =>
                (string)style.Attribute(w + "type") == "paragraph" &&
                (string)style.Attribute(w + "default") == "1")
            .First()
            .Attribute(w + "styleId");

        var q1 = doc
            .MainDocumentPart
            .GetXDocument()
            .Root
            .Element(w + "body")
            .Elements()
            .Select((p, i) =>
            {
                var styleNode = p
                    .Elements(w + "pPr")
                    .Elements(w + "pStyle")
                    .FirstOrDefault();
                var styleName = styleNode != null ?
                    (string)styleNode.Attribute(w + "val") :
                    defaultStyleName;
                return new
                {
                    Element = p,
                    Index = i,
                    StyleName = styleName
                };
            }
            );

        var q2 = q1
            .Select(i =>
            {
                string text = null;
                if (i.Element.Name == w + "p")
                    text = i.Element.Elements()
                        .Where(z => z.Name == r || z.Name == ins)
                        .Descendants(w + "t")
                        .StringConcatenate(element => (string)element);
                else
                    text = i.Element
                        .Descendants(w + "p")
                        .StringConcatenate(p => p
                            .Elements()
                            .Where(z => z.Name == r || z.Name == ins)
                            .Descendants(w + "t")
                            .StringConcatenate(element => (string)element),
                            Environment.NewLine
                        );

                return new
                {
                    Element = i.Element,
                    StyleName = i.StyleName,
                    Index = i.Index,
                    Text = text
                };
            }
            );

        var q3 = q2
            .Select(i =>
                new Commands.MatchInfo
                {
                    ElementNumber = i.Index + 1,
                    Content = i.Text,
                    Style = ContainsAnyStyles(GetAllStyleIdsAndNames(doc, i.StyleName).Distinct(), styleSearchString),
                    Pattern = ContainsAnyContent(i.Text, contentSearchString, regularExpressions, isRegularExpression, caseInsensitive),
                    IgnoreCase = caseInsensitive
                }
            )
            .Where(i => (styleSearchString == null || i.Style != null) && (contentSearchString == null || i.Pattern != null));
        return q3.ToArray();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

莫言歌 2024-08-01 01:35:02

在这些情况下,最好使用工具,然后修复该工具遗漏的任何内容。 在本例中,我使用 .net 代码转换器 来获得此结果:

Dim defaultStyleName = DirectCast(doc.MainDocumentPart.StyleDefinitionsPart.GetXDocument().Root.Elements(w & "style").Where(Function(style) DirectCast(style.Attribute(w & "type"), String) = "paragraph" AndAlso DirectCast(style.Attribute(w & "default"), String) = "1").First().Attribute(w & "styleId"), String)

Dim q1 = doc.MainDocumentPart.GetXDocument().Root.Element(w & "body").Elements().[Select](Function(p, i) Do
    Dim styleNode = p.Elements(w & "pPr").Elements(w & "pStyle").FirstOrDefault()
    Dim styleName = If(styleNode IsNot Nothing, DirectCast(styleNode.Attribute(w & "val"), String), defaultStyleName)
    Return New ()
End Function)

Dim q2 = q1.[Select](Function(i) Do
    Dim text As String = Nothing
    If i.Element.Name = w & "p" Then
        text = i.Element.Elements().Where(Function(z) z.Name = r OrElse z.Name = ins).Descendants(w & "t").StringConcatenate(Function(element) DirectCast(element, String))
    Else
        text = i.Element.Descendants(w & "p").StringConcatenate(Function(p) p.Elements().Where(Function(z) z.Name = r OrElse z.Name = ins).Descendants(w & "t").StringConcatenate(Function(element) DirectCast(element, String)), Environment.NewLine)
    End If
    
    Return New ()
End Function)

Dim q3 = q2.[Select](Function(i) New Commands.MatchInfo()).Where(Function(i) (styleSearchString Is Nothing OrElse i.Style IsNot Nothing) AndAlso (contentSearchString Is Nothing OrElse i.Pattern IsNot Nothing))
Return q3.ToArray()

同样,代码可能并不完美,但它应该可以帮助您完成 95% 的任务。 编译器应该帮助你完成剩下的事情。

In these cases it is better to use a tool and then fix up whatever the tool missed. In this case I used .net code converter to get this result:

Dim defaultStyleName = DirectCast(doc.MainDocumentPart.StyleDefinitionsPart.GetXDocument().Root.Elements(w & "style").Where(Function(style) DirectCast(style.Attribute(w & "type"), String) = "paragraph" AndAlso DirectCast(style.Attribute(w & "default"), String) = "1").First().Attribute(w & "styleId"), String)

Dim q1 = doc.MainDocumentPart.GetXDocument().Root.Element(w & "body").Elements().[Select](Function(p, i) Do
    Dim styleNode = p.Elements(w & "pPr").Elements(w & "pStyle").FirstOrDefault()
    Dim styleName = If(styleNode IsNot Nothing, DirectCast(styleNode.Attribute(w & "val"), String), defaultStyleName)
    Return New ()
End Function)

Dim q2 = q1.[Select](Function(i) Do
    Dim text As String = Nothing
    If i.Element.Name = w & "p" Then
        text = i.Element.Elements().Where(Function(z) z.Name = r OrElse z.Name = ins).Descendants(w & "t").StringConcatenate(Function(element) DirectCast(element, String))
    Else
        text = i.Element.Descendants(w & "p").StringConcatenate(Function(p) p.Elements().Where(Function(z) z.Name = r OrElse z.Name = ins).Descendants(w & "t").StringConcatenate(Function(element) DirectCast(element, String)), Environment.NewLine)
    End If
    
    Return New ()
End Function)

Dim q3 = q2.[Select](Function(i) New Commands.MatchInfo()).Where(Function(i) (styleSearchString Is Nothing OrElse i.Style IsNot Nothing) AndAlso (contentSearchString Is Nothing OrElse i.Pattern IsNot Nothing))
Return q3.ToArray()

Again, the code is probably not perfect but it should get you 95% of the way there. The compiler should help you with the rest.

青瓷清茶倾城歌 2024-08-01 01:35:02

对于第一个,这里有一种用 VB 重写它的方法:

Dim defaultStyleName = (from style in _
    doc.MainDocumentPart.StyleDefinitionsPart.GetXDocument().Root.Elements(w & "style") _
    where style.Attribute(w & "type").Value = "paragraph" AndAlso _
          style.Attribute(w & "default").Value = "1" _
    select style).First().Attribute(w & "styleId").Value

但这与在 C# 中所做的仍然没有什么不同。

在 VB.NET 中,您还可以使用 @ 表示属性,如 style.@AttributeName 中所示,它为您提供属性的字符串值,但我不确定这如何与您的串联属性一起使用名称。

此外,可以通过 . 语法访问元素,但同样,您的串联名称可能无法使用它。

一般来说,在线转换工具生成的DirectCast调用可以在VB中消除。

有关 VB.NET 如何使用 LINQ to XML 的详细介绍,请查看 贝丝·马西 (Beth Massi) 的《我如何》视频系列

For the first one, here is one way to rewrite it in VB:

Dim defaultStyleName = (from style in _
    doc.MainDocumentPart.StyleDefinitionsPart.GetXDocument().Root.Elements(w & "style") _
    where style.Attribute(w & "type").Value = "paragraph" AndAlso _
          style.Attribute(w & "default").Value = "1" _
    select style).First().Attribute(w & "styleId").Value

But this still isn't all that different from what you'd do in C#.

In VB.NET you can also use the @ for Attributes as in style.@AttributeName, which gives you the string value of the attribute, but I'm not sure how that would work with your concatenated attribute names.

Also, Elements can be accessed via .<elementName> syntax, but again, your concatenated names might not work with that.

In general, the DirectCast calls that are produced by online conversion tools can be eliminated in VB.

For a great introduction to how VB.NET does LINQ to XML, check out the How Do I video series by Beth Massi.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文