开发者

Creating objects from child elements using LINQ to XML

开发者 https://www.devze.com 2023-02-07 15:37 出处:网络
Is there any way to use a LINQ to XML to query an XML document like the one below to create new (anonymous or strongly typed) objects from the child elements of a descendant?

Is there any way to use a LINQ to XML to query an XML document like the one below to create new (anonymous or strongly typed) objects from the child elements of a descendant?

Here is my XML document:

<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>

My LINQ query looks like this:

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

After running that query, my Parms() array would have two objects in it representing the Name/Values of "Parameter 1" and "Parameter 2" that fall under "Rule_A".

The closest I have been able to get is using the p.Element(Parameter).Element(Name).Value, but that won't work as I will only get the first Parameter element. I also tried using Elements(), but was not able to figure out how I would get the value of each.


You can do it like this:

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


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

Result:

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

精彩评论

暂无评论...
验证码 换一张
取 消