none
HELP! Using Dynamic LINQ with XML... RRS feed

  • Question

  • i am trying to use dynamic LINQ to query an XML document, as indicated is possible on Scott Guthrie's blog post here:

    http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

    below is my code sample:

    -----------

    XElement pViewElement = XElement.Load(...);

    string sFilter = @"Convert.ToInt32(Attribute("Age").Value) >= 60)";
    List<XElement> pViewResults = pViewElement.Elements().AsQueryable().Where(sFilter).ToList();

    -----------

    however, when this code executes, i receive the following error:

    "No applicable method 'Attribute' exists in type 'XElement'"

    does anyone know why this is? obviously, that method DOES exist when i write the code normally. i cannot find an XML example with dynamic LINQ anywhere. rather, the only good examples are against a SQL backend.

    your help is greatly appreciated in advance.

    Wednesday, April 20, 2011 10:11 PM

Answers

  • Hi Ben,

    Welcome to MSDN forum!

    XElement.Attribute accept XName parameter instead of string.   We can directly pass string into the method since there is an automatic implicit operator convert from string to XName.  Internally the convertion calls XName.Get method.   So when pass the dynamic query string, we need to use XName.Get, like:

    "Convert.ToInt32(Attribute(XName.Get(\"Age\")).Value) >= 60"

    Another important point is that the Dynamic LINQ library declares a read-only Type array named predefinedTypes.  We need to add the type XName and XElement into this array to let the library work in your code.

    static readonly Type[] predefinedTypes = {
                typeof(Object),
                typeof(Boolean),
                typeof(Char),
                typeof(String),
                typeof(SByte),
                typeof(Byte),
                typeof(Int16),
                typeof(UInt16),
                typeof(Int32),
                typeof(UInt32),
                typeof(Int64),
                typeof(UInt64),
                typeof(Single),
                typeof(Double),
                typeof(Decimal),
                typeof(DateTime),
                typeof(TimeSpan),
                typeof(Guid),
                typeof(Math),
                typeof(Convert),
                typeof(XName),
                typeof(XElement)
            };

    If you have any questions, please feel free to let me know.

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    • Marked as answer by Ben Callister Thursday, April 21, 2011 7:08 PM
    Thursday, April 21, 2011 2:46 AM
    Moderator

All replies

  • Hi Ben,

    Welcome to MSDN forum!

    XElement.Attribute accept XName parameter instead of string.   We can directly pass string into the method since there is an automatic implicit operator convert from string to XName.  Internally the convertion calls XName.Get method.   So when pass the dynamic query string, we need to use XName.Get, like:

    "Convert.ToInt32(Attribute(XName.Get(\"Age\")).Value) >= 60"

    Another important point is that the Dynamic LINQ library declares a read-only Type array named predefinedTypes.  We need to add the type XName and XElement into this array to let the library work in your code.

    static readonly Type[] predefinedTypes = {
                typeof(Object),
                typeof(Boolean),
                typeof(Char),
                typeof(String),
                typeof(SByte),
                typeof(Byte),
                typeof(Int16),
                typeof(UInt16),
                typeof(Int32),
                typeof(UInt32),
                typeof(Int64),
                typeof(UInt64),
                typeof(Single),
                typeof(Double),
                typeof(Decimal),
                typeof(DateTime),
                typeof(TimeSpan),
                typeof(Guid),
                typeof(Math),
                typeof(Convert),
                typeof(XName),
                typeof(XElement)
            };

    If you have any questions, please feel free to let me know.

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    • Marked as answer by Ben Callister Thursday, April 21, 2011 7:08 PM
    Thursday, April 21, 2011 2:46 AM
    Moderator
  • that did the trick!

    thanks dude, you rock!

    ben

    Thursday, April 21, 2011 7:08 PM
  • It's my pleasure!  :)

    Have a nice weekend!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Friday, April 22, 2011 1:23 AM
    Moderator
  • Hi Michael, I understand it's been a while since your posted this reply, but I am having similar issue now using  this library (I understand it's evolved from the original one). Any help with this? I posted this question on Stack Overflow too.
    Friday, September 6, 2019 8:37 PM