简体   繁体   中英

How to work with different.xsd namespaces that contains the same elements/classes?

Im having a little trouble understanding how I should work with xml files, so I hope you guys can guide me in the right dirrection :) Hopefully I can explain my problem clear enough :)

I have a lot of .xsd files which are all connected from top to bottom. So I have 10 .xsd with namespace A and 10 .xsd with namespace B. Lets say that the two namespaces represents each own car. That means they both share a lot of the same elements, like engine, wheel ect.. I tought that I could make use of xsd.exe and then just serialize the xml files them in my C# code. But when I have converted the .xsd files into two .cs files (one for each namespace/car), they share a lot of the same classes. That makes a problem when I want to add the two .cs files to my project. Cant have two classes with the same name... How do I solve this? Am I using the wrong tools or have I completely misunderstood what I should do? :)

The beginning of the .cs file:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.261
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]          [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://rep.oio.dk/sundcom.dk/medcom.dk/xml/schemas/2006/07/01/")]
[System.Xml.Serialization.XmlRootAttribute("FixedFont",     Namespace="http://rep.oio.dk/sundcom.dk/medcom.dk/xml/schemas/2006/07/01/", IsNullable=false)]
public partial class SimpleFormattedText {

private object[] itemsField;

private ItemsChoiceType[] itemsElementNameField;

private string[] textField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Bold", typeof(BreakableText))]
[System.Xml.Serialization.XmlElementAttribute("Break", typeof(Break))]
[System.Xml.Serialization.XmlElementAttribute("Center", typeof(BreakableText))]
[System.Xml.Serialization.XmlElementAttribute("Italic", typeof(BreakableText))]
[System.Xml.Serialization.XmlElementAttribute("Right", typeof(BreakableText))]
[System.Xml.Serialization.XmlElementAttribute("Space", typeof(Space))]
[System.Xml.Serialization.XmlElementAttribute("Underline", typeof(BreakableText))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public object[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemsChoiceType[] ItemsElementName {
    get {
        return this.itemsElementNameField;
    }
    set {
        this.itemsElementNameField = value;
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text {
    get {
        return this.textField;
    }
    set {
        this.textField = value;
    }
}

}

The best way might prove to feed all the XSD files to xsd.exe at the same time. Just to illustrate, assuming you have three XSD files, you just call it:

xsd.exe a.xsd b.xsd c.xsd /c

If you need to override the namespace, you just provide an extra parameter to xsd.exe:

/namespace:MyCompany.Xsd.Something

One way to do this is to generate the classes in different .Net namespaces.
You will then have two set of classes, but since they are in separate namespaces there won't be a conflict in your code.

EDIT:

To instruct xsd.exe to use your namespace, use the /namespace parameter, like this:

xsd.exe myxsd.xsd /namespace:MyNamespace /classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM