如何在XML属性中传递多个值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在XML属性中传递多个值相关的知识,希望对你有一定的参考价值。

我对肥皂的请求和回应有点新意。我正在分配给我的项目,我们的合作伙伴在那里为我们提供了wsdl。在请求中,他们提到了一个接受多个值的属性。对于要传递的值,他们提供了'* .xsd'文件。现在我不知道如何传递请求以及如何使用C#.Net中提供的* .xsd文件在属性中传递多个值。任何人都可以通过一步一步的例子来帮助我做到这一点将是一个很大的帮助。

下面是通过soap UI生成的soap请求的示例。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cre="http://creation.soapws.progressoft.com/">
   <soapenv:Header>
      <cre:paymentOperation>?</cre:paymentOperation>
      <cre:requestTimeStamp>?</cre:requestTimeStamp>
      <cre:securityToken>?</cre:securityToken>
      <cre:username>?</cre:username>
   </soapenv:Header>
   <soapenv:Body>
      <cre:createPayment>
         <!--Optional:-->
         <remitterBankBIC>?</remitterBankBIC>
         <!--Optional:-->
         <requestMsg>?</requestMsg>
      </cre:createPayment>
   </soapenv:Body>
</soapenv:Envelope>

注意:在上面的示例中,我必须传递多个值的属性。

<requestMsg>?</requestMsg>

下面是xsd示例。

    <?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
targetNamespace="http://opay.progressoft.com/PaymentCreationSchema" 
xmlns:tns="http://opay.progressoft.com/PaymentCreationSchema" elementFormDefault="qualified">

<element name="file" type="tns:paymentItem"/>
 <complexType name="PaymentRequest">
    <sequence>
            <element minOccurs="1" maxOccurs='1' name="recordType" type="tns:BodyRecordType"/> 
            <element minOccurs="1" maxOccurs='1' name="transactionReference" type="tns:Max20Text"/>
            <element minOccurs="1" maxOccurs='1' name="transactionDate" type="date"/>
            <element minOccurs="0" maxOccurs='1' name="senderCurrency" type="tns:CurrencyCode"/>
            <element minOccurs="1" maxOccurs='1' name="beneficiaryCurrency" type="tns:CurrencyCode"/>
            <element minOccurs="1" maxOccurs='1' name="transferAmount" type="tns:PaymentAmount"/>
            <element minOccurs="1" maxOccurs='1' name="remitterFirstName" type="tns:Max30Text"/>
            <element minOccurs="1" maxOccurs='1' name="remitterMiddleName" type="tns:Max30Text"/>
            <element minOccurs="0" maxOccurs='1' name="remitterLastName1" type="tns:Max30Text"/>
            <element minOccurs="0" maxOccurs='1' name="remitterLastName2" type="tns:Max40Text"/>
            <element minOccurs="0" maxOccurs='1' name="remitterNationality" type="tns:Max30Text"/>
            <element minOccurs="0" maxOccurs='1' name="remitterMobileNo" type="tns:Mobile"/>
            <element minOccurs="1" maxOccurs='1' name="remitterAddress1" type="tns:Max100Text"/>
            <element minOccurs="0" maxOccurs='1' name="remitterAddress2" type="tns:Max100Text"/>
            <element minOccurs="0" maxOccurs='1' name="remitterAddress3" type="tns:Max100Text"/>
            <element minOccurs="0" maxOccurs='1' name="payerOtherInfo1" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="payerOtherInfo2" type="tns:Max50Text"/>
            <element minOccurs="1" maxOccurs='1' name="beneficiaryFirstName" type="tns:Max30Text"/>
            <element minOccurs="1" maxOccurs='1' name="beneficiaryMiddleName" type="tns:Max30Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryLastName1" type="tns:Max30Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryLastName2" type="tns:Max30Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryNationality" type="tns:Max40Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryMobile" type="tns:Mobile"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryEmailID" type="tns:Max60Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryAddress1" type="tns:Max100Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryAddress2" type="tns:Max100Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryAddress3" type="tns:Max100Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryBankBICCode" type="tns:Max35Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryIBAN" type="tns:Max35Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryBranchName" type="tns:Max35Text"/>
            <element minOccurs="0" maxOccurs='1' name="beneficiaryID" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="benOtherInfo1" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="benOtherInfo2" type="tns:Max50Text"/>
            <element minOccurs="1" maxOccurs='1' name="transferPurpose" type="tns:Max105Text"/>
            <element minOccurs="1" maxOccurs='1' name="chargeType" type="tns:ChargeType"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField1" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField2" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField3" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField4" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField5" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField6" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField7" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField8" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField9" type="tns:Max50Text"/>
            <element minOccurs="0" maxOccurs='1' name="optionalField10" type="tns:Max50Text"/>
        </sequence>
     </complexType>

 <complexType name="paymentItem">
    <sequence>
      <element type="tns:PaymentRequest" name="payment"/>
    </sequence>
  </complexType>

    <complexType name="PaymentResponse">
        <sequence>
            <element minOccurs="1" maxOccurs='1' name="recordType" type="tns:BodyRecordType"/> 
            <element minOccurs="1" maxOccurs="1" name="transactionReference" type="tns:Max20Text" />
            <element minOccurs="0" maxOccurs="1" name="resolution" type="tns:Max10Text" /> <!--  add enum for resolution -->
            <element minOccurs="0" maxOccurs="1" name="rejectionReasonCode" type="tns:Max03Text" />
            <element minOccurs="0" maxOccurs="1" name="rejectionReasonDescription" type="tns:Max70Text" />
            <element minOccurs="0" maxOccurs="1" name="custAcctName" type="tns:Max80Text" />
            <element minOccurs="0" maxOccurs="1" name="corrBankCharge" type="tns:PaymentAmount" />
            <element minOccurs="0" maxOccurs="1" name="corrSwiftCharge" type="tns:PaymentAmount" />
            <element minOccurs="0" maxOccurs="1" name="cbjCharge" type="tns:PaymentAmount" />
            <element minOccurs="0" maxOccurs="1" name="netAmt" type="tns:PaymentAmount" />
            <element minOccurs="0" maxOccurs="1" name="netAmtCrncy" type="tns:CurrencyCode" />
            <element minOccurs="0" maxOccurs="1" name="rateCode" type="tns:Max08Text" />
            <element minOccurs="0" maxOccurs="1" name="rate" type="tns:Rate" />
            <element minOccurs="0" maxOccurs="1" name="originalAmtJOD" type="tns:PaymentAmount" />
            <element minOccurs="0" maxOccurs="1" name="tranId" type="tns:Max09Text" />
            <element minOccurs="0" maxOccurs="1" name="tranDate" type="date" />
        </sequence>
    </complexType>

    <simpleType name="Mobile">
        <restriction base="string">
            <pattern value="[00|+][0-9]{28}"/>
        </restriction>
    </simpleType>
    <simpleType name="PaymentAmount">
        <restriction base="decimal">
            <minInclusive value="0.00"/>
            <fractionDigits value="3"/>
            <totalDigits value="17"/>
            <maxInclusive value="99999999999999.99"/>
            <pattern value="[0-9]{0,14}([\.]([0-9]{0,3})){0,1}"/>
        </restriction>
    </simpleType>

    <simpleType name="Rate">
        <restriction base="decimal">
            <minInclusive value="0.00"/>
            <fractionDigits value="7"/>
            <totalDigits value="10"/>
            <maxInclusive value="999.9999999"/>
            <pattern value="[0-9]{0,3}([\.]([0-9]{0,7})){0,1}"/>
        </restriction>
    </simpleType>

    <simpleType name="ChargeType">
        <restriction base="string">
            <enumeration value="BEN"/>
            <enumeration value="OUR"/>
            <enumeration value="SHA"/>
        </restriction>
    </simpleType>    
    <simpleType name="BodyRecordType">
        <restriction base="string">
            <pattern value="[D]{1,1}"/>
        </restriction>
    </simpleType>
    <simpleType name="CurrencyCode">
        <restriction base="string">
            <pattern value="[A-Z]{3,3}"/>
        </restriction>
    </simpleType>
    <simpleType name="Max03Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="3"/>
        </restriction>
    </simpleType>    
    <simpleType name="Max08Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="8"/>
        </restriction>
    </simpleType>    
    <simpleType name="Max09Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="9"/>
        </restriction>
    </simpleType>     
    <simpleType name="Max10Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="10"/>
        </restriction>
    </simpleType>
    <simpleType name="Max105Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="105"/>
        </restriction>
    </simpleType>
    <simpleType name="Max100Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="100"/>
        </restriction>
    </simpleType>    
    <simpleType name="Max20Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="20"/>
        </restriction>
    </simpleType>
    <simpleType name="Max35Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="35"/>
        </restriction>
    </simpleType>
    <simpleType name="Max40Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="40"/>
        </restriction>
    </simpleType>
    <simpleType name="Max30Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="30"/>
        </restriction>
    </simpleType>
    <simpleType name="Max50Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="50"/>
        </restriction>
    </simpleType>
    <simpleType name="Max70Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="70"/>
        </restriction>
    </simpleType>
    <simpleType name="Max60Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="70"/>
        </restriction>
    </simpleType>
    <simpleType name="Max80Text">
        <restriction base="string">
            <minLength value="0"/>
            <maxLength value="80"/>
        </restriction>
    </simpleType>       


</schema>

提前致谢

答案

我根据您提供的架构更改了代码。使用xsd工具生成类。必须从Web获取soap模式并对soap类进行微小更改以添加特定项。架构未提供与示例xml相同的结果。我认为架构是正确的,你的样本是错误的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string operation = "Purchase";
            DateTime now = DateTime.Now;
            string token = "12345";
            string name = "John Smith";
            string bank = "BOA";
            string message = "Paid in Full";

            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
            namespaces.Add("cre", "http://creation.soapws.progressoft.com/");

            Envelope envelope = new Envelope()
            {
                Header = new EnvelopeHeader[] {
                    new EnvelopeHeader() { paymentOperation = operation, requestTimeStamp = now, securityToken = token, username = name}
                },
                Body = new EnvelopeBody[] {
                    new EnvelopeBody() {
                        item = new paymentItem() {
                            payment = new PaymentRequest[] {
                                new PaymentRequest() {
                                     beneficiaryBranchName = bank,
                                     transactionDate = now,
                                }
                            }
                        }
                    }
                }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(Envelope), "soapenv:http://schemas.xmlsoap.org/soap/envelope/");

            XmlWriterSettings settings = new XmlWriterSettings();

            settings.Indent = true;
            settings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
            settings.OmitXmlDeclaration = true;


            XmlWriter writer = XmlWriter.Create(FILENAME, settings);
            serializer.Serialize(writer, envelope, namespaces);
            writer.Flush();
            writer.Close();
            //writer.Dispose();

        }
    }
    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:2.0.50727.6421
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------

    //using System.Xml.Serialization;

    // 
    // This source code was auto-generated by xsd, Version=2.0.50727.3038.
    // 


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://opay.progressoft.com/PaymentCreationSchema")]
    [System.Xml.Serialization.XmlRootAttribute("file", Namespace="http://opay.progressoft.com/PaymentCreationSchema", IsNullable=false)]
    public partial class paymentItem {

        private PaymentRequest[] paymentField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("payment")]
        public PaymentRequest[] payment {
            get {
                return this.paymentField;
            }
            set {
                this.paymentField = value;
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://opay.progressoft.com/PaymentCreationSchema")]
    public partial class PaymentRequest {

        private string recordTypeField;

        private string transactionReferenceField;

        private System.DateTime transactionDateField;

        private string senderCurrencyField;

        private string beneficiaryCurrencyField;

        private decimal transferAmountField;

        private string remitterFirstNameField;

        private string remitterMiddleNameField;

        private string remitterLastName1Field;

        private string remitterLastName2Field;

        private string remitterNationalityField;

        private string remitterMobileNoField;

        private string remitterAddress1Field;

        private string remitterAddress2Field;

        private string remitterAddress3Field;

        private string payerOtherInfo1Field;

        private string payerOtherInfo2Field;

        private string beneficiaryFirstNameField;

        private string beneficiaryMiddleNameField;

        private string beneficiaryLastName1Field;

        private string beneficiaryLastName2Field;

        private string beneficiaryNationalityField;

        private string beneficiaryMobileField;

        private string beneficiaryEmailIDField;

        private string beneficiaryAddress1Field;

        private string beneficiaryAddress2Field;

        private string beneficiaryAddress3Field;

        private string beneficiaryBankBICCodeField;

        private string beneficiaryIBANField;

        private string beneficiaryBranchNameField;

        private string beneficiaryIDField;

        private string benOtherInfo1Field;

        private string benOtherInfo2Field;

        private string transferPurposeField;

        private string chargeTypeField;

        private string optionalField1Field;

        private string optionalField2Field;

        private string optionalField3Field;

        private string optionalField4Field;

        private string optionalField5Field;

        private string optionalField6Field;

        private string optionalField7Field;

        private string optionalField8Field;

        private string optionalField9Field;

        private string optionalField10Field;

        /// <remarks/>
        public string recordType {
            get {
                return this.recordTypeField;
            }
            set {
                this.recordTypeField = value;
            }
        }

        /// <remarks/>
        public string transactionReference {
            get {
                return this.transactionReferenceField;
            }
            set {
                this.transactionReferenceField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(DataType="date")]
        public System.DateTime transactionDate {
            get {
                return this.transactionDateField;
            }
            set {
                this.transactionDateField = value;
            }
        }

        /// <remarks/>
        public string senderCurrency {
            get {
                return this.senderCurrencyField;
            }
            set {
                this.senderCurrencyField = value;
            }
        }

        /// <remarks/>
        public string beneficiaryCurrency {
            get {
                return this.beneficiaryCurrencyField;
            }
            set {
                this.beneficiaryCurrencyField = value;
            }
        }

        /// <remarks/>
        public decimal transferAmount {
            get {
                return this.transferAmountField;
            }
            set {
                this.transferAmountField = value;
            }
        }

        /// <remarks/>
        public string remitterFirstName {
            get {
                return this.remit

以上是关于如何在XML属性中传递多个值的主要内容,如果未能解决你的问题,请参考以下文章

为动态创建的 Android 片段提供自定义属性值

如何使用(数据值属性)传递多个复选框值

如何让片段中的多个视图调用片段类中声明的相同 onClick 函数?

在Tomcat的安装目录下conf目录下的server.xml文件中增加一个xml代码片段,该代码片段中每个属性的含义与用途

如何在 Fragment 之间传递值

在Android中,如何将数据从类传递到相应的布局/片段文件?