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

标签 c# xml xsd xsd.exe

我在理解我应该如何处理 xml 文件时遇到了一些困难,所以我希望你们能指导我正确的方向:) 希望我能足够清楚地解释我的问题:)

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

.cs 文件的开头:

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

最佳答案

最好的方法可能是将所有 XSD 文件同时提供给 xsd.exe。只是为了说明,假设您有三个 XSD 文件,您只需将其命名为:

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

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

/namespace:MyCompany.Xsd.Something

关于c# - 如何使用包含相同元素/类的 different.xsd 命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9698255/

相关文章:

c# - Windows-Phone-7:检查是否正在播放 soundEffectInstance 实例

java - 使用 NodeList 和 DocumentBuilder 解析 Xml 并获取值

Python 3 XML 解析器未知实体错误

java - 如何仅将 XMLGregorianCalendar 与日期部分(日、月、年)进行比较?

regex - 拆分 XSD 中的正则表达式模式限制

运行批处理文件的 C# 代码在控制台应用程序中工作,但相同的代码在 WCF 服务中不起作用

c# - .NET 进程的内存转储中存在大量无法解释的内存

c# - 解析 XML : Colon in my element causes XPath to miss it

java - 使用 hibernate 连接数据库时出错

xml - XML 中的递归元素