SGEN gives "Error: The top XML element 'class' from namespace '' references distinct types UserDefinedFunctions.Class and UserDefinedFunctions.Class1."

SGEN gives "Error: The top XML element 'class' from namespace '' references distinct types UserDefinedFunctions.Class and UserDefinedFunctions.Class1."

  • Comments 1

 

Another SGEN error that caused me some time to troubleshoot.

 

In this case customer was again calling a web service from a SQL CLR User Defined Function.

"How to: Create and Run a CLR SQL Server User-Defined Function"

http://msdn.microsoft.com/en-us/library/w2kae45k(VS.80).aspx  

 

If doing this, then you create a reference to the web service and you’ll get a proxy class.

Again, if you are serializing, you need to run SGEN on it. In this case running SGEN caused the following error:

 

C:\Program Files (x86)\Microsoft Visual Studio 8\VC>sgen /f C:<path>\thedll.dll

Microsoft (R) Xml Serialization support utility

[Microsoft (R) .NET Framework, Version 2.0.50727.42]

Copyright (C) Microsoft Corporation. All rights reserved.

Error: The top XML element 'class' from namespace '' references distinct types UserDefinedFunctions.Class and UserDefinedFunctions.Class1.

Use XML attributes to specify another XML name or namespace for the element or types.

 

The reason for this was that the WSDL used to create the proxy had the not set the XmlTypeAttribute for one class, let’s call it “MyClass”,

but it had set the XmlTypeAttribute  to “MyClass” on a class called “MyClass1”.

 

More on this attribute:

"XmlTypeAttribute.TypeName Property"

http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmltypeattribute.typename.aspx

 

An example to show this; create a new SQL Server Project (call it SgenTest) and I selected the database that you want to deploy to.

Then add a new User Defined Function, replace the code to look like this.

 

public partial class UserDefinedFunctions

{

    [XmlTypeAttribute(Namespace = "NameSpace1")]

    public class MyClass

    {

        MyClass() { }

    }

 

    [XmlTypeAttribute(TypeName="MyClass", Namespace = "NameSpace2")]

    public class MyClass1

    {

        MyClass1() { }

    }

};

 

Then build that so you get your .dll (SgenTest.dll) and fire up the “Visual Studio 2008 Command Prompt” and navigate to where the SgenTest.dll is located, run the following:

 

C:\Path>sgen /f SgenTest.dll

 

This should produce:

 

Microsoft (R) Xml Serialization support utility

[Microsoft (R) .NET Framework, Version 2.0.50727.42]

Copyright (C) Microsoft Corporation. All rights reserved.

Error: The top XML element 'class' from namespace '' references distinct types UserDefinedFunctions.Class and UserDefinedFunctions.Class1.

Use XML attributes to specify another XML name or namespace for the element or types.

 

  What happens here when you are running SGEN is that the “MyClass” and “MyClass1” will get the same XML type name (“MyClass”).

  “MyClass” because it has no XmlTypeAttribute set and will use the class name as default and “MyClass1” because the XmlTypeAttribute specifies it to be “MyClass”.

  This means that SGEN tries to create two XML types with the same name, and this can’t be done.

 

So, the solutions.

. Fix the TypeName for “MyClass1”:

. Remove the TypeName for MyClass1 so that is used the default (“MyClass1”)

. Fix both; set the TypeName to “MyClass” for “MyClass” and the TypeName to “MyClass1” for “MyClass1”.

 

For example:

 

public partial class UserDefinedFunctions

{

    [XmlTypeAttribute(TypeName="MyClass", Namespace = "NameSpace1")]

    public class MyClass

    {

        MyClass() { }

    }

 

    [XmlTypeAttribute(TypeName="MyClass1", Namespace = "NameSpace2")]

    public class MyClass1

    {

        MyClass1() { }

    }

};

 

 

HTH

Leave a Comment
  • Please add 3 and 2 and type the answer here:
  • Post
  • Isn't is perfectly OK to have two different classes in two different namespaces?  Why doesn't it use the namespace?  Is that a bug?

Page 1 of 1 (1 items)