views:

480

answers:

1

Hi,

I'm trying to get the .NET object representation of a certain XSD(element) at runtime.

How can I use Linq to XSD to do that in runtime as opposed to design time?

Thanks, --Ran.

+1  A: 

I use it on a couple of projects.

You have to add it as a target by hand editing your project file. I agree with some of the comments that information is scarce.

first you have to reference the dll in your project references

second you have to 'unload' the project (right click on project file)

then edit the project file (right click again)

add the following to the project file xml

</PropertyGroup> // End of other property groups
<PropertyGroup>
    <TXLINQBinDir Condition="'$(TXLINQBinDir)' == ''">$(ProgramFiles)\LINQ to XSD Preview\Bin</TXLINQBinDir>
</PropertyGroup>
<ItemGroup>
    <Reference Include="Microsoft.Xml.Schema.Linq, Version=0.10.1000.0, Culture=neutral, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\..\..\..\Program Files\LINQ to XSD Preview\Bin\Microsoft.Xml.Schema.Linq.DLL</HintPath>
    </Reference>
<Reference Include="XOTask, Version=0.10.1000.0, Culture=neutral, processorArchitecture=MSIL">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\..\..\..\Program Files\LINQ to XSD Preview\Bin\XOTask.dll</HintPath>
</Reference>
<Reference> // Other references 

<ItemGroup>
// Other item groups
</ItemGroup>
<ItemGroup>
    <LinqToXsdSchema Include="Schema\YourSchema.xsd" />
</ItemGroup>

<Import Project="$(TXLINQBinDir)\LinqToXsd.targets" />

You can probably replace the ....\ nonsense with the $(ProgramFiles) reference but I haven't tested it. Also you can omit the ItemGroup part if you go to the properties of the xsd file, one of the build actions should be "LinqToXsdSchema" (only after setting the LinqToXsd.targets though)

You can now use your xsd file as a fully populated object by using the Parse Method

My Xsd root element is

 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt;
      <xs:element name="AlertData">
      </xs:element>

So in code I can access it like this

// Parse the xml doc as my LinqToXsd object    
AlertData XmlSource = AlertData.Parse(xmlSource.ToString());

// Start accessing xelements
XmlSource.waalert.waa_alert
Mac