将XML序列化为C#类时,不需要现有的命名空间

时间:2016-11-01 05:44:08

标签: c# xml serialization

我正在尝试将从外部API接收到的XML序列化为C#模型,但我得知错误说

  

错误在第1行,第55位。预期项目\“Unitellerresult \”来自名称空间\“http://schemas.datacontract.org/2004/07/ \”..检测到“元素\”已命名\“unitellerresult \”namespace \“\”。

但肯定没有这个命名空间。这是原始XML:

<?xml version='1.0' encoding='utf-8' standalone='yes'?>
<unitellerresult firstcode='' secondcode='' count='0'>
    <request>
        <zipflag></zipflag>
        <shopordernumber>1</shopordernumber>
        <shop_id>00007578</shop_id>
        <format>4</format>
        <delimiter></delimiter>
        <opendelimiter></opendelimiter>
        <closedelimiter></closedelimiter>
        <rowdelimiter></rowdelimiter>
        <meantype></meantype>
        <emoneytype></emoneytype>
        <startdate></startdate>
        <enddate></enddate>
        <success>2</success>
    </request>
    <orders>
        <field>
            <ordernumber>OrderNumber</ordernumber>
            <response_code>Response_Code</response_code>
            <recommendation>Recommendation</recommendation>
            <message>Message</message>
            <comment>Comment</comment>
            <date>Date</date>
            <total>Total</total>
            <currency>Currency</currency>
            <cardtype>CardType</cardtype>
            <cardnumber>CardNumber</cardnumber>
            <lastname>LastName</lastname>
            <firstname>FirstName</firstname>
            <middlename>MiddleName</middlename>
            <address>Address</address>
            <email>Email</email>
            <country>Country</country>
            <rate>Rate</rate>
            <approvalcode>ApprovalCode</approvalcode>
            <cardsubtype>CardSubType</cardsubtype>
            <cvc2>CVC2</cvc2>
            <cardholder>CardHolder</cardholder>
            <ipaddress>IPAddress</ipaddress>
            <protocoltypename>ProtocolTypeName</protocoltypename>
            <billnumber>BillNumber</billnumber>
            <bankname>BankName</bankname>
            <status>Status</status>
            <error_code>Error_Code</error_code>
            <error_comment>Error_Comment</error_comment>
            <processingname>ProcessingName</processingname>
            <packetdate>PacketDate</packetdate>
            <paymenttype>PaymentType</paymenttype>
            <phone>Phone</phone>
            <idata>IData</idata>
            <pt_code>PT_Code</pt_code>
            <emoneytype>EMoneyType</emoneytype>
            <eorderdata>EOrderData</eorderdata>
            <card_idp>Card_IDP</card_idp>
            <acquirerid>AcquirerID</acquirerid>
            <isothercard>IsOtherCard</isothercard>
            <parent_order_number>parent_order_number</parent_order_number>
            <need_confirm>need_confirm</need_confirm>
            <gds_payment_purpose_id>gds_payment_purpose_id</gds_payment_purpose_id>
            <bookingcom_id>bookingcom_id</bookingcom_id>
            <bookingcom_pincode>bookingcom_pincode</bookingcom_pincode>
            <loanid>LoanID</loanid>
        </field>
    </orders>
</unitellerresult>

这是C#模型:

using System.Xml.Serialization;

    [XmlRoot(ElementName = "request")]
    public class Request
    {
        [XmlElement(ElementName = "zipflag")]
        public string Zipflag { get; set; }
        [XmlElement(ElementName = "shopordernumber")]
        public string Shopordernumber { get; set; }
        [XmlElement(ElementName = "shop_id")]
        public string Shop_id { get; set; }
        [XmlElement(ElementName = "format")]
        public string Format { get; set; }
        [XmlElement(ElementName = "delimiter")]
        public string Delimiter { get; set; }
        [XmlElement(ElementName = "opendelimiter")]
        public string Opendelimiter { get; set; }
        [XmlElement(ElementName = "closedelimiter")]
        public string Closedelimiter { get; set; }
        [XmlElement(ElementName = "rowdelimiter")]
        public string Rowdelimiter { get; set; }
        [XmlElement(ElementName = "meantype")]
        public string Meantype { get; set; }
        [XmlElement(ElementName = "emoneytype")]
        public string Emoneytype { get; set; }
        [XmlElement(ElementName = "startdate")]
        public string Startdate { get; set; }
        [XmlElement(ElementName = "enddate")]
        public string Enddate { get; set; }
        [XmlElement(ElementName = "success")]
        public string Success { get; set; }
    }

    [XmlRoot(ElementName = "field")]
    public class Field
    {
        [XmlElement(ElementName = "ordernumber")]
        public string Ordernumber { get; set; }
        [XmlElement(ElementName = "response_code")]
        public string Response_code { get; set; }
        [XmlElement(ElementName = "recommendation")]
        public string Recommendation { get; set; }
        [XmlElement(ElementName = "message")]
        public string Message { get; set; }
        [XmlElement(ElementName = "comment")]
        public string Comment { get; set; }
        [XmlElement(ElementName = "date")]
        public string Date { get; set; }
        [XmlElement(ElementName = "total")]
        public string Total { get; set; }
        [XmlElement(ElementName = "currency")]
        public string Currency { get; set; }
        [XmlElement(ElementName = "cardtype")]
        public string Cardtype { get; set; }
        [XmlElement(ElementName = "cardnumber")]
        public string Cardnumber { get; set; }
        [XmlElement(ElementName = "lastname")]
        public string Lastname { get; set; }
        [XmlElement(ElementName = "firstname")]
        public string Firstname { get; set; }
        [XmlElement(ElementName = "middlename")]
        public string Middlename { get; set; }
        [XmlElement(ElementName = "address")]
        public string Address { get; set; }
        [XmlElement(ElementName = "email")]
        public string Email { get; set; }
        [XmlElement(ElementName = "country")]
        public string Country { get; set; }
        [XmlElement(ElementName = "rate")]
        public string Rate { get; set; }
        [XmlElement(ElementName = "approvalcode")]
        public string Approvalcode { get; set; }
        [XmlElement(ElementName = "cardsubtype")]
        public string Cardsubtype { get; set; }
        [XmlElement(ElementName = "cvc2")]
        public string Cvc2 { get; set; }
        [XmlElement(ElementName = "cardholder")]
        public string Cardholder { get; set; }
        [XmlElement(ElementName = "ipaddress")]
        public string Ipaddress { get; set; }
        [XmlElement(ElementName = "protocoltypename")]
        public string Protocoltypename { get; set; }
        [XmlElement(ElementName = "billnumber")]
        public string Billnumber { get; set; }
        [XmlElement(ElementName = "bankname")]
        public string Bankname { get; set; }
        [XmlElement(ElementName = "status")]
        public string Status { get; set; }
        [XmlElement(ElementName = "error_code")]
        public string Error_code { get; set; }
        [XmlElement(ElementName = "error_comment")]
        public string Error_comment { get; set; }
        [XmlElement(ElementName = "processingname")]
        public string Processingname { get; set; }
        [XmlElement(ElementName = "packetdate")]
        public string Packetdate { get; set; }
        [XmlElement(ElementName = "paymenttype")]
        public string Paymenttype { get; set; }
        [XmlElement(ElementName = "phone")]
        public string Phone { get; set; }
        [XmlElement(ElementName = "idata")]
        public string Idata { get; set; }
        [XmlElement(ElementName = "pt_code")]
        public string Pt_code { get; set; }
        [XmlElement(ElementName = "emoneytype")]
        public string Emoneytype { get; set; }
        [XmlElement(ElementName = "eorderdata")]
        public string Eorderdata { get; set; }
        [XmlElement(ElementName = "card_idp")]
        public string Card_idp { get; set; }
        [XmlElement(ElementName = "acquirerid")]
        public string Acquirerid { get; set; }
        [XmlElement(ElementName = "isothercard")]
        public string Isothercard { get; set; }
        [XmlElement(ElementName = "parent_order_number")]
        public string Parent_order_number { get; set; }
        [XmlElement(ElementName = "need_confirm")]
        public string Need_confirm { get; set; }
        [XmlElement(ElementName = "gds_payment_purpose_id")]
        public string Gds_payment_purpose_id { get; set; }
        [XmlElement(ElementName = "bookingcom_id")]
        public string Bookingcom_id { get; set; }
        [XmlElement(ElementName = "bookingcom_pincode")]
        public string Bookingcom_pincode { get; set; }
        [XmlElement(ElementName = "loanid")]
        public string Loanid { get; set; }
    }

    [XmlRoot(ElementName = "orders")]
    public class Orders
    {
        [XmlElement(ElementName = "field")]
        public Field Field { get; set; }
    }

    [XmlRoot(ElementName = "unitellerresult")]
    public class Unitellerresult
    {
        [XmlElement(ElementName = "request")]
        public Request Request { get; set; }
        [XmlElement(ElementName = "orders")]
        public Orders Orders { get; set; }
        [XmlAttribute(AttributeName = "firstcode")]
        public string Firstcode { get; set; }
        [XmlAttribute(AttributeName = "secondcode")]
        public string Secondcode { get; set; }
        [XmlAttribute(AttributeName = "count")]
        public string Count { get; set; }
    }

这就是我试图阅读XML的方式:

var client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
            var url = _unitellerFormBuilder.CreateStatusUrl(orderId);
            Unitellerresult result = null;
            var response = await client.GetAsync($"https://wpay.uniteller.ru/results/{url}");
            if (response.IsSuccessStatusCode)
            {
                result = await response.Content.ReadAsAsync<Unitellerresult>();
            }
            return result;

0 个答案:

没有答案