Power of LINQ to XML

by dotnetpete 15. December 2008 11:56

I was making some changes to a client's website recently and found static repeated HTML in quite a few places. Their internal developer had put something together than ended up going live!

I decided to create put the HTML in a SQL DB and use LINQ to SQL to retieve the content on demand. All ready to go live and website being hosted somewhere else on their network with slow access to their DB server. No problem, export the content as XML and use LINQ to XML to retrieve.

I ended up getting the content data into a DataTable with a table name of "Content" and then calling the data table's WriteXML method to output as XML. The format was:

<?xml version="1.0" standalone="yes"?>
<DocumentElement>
  <Content>
    <PageName>HOME</PageName>
    <Text>blah</Text>
  </Content>
</DocumentElement>

Then you can write a function that contains your LINQ to XML:

var query = (from c in m_Content.Elements("DocumentElement").Elements("Content")
                        where c.Element("PageName").Value == "HOME"
                        select c).FirstOrDefault();
            
return HttpUtility.HtmlDecode(query.Element("Text").Value);

Very cool! And much easier that typed data sets.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

LINQ

Strongly typed LINQ to XML with LINQ to XSD

by dotnetpete 7. November 2008 11:49

Got stuck into LINQ to XML the other week and I thought wouldn't it be great if this could be strongly typed somehow. Of course the MS XML Team were WAY ahead of me. You can find the article at their blog here.

It's only in preview Alpha 0.2 Refresh at this stage (here) so I'm a little hesitatent to install on my regular machine. What the heck, if you're not living on the edge, you're taking up too much room. Right?

Scott Hanselman also has some interesting thoughts on LINQ to Everything.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

LINQ

LINQPad a great tool for learning and trying out LINQ (to SQL)

by dotnetpete 6. November 2008 09:12

LINQ for us was a huge productivity improvement as we didn't have to generate stored procedures anymore for our CRUD database operations. Because we had a rule that ALL data access was via stored procedures, everytime we had a new processing operation or new search situation, we either had to adapt an exisitng stored procedure or create a new one. Then we had to create a method in our data layer to make the call to the stored procedure.

Now with LINQ we can create a new method in our data layer. Saves time and the generated SQL is not too bad. One thing going against LINQ is that it's very easy to start putting LINQ queries in your UI (aka inline SQL from the old days).

Learning LINQ is a challenge. More specifically doing things that come naturally in TSQL are challenging. I have found it quicker to create a few stored procedures to do some complex queries and call them through the data context.

Never fear LINQPad is here. Very similar to management studio you can connect to servers, run queries against databases. I have found it to be a great tool for learning LINQ.

Happy LINQing.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

LINQ

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

dotnetpete

Hi I'm dotnetpete. I'm an owner and software developer at Advantech Software in Brisbane, Australia. My passions are writing great software and coffee.