1

我有这个 XML,

<MM>
    <Bank>
        <Result>
            <url>http://192.168.1.12/pay/api/abc</url>
            <param name="type">FUND</param>
            <param name="tid">175219</param>
            <param name="ticket">DiZfWMQxL5Qfasfsdfsdfsqweqwe</param>
            <param name="stage">1</param>
        <Result>
    <Bank>
</MM>

我知道如何获取url元素的值。

Dim qryurl = From c In doc.Descendants("Result") _
             Select c.Element("url").Value

如何param从 XML 中指定标签值?

4

2 回答 2

0

我会考虑这样做:

Dim getParam As Func(Of XElement, String, String) =
    Function (e, t)
        Return e _
            .Elements("param") _
            .Where(Function (x) x.Attribute("name") = t) _
            .Select(Function (x) x.Value) _
            .FirstOrDefault()
    End Function

Dim Query = _
    From c In doc.Descendants("Result") _
    Select New With 
    { 
        .URL = c.Element("url").Value,
        .type = getParam(c, "type"),
        .tid = getParam(c, "tid"),
        .ticket = getParam(c, "ticket"),
        .stage = getParam(c, "stage")
    }

您当然可以将getParam函数直接扩展到查询中,但这会使它变得很长。使用Func肯定会缩短它。

给定上述代码和您的示例 XML,我的结果是:

查询结果

于 2012-12-07T11:10:35.133 回答
0
from x in c.Elements("param")
select new {
               type = x.Select(y => y.Attribute("name").Value == "type").Value,
               tid = x.Select(y => y.Attribute("name").Value == "tid").Value,
               ticket = x.Select(y => y.Attribute("name").Value == "ticket").Value,
               stage = x.Select(y => y.Attribute("name").Value == "stage").Value,

           }

可能存在一些语法错误。但这是获取价值的逻辑。例如,您可以在此处查看有关此主题的信息。

于 2012-12-07T09:28:37.163 回答