Question

I have an XSD containing a simple type like this:

<xs:simpleType name="csharpName" id="csharpName">
    <xs:restriction base="xs:string">
        <xs:pattern value="[A-Za-z][A-Za-z0-9_]*" />
    </xs:restriction>
</xs:simpleType>

Now when I use this type:

 <xs:element name="typeName" type="csharpName" />

LINQ to XSD generates

this.SetElementWithValidation(XName.Get("typeName", ""), value, "typeName", global::.csharpName.TypeDefinition);`

Notice the ::. after global. Now that dot is very wrong there, I assume I'm missing a namespace. Now if I delete the dot manually it's working quite alright, but I'd rather not delete the dozen or so occurrences on every generation. Do you have any ideas?

Was it helpful?

Solution

Ok here's the real correct answer! (I'm using the nuget Linq2Xsd package)

The questioners original solution has a big problem and that is that if you're adding your own namespace to an external XSD just to prevent this bug that when you try to generate XML you'll be sending that external service a made up namespace they can't recognize.

  • Start by removing any made up namespace and targetNamespace you may have added.
  • Then just add a file LINQ-TO-XSD-CONFIG.xsd to your project. It can be anywhere you want.
  • Set the Build Action to LinqToXsdConfiguration
  • NB: The file can be called whatever you want as long as it has this build action.

enter image description here

The contents of this file should be a list of all the namespaces and mappings to Clr types. Remember that every schema in a DLL gets compiled via the same \obj\Debug\LinqToXsdSource.cs file so you need to add each namespace for every XSD you're using in your project.

The key here is the empty namespace where you put a default. This will avoid the ::. problem

<?xml version="1.0" encoding="utf-8" ?>
<Configuration xmlns="http://www.microsoft.com/xml/schema/linq">
  <Namespaces>
    <Namespace Schema="http://example.com/idr" Clr="example.com.idr"/>
    <Namespace Schema="" Clr="LinqXsdGenericNamespace"/>
  </Namespaces>
</Configuration>

See also : http://linqtoxsd.codeplex.com/discussions/238570

OTHER TIPS

I've worked it out, the custom types needed namespaces like this:

<xs:schema
    attributeFormDefault="unqualified"
    elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://codegen"
    xmlns:codegen="http://codegen">

and then use the types with codegen prefix.

Couple things I learnt in the last few HOURS about this :

  • As @TDaver said a namespace is needed to avoid ::. in the generated code. I was hoping to be able to override this somehow with the Custom Tool Namespace property but that didn't see to work. I just had to add a made up namespace to some Amazon XSDs I was using and accept the fact I would need to do this again if the XSD changed.

  • Earlier versions of the Linq2XSD project did not have this issue, something changed - so if you're recompiling using the nuget package where you previously used just a downloaded DLL then you may see this issue where you didn't have it before.

  • Important: ALL the XSD files for a project get compiled to a single LinqToXsdSource.cs file. This is very important to realize since you may be barking up the wrong XSD file looking for an error.

  • You may want to keep an eye on the obj\Debug folder which contains a file LinqToXsdSource.cs. You may want to just blow out this folder if you're having problems, or just keep it open to monitor for errors.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top