C# XmlSerializer 不会反序列化所有类属性

问题描述 投票:0回答:1

我有 C# 课程:

[XmlRoot("RESPONSE")]
public class RESPONSE
{
    [XmlElement(ElementName = "DOCUMENTS")]
    public DOCUMENTS DOCUMENTS { get; set; }

    [XmlElement(ElementName = "CONTRACTORS")]
    public CONTRACTORS CONTRACTORS { get; set; }
}

[XmlRoot(ElementName = "CONTRACTORS")]
public class CONTRACTORS
{

    [XmlElement(ElementName = "CONTRACTOR")]
    public List<CONTRACTOR> CONTRACTOR { get; set; }
}

[XmlRoot("DOCUMENTS")]
public class DOCUMENTS
{
    [XmlElement("DOCUMENT")]
    public List<DOCUMENT> DOCUMENT { get; set; }
}

我正在尝试使用该类反序列化以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<RESPONSE>
    <METAINF>
        <PRODUCER>SALDEO</PRODUCER>
        <TIMESTAMP>2023-08-12 22:22:43.628</TIMESTAMP>
        <OPERATION>document.list</OPERATION>
        <VERSION>2.12</VERSION>
        <PARAMETERS>
            <PARAMETER>
                <NAME>req_id</NAME>
                <VALUE>638274685637588935</VALUE>
            </PARAMETER>
            <PARAMETER>
                <NAME>company_program_id</NAME>
                <VALUE>1</VALUE>
            </PARAMETER>
            <PARAMETER>
                <NAME>username</NAME>
                <VALUE>xdeft</VALUE>
            </PARAMETER>
            <PARAMETER>
                <NAME>policy</NAME>
                <VALUE>LAST_10_DAYS_OCRED</VALUE>
            </PARAMETER>
        </PARAMETERS>
    </METAINF>
    <STATUS>OK</STATUS>
    <CONTRACTORS>
        <CONTRACTOR>
            <CONTRACTOR_ID>108596461</CONTRACTOR_ID>
            <SHORT_NAME>KOMPUTRONIK</SHORT_NAME>
            <FULL_NAME>KOMPUTRONIK S.A.</FULL_NAME>
            <SUPPLIER>false</SUPPLIER>
            <CUSTOMER>true</CUSTOMER>
            <VAT_NUMBER>9720902729</VAT_NUMBER>
            <CITY>Poznań</CITY>
            <POSTCODE>60-003</POSTCODE>
            <STREET>ul. Wołczyńska 37</STREET>
            <COUNTRY_ISO3166A2>PL</COUNTRY_ISO3166A2>
            <BANK_ACCOUNTS>
                <BANK_ACCOUNT>
                    <NAME>Credit Agricole</NAME>
                    <NUMBER>06 1940 1210 0103 7104 0010 0000</NUMBER>
                </BANK_ACCOUNT>
                <BANK_ACCOUNT>
                    <NAME>Credit Agricole 2</NAME>
                    <NUMBER>06 1940 1210 0103 7104 0010 0000 2</NUMBER>
                </BANK_ACCOUNT>
                <BANK_ACCOUNT>
                    <NUMBER>PL28102011270123993661772034</NUMBER>
                </BANK_ACCOUNT>
                <BANK_ACCOUNT>
                    <NUMBER>PL28102011270123993661772111</NUMBER>
                </BANK_ACCOUNT>
            </BANK_ACCOUNTS>
            <PROGRAM_PARAMETERS>
                <AREA>COUNTRY</AREA>
                <FINAL>false</FINAL>
            </PROGRAM_PARAMETERS>
            <INACTIVE>false</INACTIVE>
        </CONTRACTOR>
    </CONTRACTORS>
    <DOCUMENTS>
        <DOCUMENT>
            <DOCUMENT_ID>237888216</DOCUMENT_ID>
            <NUMBER>209/2017</NUMBER>
            <DOCUMENT_TYPE>
                <ID>51642</ID>
                <NAME>Faktura kosztowa</NAME>
                <SHORT_NAME>FK</SHORT_NAME>
                <TYPE>INVOICE_COST</TYPE>
            </DOCUMENT_TYPE>
            <CLASSIFICATION>INVOICE</CLASSIFICATION>
            <FOLDER>
                <MONTH>8</MONTH>
                <YEAR>2023</YEAR>
            </FOLDER>
            <SUM>123.00</SUM>
            <VAT_REGISTRIES>
                <VAT_REGISTRY>
                    <RATE>23</RATE>
                    <NETTO>100.00</NETTO>
                    <VAT>23.00</VAT>
                </VAT_REGISTRY>
            </VAT_REGISTRIES>
            <ITEMS>
                <ITEM>
                    <RATE>23</RATE>
                    <NETTO>100.00</NETTO>
                    <VAT>23.00</VAT>
                </ITEM>
            </ITEMS>
            <CURRENCY_ISO4217>PLN</CURRENCY_ISO4217>
            <PAYMENT_TYPE>Przelew</PAYMENT_TYPE>
            <STAGE>Nowy</STAGE>
            <EXPORTED>2023-08-12 21:55:11.565</EXPORTED>
            <SOURCE>/sdocs/sb25/127092/20238/851695172174f122faa702a6dc7c4deb/ipPkQuSPyUtVDOldAEAkaLOnzjeOuaALndRhdk230808094734_851695172174f122faa702a6dc7c4deb-1.jpg?todownload=faktura2-1.jpg</SOURCE>
            <PAGE_COUNT>1</PAGE_COUNT>
            <PROGRAM_PARAMETERS>
                <ACCOUNTING_PAYMENT_TYPE_NAME>Przelew</ACCOUNTING_PAYMENT_TYPE_NAME>
                <AREA>COUNTRY</AREA>
                <ANALYTICAL_DESCS>
                    <ANALYTICAL_DESC>
                        <DIMENSION_NAME>SALDEO</DIMENSION_NAME>
                        <NETTO>100.0</NETTO>
                    </ANALYTICAL_DESC>
                </ANALYTICAL_DESCS>
            </PROGRAM_PARAMETERS>
            <PREVIEWS>
                <PREVIEW_URL>/sdocs/sb25/127092/20238/851695172174f122faa702a6dc7c4deb/PREVIEW/f12df89aaa475b35f6c2f0cf455859eb/ipPkQuSPyUtVDOldAEAkaLOnzjeOuaALndRhdk230808094734_851695172174f122faa702a6dc7c4deb-1.jpg</PREVIEW_URL>
            </PREVIEWS>
            <LANG_ISO639_1>PL</LANG_ISO639_1>
            <IS_DOCUMENT_PAID>false</IS_DOCUMENT_PAID>
            <IS_DOCUMENT_BELONG_TO_COMPANY>true</IS_DOCUMENT_BELONG_TO_COMPANY>
            <ADDED_BY_USER_ID>188193</ADDED_BY_USER_ID>
            <ADDED_BY_USER>xdeft</ADDED_BY_USER>
            <CREATE_SOURCE>UPLOAD</CREATE_SOURCE>
        </DOCUMENT>
        <DOCUMENT>
            <DOCUMENT_ID>237888222</DOCUMENT_ID>
            <NUMBER>FV 1/09/2019</NUMBER>
            <DOCUMENT_TYPE>
                <ID>51642</ID>
                <NAME>Faktura kosztowa</NAME>
                <SHORT_NAME>FK</SHORT_NAME>
                <TYPE>INVOICE_COST</TYPE>
            </DOCUMENT_TYPE>
            <CLASSIFICATION>INVOICE</CLASSIFICATION>
            <CONTRACTOR>
                <CONTRACTOR_ID>108596461</CONTRACTOR_ID>
            </CONTRACTOR>
            <BANK_ACCOUNTS>
                <BANK_ACCOUNT>PL28102011270123993661772111</BANK_ACCOUNT>
            </BANK_ACCOUNTS>
            <FOLDER>
                <MONTH>8</MONTH>
                <YEAR>2023</YEAR>
            </FOLDER>
            <SUM>15953.10</SUM>
            <VAT_REGISTRIES>
                <VAT_REGISTRY>
                    <RATE>23</RATE>
                    <NETTO>12970.00</NETTO>
                    <VAT>2983.10</VAT>
                </VAT_REGISTRY>
            </VAT_REGISTRIES>
            <ITEMS>
                <ITEM>
                    <RATE>23</RATE>
                    <NETTO>12970.00</NETTO>
                    <VAT>2983.10</VAT>
                    <DIMENSIONS>
                        <DIMENSION>
                            <CODE>Paliwo 100 %</CODE>
                            <NAME>Paliwo 100 %</NAME>
                            <TYPE>AMOUNT</TYPE>
                            <DIMENSION_VALUES>
                                <DIMENSION_VALUE>
                                    <VALUE>123.0000</VALUE>
                                </DIMENSION_VALUE>
                            </DIMENSION_VALUES>
                        </DIMENSION>
                        <DIMENSION>
                            <CODE>Paliwo 50%</CODE>
                            <NAME>Paliwo 50%</NAME>
                            <TYPE>NUM</TYPE>
                            <DIMENSION_VALUES>
                                <DIMENSION_VALUE>
                                    <VALUE>321</VALUE>
                                </DIMENSION_VALUE>
                            </DIMENSION_VALUES>
                        </DIMENSION>
                    </DIMENSIONS>
                </ITEM>
            </ITEMS>
            <CURRENCY_ISO4217>PLN</CURRENCY_ISO4217>
            <PAYMENT_TYPE>Przelew</PAYMENT_TYPE>
            <STAGE>Nowy</STAGE>
            <EXPORTED>2023-08-12 21:55:11.565</EXPORTED>
            <SOURCE>/sdocs/sb25/127092/20238/78159d41478225314658a632c22bc259/SvovPBGEOTxMrhfKyAkKfJdszpPBoOegDHHPJz230808094717_78159d41478225314658a632c22bc259-1.jpg?todownload=faktura1-1.jpg</SOURCE>
            <PAGE_COUNT>1</PAGE_COUNT>
            <PROGRAM_PARAMETERS>
                <ACCOUNTING_PAYMENT_TYPE_NAME>Przelew</ACCOUNTING_PAYMENT_TYPE_NAME>
                <AREA>COUNTRY</AREA>
                <ANALYTICAL_DESCS>
                    <ANALYTICAL_DESC>
                        <DIMENSION_NAME>SALDEO</DIMENSION_NAME>
                        <NETTO>12970.0</NETTO>
                    </ANALYTICAL_DESC>
                </ANALYTICAL_DESCS>
            </PROGRAM_PARAMETERS>
            <PREVIEWS>
                <PREVIEW_URL>/sdocs/sb25/127092/20238/78159d41478225314658a632c22bc259/PREVIEW/06c7052e5c901c5c00554a56f3139ee9/SvovPBGEOTxMrhfKyAkKfJdszpPBoOegDHHPJz230808094717_78159d41478225314658a632c22bc259-1.jpg</PREVIEW_URL>
            </PREVIEWS>
            <LANG_ISO639_1>PL</LANG_ISO639_1>
            <DIMENSIONS>
                <DIMENSION>
                    <CODE>Paliwo 100 %</CODE>
                    <NAME>Paliwo 100 %</NAME>
                    <TYPE>AMOUNT</TYPE>
                    <DIMENSION_VALUES>
                        <DIMENSION_VALUE>
                            <VALUE>123.0000</VALUE>
                        </DIMENSION_VALUE>
                    </DIMENSION_VALUES>
                </DIMENSION>
                <DIMENSION>
                    <CODE>Paliwo 50%</CODE>
                    <NAME>Paliwo 50%</NAME>
                    <TYPE>NUM</TYPE>
                    <DIMENSION_VALUES>
                        <DIMENSION_VALUE>
                            <VALUE>321</VALUE>
                        </DIMENSION_VALUE>
                    </DIMENSION_VALUES>
                </DIMENSION>
            </DIMENSIONS>
            <IS_DOCUMENT_PAID>false</IS_DOCUMENT_PAID>
            <IS_DOCUMENT_BELONG_TO_COMPANY>false</IS_DOCUMENT_BELONG_TO_COMPANY>
            <ADDED_BY_USER_ID>188193</ADDED_BY_USER_ID>
            <ADDED_BY_USER>xdeft</ADDED_BY_USER>
            <CREATE_SOURCE>UPLOAD</CREATE_SOURCE>
        </DOCUMENT>
        <DOCUMENT>
            <DOCUMENT_ID>237888224</DOCUMENT_ID>
            <NUMBER>1/5</NUMBER>
            <ISSUE_DATE>2022-05-02</ISSUE_DATE>
            <SALE_DATE>2022-05-02</SALE_DATE>
            <PAYMENT_DATE>2022-05-01</PAYMENT_DATE>
            <RECEIVE_DATE>2022-05-02</RECEIVE_DATE>
            <DOCUMENT_TYPE>
                <ID>51642</ID>
                <NAME>Faktura kosztowa</NAME>
                <SHORT_NAME>FK</SHORT_NAME>
                <TYPE>INVOICE_COST</TYPE>
            </DOCUMENT_TYPE>
            <CLASSIFICATION>INVOICE</CLASSIFICATION>
            <CONTRACTOR>
                <NIP>9291734667</NIP>
            </CONTRACTOR>
            <BANK_ACCOUNTS>
                <BANK_ACCOUNT>PL15116022020000000094904655</BANK_ACCOUNT>
            </BANK_ACCOUNTS>
            <FOLDER>
                <MONTH>8</MONTH>
                <YEAR>2023</YEAR>
            </FOLDER>
            <SUM>270.60</SUM>
            <VAT_REGISTRIES>
                <VAT_REGISTRY>
                    <RATE>23</RATE>
                    <NETTO>220.00</NETTO>
                    <VAT>50.60</VAT>
                </VAT_REGISTRY>
            </VAT_REGISTRIES>
            <ITEMS>
                <ITEM>
                    <RATE>23</RATE>
                    <NETTO>220.00</NETTO>
                    <VAT>50.60</VAT>
                </ITEM>
            </ITEMS>
            <CURRENCY_ISO4217>PLN</CURRENCY_ISO4217>
            <PAYMENT_TYPE>Przelew</PAYMENT_TYPE>
            <STAGE>Nowy</STAGE>
            <EXPORTED>2023-08-12 21:55:11.565</EXPORTED>
            <SOURCE>/sdocs/sb25/127092/20238/467ba70eacafaca18b81a18a2cdea9fc/BumoudrQwbAnyHVrlPjFbUgVpqmRmOrERHmCUH230808094734_467ba70eacafaca18b81a18a2cdea9fc-1.jpg?todownload=faktura3-1.jpg</SOURCE>
            <PAGE_COUNT>1</PAGE_COUNT>
            <PROGRAM_PARAMETERS>
                <VAT_DEDUCTION>2022-05-02</VAT_DEDUCTION>
                <ACCOUNTING_PAYMENT_TYPE_NAME>Przelew</ACCOUNTING_PAYMENT_TYPE_NAME>
                <AREA>COUNTRY</AREA>
                <ANALYTICAL_DESCS>
                    <ANALYTICAL_DESC>
                        <DIMENSION_NAME>SALDEO</DIMENSION_NAME>
                        <NETTO>220.0</NETTO>
                    </ANALYTICAL_DESC>
                </ANALYTICAL_DESCS>
            </PROGRAM_PARAMETERS>
            <PREVIEWS>
                <PREVIEW_URL>/sdocs/sb25/127092/20238/467ba70eacafaca18b81a18a2cdea9fc/PREVIEW/b6f4f08680b6248c199b411f9e258c12/BumoudrQwbAnyHVrlPjFbUgVpqmRmOrERHmCUH230808094734_467ba70eacafaca18b81a18a2cdea9fc-1.jpg</PREVIEW_URL>
            </PREVIEWS>
            <LANG_ISO639_1>PL</LANG_ISO639_1>
            <IS_DOCUMENT_PAID>false</IS_DOCUMENT_PAID>
            <IS_DOCUMENT_BELONG_TO_COMPANY>true</IS_DOCUMENT_BELONG_TO_COMPANY>
            <ADDED_BY_USER_ID>188193</ADDED_BY_USER_ID>
            <ADDED_BY_USER>xdeft</ADDED_BY_USER>
            <CREATE_SOURCE>UPLOAD</CREATE_SOURCE>
        </DOCUMENT>
    </DOCUMENTS>
</RESPONSE>

我得到以下结果:

(https://i.stack.imgur.com/SZcG3.png)

其中

DOCUMENTS
属性为 null,但是当我从
CONTRACTORS
类中删除
RESPONSE
属性时:

[XmlRoot("RESPONSE")]
public class RESPONSE
{
    [XmlElement(ElementName = "DOCUMENTS")]
    public DOCUMENTS DOCUMENTS { get; set; }

    //[XmlElement(ElementName = "CONTRACTORS")]
    //public CONTRACTORS CONTRACTORS { get; set; }

}

DOCUMENTS
属性已正确反序列化。

(https://i.stack.imgur.com/yq6pK.png)

XmlSerializer
类没有任何异常集合。

您对问题所在有什么建议吗?

我为

DOCUMENTS
CONTRACTORS
类创建了构造函数,并在内部设置了断点。调试器仅在
CONTRACTORS
类中停止。

我在 notepadd++ 中启用了一个选项“查看所有特殊字符”以检查 XML 是否包含意外字符。它没有任何意想不到的字符。

我使用 https://xmltocsharp.azurewebsites.net/生成的所有类 我期望正确反序列化所有属性。

c# xml xml-deserialization
1个回答
0
投票

您需要小心并注意 XML 文档中 XML 节点的顺序。这需要与 C# 类中属性的顺序保持一致。

您的 XML 显示

<CONTRACTORS>
before
<DOCUMENTS>
- 因此您的 C# 类需要采用相同的顺序 -
CONTRACTORS
属性 before
DOCUMENTS
属性:

[XmlRoot("RESPONSE")]
public class RESPONSE
{
    [XmlElement(ElementName = "CONTRACTORS")]
    public CONTRACTORS CONTRACTORS { get; set; }

    [XmlElement(ElementName = "DOCUMENTS")]
    public DOCUMENTS DOCUMENTS { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.