Dynamic LINQ to XML

c# dynamic-linq linq linq-to-xml


I am trying to use Dynamic LINQ Expressions with LINQ to XML, but having troubles with dynamic parameters.

Here's an example query from Dynamic LINQ Wiki:

var query = db.Customers
    .Where("City == @0 and Orders.Count >= @1", "London", 10)
    .Select("new(CompanyName as Name, Phone)");

In my case for example I am trying to query the following structure:

   <Server IP="">
   <Server IP="">

I am trying to read and parse it like this:

XElement XmlSource = XElement.Load(filePath)
var query = XmlSource.Elements().AsQueryable().Select("???????");

What do I put as a string parameter? For example if I try to retrieve IP and OS of all servers. When I try to use Element( ) or Attribute( ) methods of XElement I get error that "XElement doesn't have such property or method".

Though firstAtteibute and firstNode seems available. And something like this works:

var query = XmlSource.Elements()
         .Select("new (FirstAttribute.Value as IP, FirstNode.toString() as OS)");
9/6/2019 9:15:53 PM

Accepted Answer

Assuming you are using System.Linq.Dynamic.Core, you can add XName and XElement to the accessible types so they will work:

public class MyCustomTypeProvider : DefaultDynamicLinqCustomTypeProvider {
    public override HashSet<Type> GetCustomTypes() => new[] { typeof(XName), typeof(XElement) }.ToHashSet();

Since Dynamic LINQ doesn't use implicit conversions for method lookup, and doesn't understand explicit conversions, I showed two different ways to handle conversion of string to XName normally done by implicit conversion:

ParsingConfig.Default.CustomTypeProvider = new MyCustomTypeProvider();

var OSName = (XName)"OS";
var query = XmlSource.Elements().AsQueryable()
                .Select("new (Attribute(XName.Get(\"IP\")).Value as IP, Element(@0).Value as OS)", OSName);

If it is possible for the element OS to be missing, you must test for null. Unfortunately, I have not been able to get the np predicate to work:

var query = XmlSource.Elements().AsQueryable()
                .Select("new (Attribute(XName.Get(\"IP\")).Value as IP, (Element(@0) != null ? Element(@0).Value : null) as OS)", OSName);
9/7/2019 12:06:22 AM

Popular Answer

    string xml =
            "<DataCenter><Server IP=\"\"><OS>Windows</OS></Server><Server IP=\"\"><OS>Linux</OS></Server></DataCenter>";

    XDocument doc = XDocument.Parse(xml);
    var servers = doc.Elements("DataCenter").Elements("Server");

    var results = from s in doc.Elements("DataCenter").Elements("Server") where (string)s.Attribute("IP") == "" select s;


Since it's XML and we want the values from the elements/attributes why not just grab their Value property. Then there is no reason to deal with the accessible types.

XDocument doc2 = XDocument.Parse(xml);
var results = from s in doc2.Descendants("Server") select new { OsName = s.Element("OS").Value, IpAddress = s.Attribute("IP").Value };

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow