我在使用LINQ创建的C#中有这个私有字符串,它返回在XML文件中找到的值。我需要将其转换为VB.Net函数。我用C# converter
尝试了它,但它不起作用。
这是代码。
private string ImportoXML(string PercorsoXML, string ID)
{
XElement xdoc = XElement.Load(PercorsoXML, LoadOptions.PreserveWhitespace);
string ns = xdoc.Name.Namespace.NamespaceName;
var elements = xdoc.Elements(XName.Get("PmtInf", ns))
.Elements(XName.Get("DrctDbtTxInf", ns));
var ElencoValori = from lv2 in elements
select new
{
PmtId = lv2.Element(XName.Get("DrctDbtTx", ns))
.Element(XName.Get("MndtRltdInf", ns))
.Element(XName.Get("MndtId", ns)).Value, InstdAmt
= lv2.Element(XName.Get("InstdAmt", ns)).Value;
};
return ElencoValori.Where(c => c.PmtId.EndsWith(ID)).FirstOrDefault().InstdAmt.ToString();
}
这是我从自动代码转换器获得的VB代码:
Private Function ImportoXML(ByVal PercorsoXML As String, ByVal ID As String) As String
Dim xdoc As XElement = XElement.Load(PercorsoXML, LoadOptions.PreserveWhitespace)
Dim ns As String = xdoc.Name.[Namespace].NamespaceName
Dim elements = xdoc.Elements(XName.[Get]("PmtInf", ns)).Elements(XName.[Get]("DrctDbtTxInf", ns))
Dim ElencoValori = From lv2 In elements
SELECT NEW
{
PmtId = lv2.Element(XName.[Get]("DrctDbtTx", ns)).Element(XName.[Get]("MndtRltdInf", ns)).Element(XName.[Get]("MndtId", ns)).Value,
InstdAmt = lv2.Element(XName.[Get]("InstdAmt", ns)).Value
}
End Select
Return ElencoValori.Where(Function(c) c.PmtId.EndsWith(ID)).FirstOrDefault().InstdAmt.ToString()
End Function
错误在SELECT NEW
:
在选择案例和第一案例
之间,语句和标签无效
答案 0 :(得分:2)
如果没有看到你将其转换为什么,就很难看出错误的位置。
以下是我为您手动转换的内容:
Private Function ImportXML(PercorsoXML As String, ID As String) As String
Dim xdoc As XElement = XElement.Load(PercorsoXML, LoadOptions.PreserveWhitespace)
Dim ns As String = xdoc.Name.Namespace.NamespaceName
Dim elements = xdoc.Elements(XName.Get("PmtInf", ns)).Elements(XName.Get("DrctDbtTxInf"))
Dim ElencoValori = From lv2 In elements
Select PmtId = lv2.Element(XName.Get("DrctDbtTx", ns)) _
.Element(XName.Get("MndtRltdInf", ns)) _
.Element(XName.Get("MndtId", ns)).Value, InstdAmt _
= lv2.Element(XName.Get("InstdAmt", ns)).Value
Return ElencoValori.Where(Function(c) c.PmtId.EndsWith(ID)).FirstOrDefault().InstdAmt.ToString()
End Function
这对我来说很好,所以试一试。
答案 1 :(得分:1)
再一次,我只在这里发布VB等价物,因为在线转换器不会开始处理这类代码,所以鉴于此,这是一个合理的问题:
Option Infer On
Private Function ImportoXML(ByVal PercorsoXML As String, ByVal ID As String) As String
Dim xdoc As XElement = XElement.Load(PercorsoXML, LoadOptions.PreserveWhitespace)
Dim ns As String = xdoc.Name.Namespace.NamespaceName
Dim elements = xdoc.Elements(XName.Get("PmtInf", ns)).Elements(XName.Get("DrctDbtTxInf", ns))
Dim ElencoValori = From lv2 In elements
Select New With {
Key .PmtId = lv2.Element(XName.Get("DrctDbtTx", ns)).Element(XName.Get("MndtRltdInf", ns)).Element(XName.Get("MndtId", ns)).Value,
Key .InstdAmt = lv2.Element(XName.Get("InstdAmt", ns)).Value;
}
Return ElencoValori.Where(Function(c) c.PmtId.EndsWith(ID)).FirstOrDefault().InstdAmt.ToString()
End Function