28. March 2013 20:29 by admin in
.net, linqtoxsd, vs2012, linq Intro
Linq To XSD is a code generation library for accessing xml in a strongly typed manner. Linq to XSD allows you to query and xml file like this:
Offcourse you need a XSD to define the structure of your xml but once you have that the API is much nicer than the general LinqToXML.
Setting it up in Visual Studio
Since it took me way too long and way too much googling to setup LinqToXSD in VS 2012 I thought I'd do
a HOWTO setup for VS2012. So here's the step by step
- Download linqtoxsd from http://linqtoxsd.codeplex.com/ (I used 2.0.2.56002)
- Extract the zip to ($SolutionDir)\LinqToXSD
- Next unload the project in which you want use LinqToXSD (right click -> Unload Project) and edit the project file (right click->edit myproject.csproj)
- Now you have to reference the targets file in the LinqToXSD folder. At the top of the file insert the following line:
<LinqToXsdBinDir Condition="'$(LinqToXsdBinDir)' == ''">$(SolutionDir)\LinqToXSD\</LinqToXsdBinDir>
So it looks like this
5. Finally at the bottom of the project file add
<Import Project="$(LinqToXsdBinDir)\LinqToXsd.targets" />
below <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6. Reload the project
7. If all is well you can now change the build action of xsd files in your project like this:
8. Once this is done code is generated which you should be able use immediately. Your xsd/xml has to obey certain restrictions. I included two sample files which should work out of the box here :
books.xsd (,92 KB)books.xml (,93 KB)
Hope this is of some use and happy easter!