Dynamic LINQ to XML

c# dynamic-linq linq linq-to-xml


Dynamic parameters are giving me difficulties when I attempt to use Flexible LINQ Expressions with XML to LINQ.

Here is an example from the Dynamic LINQ Wiki:

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

For instance, in my scenario, I'm attempting to query the structure shown below:

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

I'm attempting to read and understand it as follows:

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

I need to provide a string parameter. For instance, let's say I wanted to get the IP and OS of every server. When usingElement( ) or Attribute( ) techniques for XElement I get the error "There is no such property or function in XElement.."

Though firstAtteibute and firstNode seems to be open. And it works like this:

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

Accepted Answer

Given that you're usingSystem.Linq.Dynamic.Core , you could alsoXName and XElement so that the zzz-14 zzz will function:

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

Since Dynamic LINQ doesn't recognize explicit conversions and doesn't utilize implicit conversions for method lookup, I demonstrated two distinct approaches to handling conversion ofstring to XName often accomplished through 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);

In the event when the elementOS You must check for absence in order fornull Unfortunately, I have not been successful in obtaining thenp Prerequisite for 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;


Why not simply take their values because it's XML and we need them from the elements/attributes?Value property, in which case dealing with the available kinds is unnecessary.

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