locked
data layer with LINQ querying XML documents RRS feed

  • Question

  • Hi,

    i'm working on building data layer having XML documents for storage like :

     

    Code Snippet

    <rowset>

    <item>

    <id>1</id>

    <name>First item</name>

    <price>12.32</price>

    </item>

    <item>

    ...

     

     

     

     

    So in my data layer i want to implement methods : GetAll, GetByName, GetByPrice,...

    so far this is the scenario i used :

    Code Snippet

    class ItemDal
        {
            private static IEnumerable<Item> GetCore()
            {

                XDocument xml = XDocument.Load("dataItems.xml");

                return (    from row in xml.Descendants("item")
                              select new Item
                              {
                                  ItemID = (int)row.Element("id"),
                                  Name = row.Element("name") != null ? (string)row.Element("name") : "",
                                  Price = row.Element("price") != null ? (double?)Double.Parse(row.Element("price").Value) : null
                              }
                        );
            }


            public static List<Item> GetAll()
            {
                return (from row in GetCore()
                        select row
                       ).ToList();
            }

            public static List<Item> GetByName(string name)
            {
                return ( from row in GetCore()
                         where row.Name == name
                         select row
                       ).ToList();
            }
          

            public static List<Item> GetByPrice(double price)
            {
                return ( from row in GetCore()
                         where row.Price == price
                         select row
                       ).ToList();
            }

    }

     

     

    The problem with this is, they all call GetCore(), which loads all records from XML file and after that i use conditional linq query. So how can i reuse the query in the GetCore method for my other methods and apply the filter without having to read all from XML.

     

    p.s. I know it's possible to achieve this with IQueryable<> for querying SQL server...

     

    Tuesday, July 29, 2008 5:43 PM

Answers

  • It seems to me, that the problem is:  XDocument.Load("dataItems.xml");

     

    You need to find some way to load a portion of an xml file.  I don't know of anything in the .net framework that provides this.

    Tuesday, July 29, 2008 5:56 PM

All replies

  • It seems to me, that the problem is:  XDocument.Load("dataItems.xml");

     

    You need to find some way to load a portion of an xml file.  I don't know of anything in the .net framework that provides this.

    Tuesday, July 29, 2008 5:56 PM
  • Hi there

    In my oppinion, the only walkable way is caching the whole "core" thing.
    In addition with a CacheDependency on the file name, you are allways up to date.

    InMemory queries are a few, few, few times faster, then every time access the I/O _and_ parse the whole thing with LinqToXml.

    HTH


    Cheers, Peter
    MVP - Visual Developer ASP / ASP.NET, Switzerland
    Monday, August 10, 2009 7:55 PM
  • Hi there
    You need to find some way to load a portion of an xml file.  I don't know of anything in the .net framework that provides this.

    There is no way and never will be.
    How you want to know which portion to load without parsing the whole thing?

    Logically the only way is holding the whole thing - in the yet current version - in memory and run queries on that.


    Cheers Peter
    MVP - Visual Developer ASP / ASP.NET, Switzerland
    Monday, August 10, 2009 7:58 PM