This is a migrated thread and some comments may be shown as answers.

linq to xml using openaccess - googlesitemap creation

4 Answers 133 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
acm2001
Top achievements
Rank 2
acm2001 asked on 19 Jan 2009, 10:58 PM
I am surprised at the small number of articles that cover how to export data from an sql database to an xml file using linq.
I found the site Hooked on Linq which was very useful, but have now come up with an error that i can not get around.

The example on this site uses the format: (in C#)

XElement xml = new XElement("contacts",
                    from c in db.Contacts
                    orderby c.ContactId
                    select new XElement("contact",
                              new XAttribute("contactId", c.ContactId),
                              new XElement("firstName", c.FirstName),
                              new XElement("lastName", c.LastName))
                    );

Here is my code: (in VB)

Dim scope As IObjectScope = BlogLinkVB.ObjectScopeProvider1.GetNewObjectScope()

 Dim xml As XElement = New XElement("urlset", _
                    From p In scope.Extent(Of BlogLinkVB.Category)() _
                    Select New XElement("url", _
                         New XElement("loc", p.Name), _
                         New XElement("lastmod", customdate), _
                         New XElement("changefreq", "daily"), _
                         New XElement("priority", "0.50")))

As you might notice I am trying to create a Google Sitemap from data I have stored in my database, the tags from blogposts.

The error message I am getting is:

Unable to cast object of type 'System.Linq.Expressions.NewArrayExpression' to type 'OpenAccessRuntime.DataObjects.query.Node'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.NewArrayExpression' to type 'OpenAccessRuntime.DataObjects.query.Node'.

Source Error:

Line 48:         'Next
Line 49: 
Line 50:         Dim xml As XElement = New XElement("urlset", _
Line 51:                     From p In scope.Extent(Of BlogLinkVB.Category)() _
Line 52:                     Select New XElement("url", _


Any thoughts would be appreciated. My other calls in Linq are working ok and the scope.Extent() intellisense tooltip confirms that this is an IQueryable instance.

When this is working it might make a very useful example for the OpenAccess demos page. Dynamic creation of xml from databases is very useful as is being able to make a rapid fire sitemap for google or yahoo and keep it easily updated.

Thanks
Alan







4 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 21 Jan 2009, 08:43 AM
Hello Alan,

Unfortunately we do not currently support such queries. This feature is in our TODO list but I am not able to give you an exact time-frame when it will be ready for distribution.
In the meantime you will have to first get the objects from the database and then create a XElement from them:

var result = from c in scope.Extent<LinqToXML.Customer>() 
           select c; 
 
List<XElement> elems = new List<XElement>(); 
 
foreach( var x in result) 
elems.Add(new XElement("contact"new XAttribute("contactId", x.ContactId), 
                                 new XElement("firstName", x.FirstName), 
                                 new XElement("lastName", x.LastName)) ); 
 
XElement xml = new XElement("contacts", elems); 

Hope this helps.

Kind regards,
Alexander
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
acm2001
Top achievements
Rank 2
answered on 21 Jan 2009, 09:27 AM
Thanks. Appreciate the quick response and the workaround.

Great products. Great service.

Would be nice to see a few more demos for using linq and openaccess similar to the asp.net ajax demos


0
Dimitar Kapitanov
Telerik team
answered on 21 Jan 2009, 09:31 AM
Hi acm2001,
We will provide our version of Microsoft's L2S 101 Examples, in C# and VB.NET both. Expect them no later than Q1 2009 release.

Regards,
Dimitar Kapitanov
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
acm2001
Top achievements
Rank 2
answered on 21 Jan 2009, 09:54 AM
You guys are the best.

Here is the code which I have now working in VB based on the original google sitemap concept:

        Dim scope As IObjectScope = BlogLinkVB.ObjectScopeProvider1.GetNewObjectScope()
        Dim result As IObjectScopeQuery(Of BlogLinkVB.Category) = From p In scope.Extent(Of BlogLinkVB.Category)()

        Dim elems As List(Of XElement) = New List(Of XElement)
        Dim localMap = "http://xxxxxxxxxxxxxx.com/category/tag.aspx/"
        Dim customdate As String = String.Format(Date.Now, "yyyy/MM/dd")

        For Each p As Object In result
            elems.Add(New XElement("urlset", New XElement("url", _
                         New XElement("loc", localMap & p.Name), _
                         New XElement("lastmod", customdate), _
                         New XElement("changefreq", "daily"), _
                         New XElement("priority", "0.50"))))
        Next

        Dim xmlDoc As XElement = New XElement("contacts", elems)

Thanks again.
Alan
Tags
LINQ (LINQ specific questions)
Asked by
acm2001
Top achievements
Rank 2
Answers by
Alexander
Telerik team
acm2001
Top achievements
Rank 2
Dimitar Kapitanov
Telerik team
Share this question
or