xsd2code creates extra nested collection when serializing lists

纵饮孤独 提交于 2019-12-12 02:15:35

问题


I've been using xsd2code v3.4. So far I'm pretty close to getting it to work, however I'm facing one glaring issue and I can't seem to find any solutions. When my XML gets generated after I serialize my object, it's adding an additional complex type that is named exactly like the class. This is what I currently get. Notice how it's adding an unnecessary collection right after an order line:

<?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>

This is what I actually want:

<?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>

Is there some setting I'm using incorrectly? I have it set to work with List collections. Seems as if this problem only exists for collections of a class that's generated from this tool.

Edit: Adding some snippets of the designer class that gets generated by xsd2code. Note that this file is pretty large (almost 10k lines...), so I'm not going to post the whole thing here, but rather the chunks that pertain to the purchase order line collections of elements:

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:


I think I found a solution. I spoke with a colleague whom had done something similar. He said he used the native "xsd" and not "xsd2code". We did a compare on what got generated and noticed that on the arrays (in my case, I use lists...), he had the following annotation:

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

Is there a way to trigger this same annotation via xsd2code? Looks like without it extra elements get generated upon executing the serializer.



来源:https://stackoverflow.com/questions/35190486/xsd2code-creates-extra-nested-collection-when-serializing-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!