另一个 LINQ to XML(C# 到 VB.NET 转换)
这些的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这些情况下,最好使用工具,然后修复该工具遗漏的任何内容。 在本例中,我使用 .net 代码转换器 来获得此结果:
同样,代码可能并不完美,但它应该可以帮助您完成 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:
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.
对于第一个,这里有一种用 VB 重写它的方法:
但这与在 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:
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.