【问题标题】:ASMX Service XML request deserializing partiallyASMX 服务 XML 请求部分反序列化
【发布时间】:2017-05-12 09:15:28
【问题描述】:

首先我想说我知道这项古老的技术,如果由我来决定,生活会有所不同。我对这项旧技术比较陌生,我正在扩展工作中的现有服务,我遇到了一个奇怪的问题。我有 3 个服务端点,其中 2 个按预期工作,但我对第三个的问题是“框架”无法反序列化 xml 请求。

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <UpdateSale xmlns="http://tokenws.netgen.co.za/">
         <p_objrequest>
            <transactionTypeId>1</transactionTypeId>
            <tenderTypeId>1</tenderTypeId>
            <standardHeader>
               <requestId xmlns="">1_8</requestId>
               <localeId xmlns="" />
               <systemId xmlns="">asdf</systemId>
               <batchReference xmlns="">11</batchReference>
            </standardHeader>
            <account>
               <accountId xmlns="">123</accountId>
               <pin xmlns="" >123</pin>
            </account>
            <amount>
               <valueCode xmlns="">ZAR</valueCode>
               <enteredAmount xmlns="">30</enteredAmount>
               <nsfAllowed xmlns="">N</nsfAllowed>
            </amount>
            <lineItems>
               <LineItem>
                  <productCode>1</productCode>
                  <categoryCode>1</categoryCode>
                  <qty>1</qty>
                  <price>50</price>
                  <discountedPrice>0</discountedPrice>
                  <description>Buffet Breakfast</description>
               </LineItem>
            </lineItems>
         </p_objrequest>
         <netCredentials>
            <UserName xmlns="http://tempuri.org/">123</UserName>
            <Password xmlns="http://tempuri.org/">123</Password>
         </netCredentials>
      </UpdateSale>
   </soap:Body>
</soap:Envelope>

上面是 xml,netCredentials 被正确反序列化,但 p_objrequest 为空。

如果您遇到过这个问题并解决了它,如果您能引导我走向正确的方向,我将不胜感激。

提前致谢。

编辑

下面是类

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://tokenws.netgen.co.za/")]
    public class Sale
    {

        private int transactionTypeIdField;

        private int tenderTypeIdField;

        private RequestStandardHeaderComponent standardHeaderField;

        private AccountComponent accountField;

        private string activatingField;

        private AmountComponent amountField;

        private CustomerInfoComponent customerInfoField;

        private PromotionCode[] promotionCodesField;

        private QuestionAndAnswer[] questionsAndAnswersField;

        private LineItem[] lineItemsField;

        private string includeTipField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
        public int transactionTypeId
        {
            get
            {
                return this.transactionTypeIdField;
            }
            set
            {
                this.transactionTypeIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
        public int tenderTypeId
        {
            get
            {
                return this.tenderTypeIdField;
            }
            set
            {
                this.tenderTypeIdField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 2)]
        public RequestStandardHeaderComponent standardHeader
        {
            get
            {
                return this.standardHeaderField;
            }
            set
            {
                this.standardHeaderField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 3)]
        public AccountComponent account
        {
            get
            {
                return this.accountField;
            }
            set
            {
                this.accountField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 4)]
        public string activating
        {
            get
            {
                return this.activatingField;
            }
            set
            {
                this.activatingField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 5)]
        public AmountComponent amount
        {
            get
            {
                return this.amountField;
            }
            set
            {
                this.amountField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 6)]
        public CustomerInfoComponent customerInfo
        {
            get
            {
                return this.customerInfoField;
            }
            set
            {
                this.customerInfoField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Order = 7)]
        public PromotionCode[] promotionCodes
        {
            get
            {
                return this.promotionCodesField;
            }
            set
            {
                this.promotionCodesField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Order = 8)]
        public QuestionAndAnswer[] questionsAndAnswers
        {
            get
            {
                return this.questionsAndAnswersField;
            }
            set
            {
                this.questionsAndAnswersField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlArrayAttribute(Order = 9)]
        public LineItem[] lineItems
        {
            get
            {
                return this.lineItemsField;
            }
            set
            {
                this.lineItemsField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 10)]
        public string includeTip
        {
            get
            {
                return this.includeTipField;
            }
            set
            {
                this.includeTipField = value;
            }
        }
    }

【问题讨论】:

  • 这可能没什么,但你试过切换顺序吗?将Password 放在UserName 之前?
  • 我需要去上课。要么 a 属性不公开,要么缺少其中一个命名空间:xmlns="tokenws.netgen.co.za" 或 "tempuri.org"
  • @jdweng 我已经通过放置类更新了帖子
  • @christophano 我有,它没有影响,仍然正确反序列化它但 p_objrequest 失败。

标签: c# xml web-services wcf asmx


【解决方案1】:

标签名称和类/属性名称必须匹配,包括大小写。见下文:

    [XmlRoot(ElementName= "UpdateSale", Namespace="http://tokenws.netgen.co.za/")]
    public class Sale
    {
    }
    [XmlRoot(ElementName= "netCredentials")]
    public class NetCredentials
    {
        [XmlElement(ElementName="UserName",Namespace="http://tempuri.org/")]
        public Name userName { get; set;} 
        [XmlElement(ElementName="Password",Namespace="http://tempuri.org/")]
        public Name password { get; set;} 
    }
    public class Name
    {
        [XmlText]
        public string name { get; set;}
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2020-05-12
    • 2011-01-04
    • 1970-01-01
    • 2010-10-30
    相关资源
    最近更新 更多