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

不问归期 提交于 2019-12-23 18:07:34

问题


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;
    }
}

}


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/9698255/how-to-work-with-different-xsd-namespaces-that-contains-the-same-elements-classe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!