xsd2code在序列化列表时创建额外的嵌套集合

时间:2016-02-04 00:37:02

标签: c# .net xml xsd xsd2code

我一直在使用xsd2code v3.4。 到目前为止,我已经非常接近它的工作,但是我面临一个明显的问题,我似乎无法找到任何解决方案。在我序列化对象后生成XML时,它会添加一个与类完全相同的复杂类型。这就是我目前得到的。请注意它是如何在订单行之后添加不必要的集合的:

<?xml version="1.0" encoding="utf-8"?>
<CORE_PO_INBOUND_V2 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <INTEGRATION_MESSAGE_CONTROL>
    <ACTION>FULL_UPDATE</ACTION>
    <COMPANY_CODE>COMPANY</COMPANY_CODE>
    <ORG_CODE>COMPANY</ORG_CODE>
    <MESSAGE_TYPE>INBOUND_ENTITY_INTEGRATION</MESSAGE_TYPE>
    <USERID>COMPANY</USERID>
    <RECEIVER>TA15</RECEIVER>
    <SENDER>COMPANY</SENDER>
    <BATCH_ID>1234</BATCH_ID>
    <BUS_KEY>
      <ORG_CODE>COMPANY</ORG_CODE>
      <PO_NUMBER>1234</PO_NUMBER>
    </BUS_KEY>
  </INTEGRATION_MESSAGE_CONTROL>
  <PURCHASE_ORDER_HEADER>
    <CTRY_OF_EXPORT>TR</CTRY_OF_EXPORT>
    <CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
    <CURRENCY_CODE>USD</CURRENCY_CODE>
    <INCOTERM_CODE>011</INCOTERM_CODE>
    <ORG_CODE>COMPANY</ORG_CODE>
    <SOURCE_TX_ID>THING</SOURCE_TX_ID>
    <PO_NUMBER>1234</PO_NUMBER>
    <PURCHASE_ORDER_LINE>
      <CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE>
        <BUSINESS_UNIT>BCA</BUSINESS_UNIT>
        <COMMERCIAL_UOM>EA</COMMERCIAL_UOM>
        <CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
        <CURRENCY_CODE>USD</CURRENCY_CODE>
        <DEPARTMENT>602</DEPARTMENT>
        <LINE_ID>1</LINE_ID>
      </CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE>
    </PURCHASE_ORDER_LINE>
    <PURCHASE_ORDER_HEADER_PARTNER>
      <CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_HEADER_PARTNER>
        <REF_RESOLUTION_PARTNER>Stuff</REF_RESOLUTION_PARTNER>
      </CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_HEADER_PARTNER>
    </PURCHASE_ORDER_HEADER_PARTNER>
  </PURCHASE_ORDER_HEADER>
</CORE_PO_INBOUND_V2>

这就是我真正想要的:

<?xml version="1.0" encoding="utf-8"?>
<CORE_PO_INBOUND_V2 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <INTEGRATION_MESSAGE_CONTROL>
    <ACTION>FULL_UPDATE</ACTION>
    <COMPANY_CODE>COMPANY</COMPANY_CODE>
    <ORG_CODE>COMPANY</ORG_CODE>
    <MESSAGE_TYPE>INBOUND_ENTITY_INTEGRATION</MESSAGE_TYPE>
    <USERID>COMPANY</USERID>
    <RECEIVER>TA15</RECEIVER>
    <SENDER>COMPANY</SENDER>
    <BATCH_ID>1234</BATCH_ID>
    <BUS_KEY>
      <ORG_CODE>COMPANY</ORG_CODE>
      <PO_NUMBER>1234</PO_NUMBER>
    </BUS_KEY>
  </INTEGRATION_MESSAGE_CONTROL>
  <PURCHASE_ORDER_HEADER>
    <CTRY_OF_EXPORT>TR</CTRY_OF_EXPORT>
    <CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
    <CURRENCY_CODE>USD</CURRENCY_CODE>
    <INCOTERM_CODE>011</INCOTERM_CODE>
    <ORG_CODE>COMPANY</ORG_CODE>
    <SOURCE_TX_ID>THING</SOURCE_TX_ID>
    <PO_NUMBER>1234</PO_NUMBER>
    <PURCHASE_ORDER_LINE>
        <BUSINESS_UNIT>BCA</BUSINESS_UNIT>
        <COMMERCIAL_UOM>EA</COMMERCIAL_UOM>
        <CTRY_OF_IMPORT>US</CTRY_OF_IMPORT>
        <CURRENCY_CODE>USD</CURRENCY_CODE>
        <DEPARTMENT>602</DEPARTMENT>
        <LINE_ID>1</LINE_ID>
    </PURCHASE_ORDER_LINE>
    <PURCHASE_ORDER_HEADER_PARTNER>
        <REF_RESOLUTION_PARTNER>Stuff</REF_RESOLUTION_PARTNER>
    </PURCHASE_ORDER_HEADER_PARTNER>
  </PURCHASE_ORDER_HEADER>
</CORE_PO_INBOUND_V2>

我有不正确使用的设置吗?我将它设置为使用List集合。似乎此问题仅存在于从此工具生成的类的集合中。

编辑:添加由xsd2code生成的设计器类的一些片段。请注意,此文件非常大(几乎10k行...),因此我不打算在此处发布整个内容,而是发布与采购订单行元素集合相关的块:

public partial class CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADER : EntityBase<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADER>
    {
private List<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE> pURCHASE_ORDER_LINEField;

public List<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE> PURCHASE_ORDER_LINE
        {
            get
            {
                if ((this.pURCHASE_ORDER_LINEField == null))
                {
                    this.pURCHASE_ORDER_LINEField = new List<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE>();
                }
                return this.pURCHASE_ORDER_LINEField;
            }
            set
            {
                this.pURCHASE_ORDER_LINEField = value;
            }
        }
}
public partial class CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE : EntityBase<CORE_PO_INBOUND_V2PURCHASE_ORDER_HEADERPURCHASE_ORDER_LINE>
    {

        private System.Nullable<decimal> aREAField;

        private bool aREAFieldSpecified;

        private string aREA_UOMField;
...
}

1 个答案:

答案 0 :(得分:1)

我想我找到了解决方案。我和一位做过类似事情的同事交谈过。他说他使用原生“xsd”而不是“xsd2code”。我们对生成的内容进行了比较并注意到在数组上(在我的例子中,我使用了列表......),他有以下注释:

[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]

有没有办法通过xsd2code触发相同的注释?看起来没有它,在执行串行器时会产生额外的元素。