Query XML source in LinqPad using lambda syntax

1.1k views Asked by At

Considering the following XML data stored in a file xmlData.xml, I am trying to use LinqPad to query it using lambda syntax.

I remember using this before but can't figure it out again. I could query the data using properties as if I was querying an object in Visual Studio.

    <?xml version="1.0"?>
<ArrayOfHROps_User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <HROps_User>
    <EntityKey>
      <EntitySetName>HROps_User</EntitySetName>
      <EntityContainerName>HROperationsEntities</EntityContainerName>
      <EntityKeyValues>
        <EntityKeyMember>
          <Key>UserID</Key>
          <Value xsi:type="xsd:int">44405</Value>
        </EntityKeyMember>
      </EntityKeyValues>
    </EntityKey>
    <UserID>44405</UserID>
    <EmployeeID>AAA40</EmployeeID>
    <Period>2015-06-17T00:00:00</Period>
    <Active>true</Active>
    <Options>false</Options>
    <Pager>false</Pager>
    <Contractor>false</Contractor>
    <TimeStamp>2015-06-18T13:37:38.3</TimeStamp>
    <UserName>Mark.Walsh</UserName>
  </HROps_User>
  <HROps_User>
    <EntityKey>
      <EntitySetName>HROps_User</EntitySetName>
      <EntityContainerName>HROperationsEntities</EntityContainerName>
      <EntityKeyValues>
        <EntityKeyMember>
          <Key>UserID</Key>
          <Value xsi:type="xsd:int">44406</Value>
        </EntityKeyMember>
      </EntityKeyValues>
    </EntityKey>
    <UserID>44406</UserID>
    <EmployeeID>AAA60</EmployeeID>
    <Period>2015-06-17T00:00:00</Period>
    <Active>true</Active>
    <Options>false</Options>
    <Pager>false</Pager>
    <Contractor>false</Contractor>
    <TimeStamp>2015-06-18T13:37:38.94</TimeStamp>
    <UserName>Mark.Walsh</UserName>
  </HROps_User>

In LinqPad - these statements load the data and output it correctly:

var myxml = XElement.Load (@"c:\temp\xmlData.xml");
myxml.Elements().Dump();

I expected the following to work too:

myxml.Elements().FirstOrDefault(x=>x.UserName == "Mark.Walsh").Dump();

but it gives me the error:

'System.Xml.Linq.XElement' does not contain a definition for 'UserName' and no extension method 'UserName' accepting a first argument of type 'System.Xml.Linq.XElement' could be found (press F4 to add a using directive or assembly reference)

Again, I remember having a nice clean syntax before, but I can't figure out exactly how it went. Thanks!

2

There are 2 answers

0
cdsln On BEST ANSWER

In the end I had to:

  • Serialize my list of objects
  • Save it to a .xml file
  • Copy my class definition into LinqPad
  • Load in the XML file
  • Deserialize it to the class type

Then iterate through the list using lambda expressions as I would in VS. A somewhat roundabout way but it was what allowed me to query the results closest to the syntax in VS.

0
har07 On

Probably you meant using Element() method passing element name as parameter :

myxml.Elements()
     .FirstOrDefault(x => (string)x.Element("UserName") == "Mark.Walsh")
     .Dump();

Dotnetfiddle Demo