views:

972

answers:

2

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();
A: 

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.

Andrew Hare
Hi Andrew, thanks for the reply. Great minds think alike, unfortunately the .net code conversion services do not get close enough and do not allow me to "learn" the vb.net side of linq-to-xml and time is of the essence in converting this one.
I am confused, if time is of the essence why are you concerned about learning how to convert the code?
Andrew Hare
Because the client insists on VB only. I wish I had a better answer for you than that, lol!
A: 

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.

Dennis Palmer
Excellent video series it gave me a better idea of how its done. Now of course I'm running into a problem with this specific chunk in the second Dim where it goes .Select((p, i) =>{ var styleNode = pi understand to write the first part of this as .Select(Function(p, i) but what does the bitwise operator become in this case?
Dennis Palmer