Title: C# generate class to namespace
Tags: c#;xsd
Question: I am guessing the answer is no, but is there a way to move a generated class to a different namespace?
Background:
I have a XSD file that defines a class, which is turned into a C# class via xsd.exe. By default xsd.exe put the class into the global namespace:
<?xml version="1.0" encoding="utf-8"?&gt;
<xs:schema
elementFormDefault="qualified"
xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
<xs:complexType name="Delays"&gt;
<xs:sequence&gt;
<xs:element name="Delay" minOccurs="0" maxOccurs="unbounded"&gt;
<xs:complexType&gt;
<xs:simpleContent&gt;
<xs:extension base="xs:double"&gt;
<xs:attribute name="function" type="xs:string"/&gt;
</xs:extension&gt;
</xs:simpleContent&gt;
</xs:complexType&gt;
</xs:element&gt;
</xs:sequence&gt;
</xs:complexType&gt;
<xs:element name="Driver"&gt;
<xs:complexType&gt;
<xs:sequence&gt;
<xs:element name="Delays" type="Delays" minOccurs="1" maxOccurs="1"/&gt;
</xs:sequence&gt;
<xs:attribute name="name" type="xs:string"/&gt;
</xs:complexType&gt;
</xs:element&gt;
</xs:schema&gt;
generated class:
//------------------------------------------------------------------------------
// <auto-generated&gt;
//     Dieser Code wurde von einem Tool generiert.
//     Laufzeitversion:4.0.30319.42000
//     Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
//     der Code erneut generiert wird.
// </auto-generated&gt;
//------------------------------------------------------------------------------
using System.Xml.Serialization;
// Dieser Quellcode wurde automatisch generiert von xsd, Version=4.6.1055.0.
/// <remarks/&gt;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Driver {
private DelaysDelay[] delaysField;
private string nameField;
/// <remarks/&gt;
[System.Xml.Serialization.XmlArrayItemAttribute("Delay", IsNullable=false)]
public DelaysDelay[] Delays {
get {
return this.delaysField;
}
set {
this.delaysField = value;
}
/// <remarks/&gt;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
/// <remarks/&gt;
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class DelaysDelay {
private string functionField;
private double valueField;
/// <remarks/&gt;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string function {
get {
return this.functionField;
}
set {
this.functionField = value;
}
/// <remarks/&gt;
[System.Xml.Serialization.XmlTextAttribute()]
public double Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
I would prefer the class to be in a custom namespace. However the generation of the .cs file is not in my control. Editing the generated .cs file is bad practice and the .cs file is also not part of the SCM. The "Driver" class is also extended in a separate file that is stored in the SCM.
So, is there anyway to move the class into a custom namespace without
editing the generated code?
modify the generation?
modify the call to xsd.exe?
Comment: "is there a way to move a (partial) class to a different namespace?" No, a class surely belongs to **one single** namespace. How would you refer to the class if it belongs to different namespaces?

Comment: Did you see https://stackoverflow.com/questions/1843209/xsd-namespace-to-c-sharp-namespace for how to indicate a namespace for the generated classes?

Comment: I updated your question as J. von Langen proposed, because the `partial` leads to confusion here and has nothing to do with your question. It´s only added from xsd.exe in order to add custom behaviour to your classes that can´t be automatically generated.

Comment: You´d have to add some own post-processing on the generated code when you can´t modify how xsd is called. One way to achieve this is using CodeDom and manipulate the code generated by xsd.exe.

Comment: The question is, can he change the namespace of the generated code. The partial is confusing and unnecessary for this question.

Comment: @Damien_The_Unbeliever If you look back at the history, you will see that the question was edited to add the fact that the OP cannot modify the call to xsd.exe. It wasn't in the original question. The answer about changing how generation happens were posted at about the same time as the edit. I answered at around the same time as one of the other posters, but deleted my answer because of this change and because another poster also answered with very similar content a few minutes before me.

Comment: @HimBromBeere - since the question has always stated "However the generation of the .cs file is not in my control", I'm not sure the edited title helps. It also makes me wonder why we're getting answers stating to change how the generation happens.

Comment: @HimBromBeere - those "options" are preceded by essentially "I can't do:"

Comment: They've always had "However the generation of the .cs file is not in my control", which I took to already mean that changing the call to xsd was out of the question. So far as I'm concerned the edit to make that explicit was a *clarification* of something already in the question.

Comment: @J.vanLangen You are right, I modified the title.

Comment: @Damien_The_Unbeliever you are right, that was always part of the initial question, I just added the third list item to make it more clear.


Here is another answer: Okay, I came up with a solution...
I just inherit from the generated class and use MyDriver instead of Driver in my own code:
namespace DriverXMLSerialization
[System.Xml.Serialization.XmlRoot("Driver")]
public class MyDriver : Driver
// foo

Here is another answer: To change the namespace of generated class you need to pass additional parameter to xsd.exe call:
/n[amespace]**:**namespace    Specifies the runtime namespace for the
generated types. The default namespace is Schemas.
After regenerating classes you need to move other partial(s) for this classes to new namespace as well.
Comment for this answer: +1 I posted nearly the exact same thing, but you beat me to it - so  deleted my post. I think the OP's question is quite badly worded
Comment for this answer: Sorry, I forget to add to the list, that the call to xsd.exe is not in my control.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 11, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:11/07/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><xsd>', 'Title': 'C# generate class to namespace', 'ViewCount': 888, 'id': 53186426, 'user_AboutMe': '', 'user_AccountId': 1229253.0, 'user_DisplayName': 'uceumern', 'user_Reputation': 847.0, 'user_Views': 135.0, 'user_id': 1194180.0}}