1

我在使用 Xsd2Code 从 xsd 生成代码时遇到问题。xsd 看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:ns1="urn:oasis:names:specification:ubl:schema:xsd:CountryIdentificationCode-1.0"
           xmlns:ns2="urn:oasis:names:specification:ubl:schema:xsd:LocationIdentificationCode-1.0"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

  <xs:import namespace="http://www.w3.org/2000/09/xmldsig#" schemaLocation="xmldsig.xsd" />

  .......

</xs:schema>

当我尝试序列化生成类的实例时遇到的错误:

"Cannot serialize object of type 'MyXSD.SignatureValueType'. 
 Consider changing type of XmlText member 'MyXSD.SignatureValueType.Value' from System.Byte[] to string or string array."

错误发生在这里:

[System.Xml.Serialization.XmlTextAttribute(DataType = "base64Binary")]
public byte[] Value
{
    get
    {
        return this.valueField;
    }
    set
    {
        this.valueField = value;
    }
}

如果我将该属性更改为:

[System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")]

..我得到其他错误:

Type of choice identifier 'ItemsElementName' is inconsistent with type of 'Items'.
Please use array of System.Collections.Generic.List`1[[MyXSD.ItemsChoiceType2, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].

有人成功使用 Xsd2Code 和 xmldsig 吗?如果可能的话,我宁愿用 Xsd2Code 生成器而不是手动解决这个问题。还有其他选择吗?

4

3 回答 3

0

问题是文件顶部的 DOCTYPE 定义。

<!DOCTYPE schema
 [
   <!ATTLIST schema 
     xmlns:ds CDATA #FIXED "http://www.w3.org/2000/09/xmldsig#">
   <!ENTITY dsig 'http://www.w3.org/2000/09/xmldsig#'>
   <!ENTITY % p ''>
   <!ENTITY % s ''>
  ]>

将此注释掉,代码生成将起作用。这个问题在 2009 年被报告给 xsd2code:https ://xsd2code.codeplex.com/discussions/45133 。看来这仍然是一个悬而未决的问题。

于 2014-07-05T03:01:41.703 回答
0

您是否尝试过仅使用 xsd.exe ?(http://msdn.microsoft.com/fr-fr/library/x6c1kb0s%28v=vs.80%29.aspx)

您可以使用失败的 xsd 代码部分更新您的问题吗?

于 2012-08-10T07:24:49.540 回答
0

在 Xsd2Code 选项中,将选项 'CollectionObjectType' 设置为 'Array' 而不是 'List'。

于 2015-06-23T11:57:39.183 回答