如何使用包含相同元素/类的different.xsd命名空间?

我有点理解我应该如何使用xml文件,所以我希望你们可以指导我正确的dirrection :)希望我能解释我的问题清楚:)

我有很多.xsd文件都是从上到下连接的。 所以我有10个带有命名空间A的.xsd和带有命名空间B的10个.xsd。让我们说两个命名空间代表每个自己的汽车。 这意味着它们共享许多相同的元素,如引擎,滚轮等。我认为我可以使用xsd.exe,然后只需在我的C#代码中序列化xml文件。 但是,当我将.xsd文件转换为两个.cs文件(每个命名空间/汽车一个)时,它们共享许多相同的类。 当我想将两个.cs文件添加到我的项目时,这会产生问题。 不能有两个同名的class级…我该如何解决这个问题? 我使用错误的工具还是完全误解了我应该做什么? 🙂

.cs文件的开头:

//------------------------------------------------------------------------------ //  // 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. //  //------------------------------------------------------------------------------ using System.Xml.Serialization; // // This source code was auto-generated by xsd, Version=4.0.30319.1. // ///  [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; ///  [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; } } ///  [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")] [System.Xml.Serialization.XmlIgnoreAttribute()] public ItemsChoiceType[] ItemsElementName { get { return this.itemsElementNameField; } set { this.itemsElementNameField = value; } } ///  [System.Xml.Serialization.XmlTextAttribute()] public string[] Text { get { return this.textField; } set { this.textField = value; } } 

}

最好的方法可能是同时将所有XSD文件提供给xsd.exe。 只是为了说明,假设你有三个XSD文件,你只需要调用它:

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

如果需要覆盖命名空间,只需为xsd.exe提供一个额外参数:

 /namespace:MyCompany.Xsd.Something 

一种方法是在不同的.Net命名空间中生成类。
然后,您将拥有两组类,但由于它们位于不同的命名空间中,因此代码中不会出现冲突。

编辑:

要指示xsd.exe使用您的命名空间,请使用/ namespace参数,如下所示:

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