使用LINQ to XML从子元素创建对象

时间:2011-02-01 01:50:03

标签: vb.net linq-to-xml

有没有办法使用LINQ to XML来查询下面的XML文档,从后代的子元素创建新的(匿名或强类型)对象?

这是我的XML文档:

<Root>
 <Rules>
  <Rule Name="Rule_A">
   <Parameter>
    <Name>Parameter 1</Parameter>
    <Value>100</Value>
   </Parameter>
   <Parameter>
    <Name>Parameter 2</Parameter>
    <Value>200</Value>
   <Parameter>
  </Rule>
  <Rule Name="Rule_B">
   <Parameter>
    <Name>Parameter 1</Parameter>
    <Value>600</Value>
   </Parameter>
   <Parameter>
    <Name>Parameter 2</Parameter>
    <Value>300</Value>
   <Parameter>
  </Rule>
 </Rules>
</Root>

我的LINQ查询如下所示:

Dim RuleName as String = "Rule_A"
Dim parms() = (From p In pXDoc.Descendants("Rule") _
               Where p.Attributes("Name").Any And p.Attribute("Name").Value.Equals(RuleName) _
               Select New DataParameter With { _
                   '' Here is where I would like to pull the values of the Parameter
                   '' elements underneath "Rule_A" and construct an object like below
                   .Name = LINQ magic to get the value of <Name>
                   .Value = LINQ magic to get the value of <Value>
               }).ToArray

运行该查询后,我的Parms()数组中会有两个对象,表示"Parameter 1""Parameter 2"的名称/值,属于"Rule_A"

我能够得到的最接近的是使用p.Element(Parameter).Element(Name).Value,但这不起作用,因为我只会得到第一个Parameter元素。我也尝试使用Elements(),但无法弄清楚如何获得每个值。

2 个答案:

答案 0 :(得分:1)

你可以这样做:

Dim parms() = (From p In pXDoc...<Rule>              _ '' all "Rule" descendants
               Where p.@Name = RuleName              _ '' "Name" attribute equals RuleName
               From parm In p.<Parameter>            _ '' all "Parameter" elements
               Select New DataParameter With         _
               {                                     _
                   .Name = CStr(parm.<Name>.Value),  _ '' value of "Name" element (as string)
                   .Value = CInt(parm.<Value>.Value) _ '' value of "Value" element (as int)
               }).ToArray

答案 1 :(得分:0)

Sub Main()
    Dim pXDoc As XElement = <Root>
                                <Rules>
                                    <Rule Name="Rule_A">
                                        <Parameter>
                                            <Name>Parameter 1</Name>
                                            <Value>100</Value>
                                        </Parameter>
                                        <Parameter>
                                            <Name>Parameter 2</Name>
                                            <Value>200</Value>
                                        </Parameter>
                                    </Rule>
                                    <Rule Name="Rule_B">
                                        <Parameter>
                                            <Name>Parameter 1</Name>
                                            <Value>600</Value>
                                        </Parameter>
                                        <Parameter>
                                            <Name>Parameter 2</Name>
                                            <Value>300</Value>
                                        </Parameter>
                                    </Rule>
                                </Rules>
                            </Root>



    Dim RuleName As String = "Rule_A"
    Dim parms = (From p In pXDoc.Descendants("Rule") _
                Where p.Attributes("Name").Any _ 
                  And p.Attribute("Name").Value.Equals(RuleName) _
                From parm In p.Elements("Parameter")
                Select New With { _
            .Name = parm.Element("Name").Value,
            .Value = parm.Element("Value").Value
                 })
    For Each x In parms
        Console.WriteLine(x)
    Next

    Console.Write("Enter to Exit: ")
    Console.ReadLine()
End Sub

结果:

{ Name = Parameter 1, Value = 100 }
{ Name = Parameter 2, Value = 200 }
Enter to Exit: