c# - Xsd.exe 生成 <xs :list> 的错误实现

标签 c# code-generation xsd.exe

这是 Xsd.exe 生成的示例(也是 Xsd2Code,但使用通用 List<>)。

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="xxxxxxxxxxxxxxxx")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="xxxxxxxxxxxxxxxx", IsNullable=false)]
public partial class ItemQuantity 
{

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Units;

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyAttributeAttribute()]
    public System.Xml.XmlAttribute[] AnyAttr;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()] // <-- this is the bug
    public double[] Text;   // <-- this is the bug corresponding to a <xs:list itemType=xs:double/> in the schema
}

我正在处理一个非常大的模式,它多次使用 xs:list 来表示不同的基元...

所有这些不是字符串 [](而是 double [] 十进制 [])的文本属性都会在运行时导致此异常当与 XmlSerializer 一起使用时:(我将其翻译成英文) 成员 Text 不能使用属性 XmlText 进行编码。您可以将属性 XmlText 与基元、枚举、字符串数组或 XmlNode 数组一起使用。

你有什么建议?

最佳答案

我已经编辑了 xsd.exe 生成的代码并放置

[System.Xml.Serialization.XmlArrayItemAttribute("Double",typeof(double), IsNullable = false)]

代替

[System.Xml.Serialization.XmlTextAttribute()]

据我判断……它确实表现得更好……

<Test><Double>0.1</Double><Double>0.2</Double><Double>0.8</Double><Double>0.505</Double></Test>

但我应该确保在 Test 标记中包含的所有列表对于我的架构仍然有效...

关于c# - Xsd.exe 生成 <xs :list> 的错误实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6495516/

相关文章:

c# - 保留选择序列中的顺序 (LINQ To XSD)

c# - 如何找到两个日期之间的年月差?

c# - 在设计或构建时生成方法 (C#)

xml - Delphi 类到 XML 表示 :

java - 将java类对象写入java源文件

c# - TDD - 使用属性自动生成代码

c# - XmlSerializer 不填充子元素

c# - 将参数传递给 .fromsql

c# - OleDB 连接仅在调试时有效

c# - msdn C# 图形示例中的无效参数错误