0

我有一个对象,我正在使用 xml seriliazer 对对象进行序列化。序列化后我的数据看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<doPaymentRequest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Credentials>
<accountId>101</accountId>
<userName>santosh@gmail.com</userName>
<password>1234</password>
</Credentials>
<custFirstName>santosh</custFirstName>
<custLastName>reddy</custLastName>
<ssn>1234</ssn>
<phoneNbr>1234567890</phoneNbr>
<PaymentRequest>
<ncpTranCode>10104587</ncpTranCode>
<tranType>PAY</tranType>
<Payment>
  <tranAmount>100</tranAmount>
  <paymentDateDate>2013-04-30</paymentDateDate>
  <tenderType>C</tenderType>
</Payment>
</PaymentRequest>
 <csoId>203</csoId>
 <csoStCode>8974</csoStCode>
 </doPaymentRequest>

但我的 xml 应该是这样的

   <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"   xmlns:req="http://xxxxxxxxxxxx" xmlns:com="http://xxxxxxxxxxxxxxxxxx">
             <soapenv:Header/>
                 <soapenv:Body>
                 <req:doPaymentRequest>
    <req:credentials>
     <com:accountId>*********</com:accountId>
      <com:userName>*********</com:userName>
       <com:password>*********</com:password>
             </req:credentials>
                <req:custFirstName>Test</req:custFirstName>
               <!-- Optional: -->
                 <req:custMiddleName>Test</req:custMiddleName>
                   <req:custLastName>Test</req:custLastName>
                 <req:ssn>7895</req:ssn>
                 <!-- Optional: -->
              <req:phoneNbr>1234567890</req:phoneNbr>
                   <req:paymentRequest>
                    <req:ncpTranCode>10104587</req:ncpTranCode>
                 <req:tranType>PAY</req:tranType>
                          <req:payment>
                        <req:tranAmount>100</req:tranAmount>
                         <req:paymentDateDate>2013-04-30</req:paymentDateDate>
                             <req:tenderType>C</req:tenderType>
                              </req:payment>
                            </req:paymentRequest>
                          <req:csoId>203</req:csoId>
                             <req:csoStCode>8974</req:csoStCode>
                   </req:doPaymentRequest>
               </soapenv:Body>
                 </soapenv:Envelope>

如何添加前缀,如 req、com。我正在使用 XmlSerializer 将我的对象转换为 xml。xmlSerializer.Serialize(stWriter, myobject);

是否有任何 SOAP 序列化之类的东西............编辑......我使用 SoapFormatter 并序列化对象并使用
Encoding.ASCII 将其转换为字符串。 GetString(memStream.GetBuffer())

然后我的字符串看起来很奇怪,如下所示。

  <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
          <SOAP-ENV:Body>
                <i2:doPaymentRequest id="ref-1" xmlns:i2="http://schemas.xmlsoap.org/soap/envelope/">
         <_x003C_Credentials_x003E_k__BackingField href="#ref-4"/>
          <_x003C_custFirstName_x003E_k__BackingField id="ref-      5">santosh</_x003C_custFirstName_x003E_k__BackingField>
         <_x003C_custMiddleName_x003E_k__BackingField xsi:null="1"/>
           <_x003C_custLastName_x003E_k__BackingField id="ref-  6">reddy</_x003C_custLastName_x003E_k__BackingField>
         <_x003C_ssn_x003E_k__BackingField id="ref-7">1234</_x003C_ssn_x003E_k__BackingField>
            <_x003C_phoneNbr_x003E_k__BackingField id="ref-  8">1234567890</_x003C_phoneNbr_x003E_k__BackingField>
           <_x003C_PaymentRequest_x003E_k__BackingField href="#ref-9"/>
            <_x003C_csoId_x003E_k__BackingField id="ref-  10">203</_x003C_csoId_x003E_k__BackingField>
            <_x003C_csoStCode_x003E_k__BackingField id="ref-  11">8974</_x003C_csoStCode_x003E_k__BackingField>
          </i2:doPaymentRequest>
              <a1:credentials id="ref-4"   xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MemoService/MemoService%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
             <_x003C_accountId_x003E_k__BackingField id="ref- 12">101</_x003C_accountId_x003E_k__BackingField>
             <_x003C_userName_x003E_k__BackingField id="ref-13">santosh@gmail.com</_x003C_userName_x003E_k__BackingField>
              <_x003C_password_x003E_k__BackingField id="ref-14">1234</_x003C_password_x003E_k__BackingField>
           </a1:credentials>
              <a1:paymentRequest id="ref-9"   xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MemoService/MemoService%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
               <_x003C_ncpTranCode_x003E_k__BackingField id="ref-  15">10104587</_x003C_ncpTranCode_x003E_k__BackingField>
            <_x003C_tranType_x003E_k__BackingField id="ref-  16">PAY</_x003C_tranType_x003E_k__BackingField>
             <_x003C_Payment_x003E_k__BackingField href="#ref-17"/>
                </a1:paymentRequest>
                <a1:payment id="ref-17" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/MemoService/MemoService%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
              <_x003C_tranAmount_x003E_k__BackingField id="ref- 18 ">100</_x003C_tranAmount_x003E_k__BackingField>
               <_x003C_paymentDateDate_x003E_k__BackingField id="ref-19">2013-04-

            30</_x003C_paymentDateDate_x003E_k__BackingField>
                <_x003C_tenderType_x003E_k__BackingField id="ref-  

             20">C</_x003C_tenderType_x003E_k__BackingField>
          </a1:payment>
             </SOAP-ENV:Body>
                 </SOAP-ENV:Envelope>

我在这里有什么遗漏吗...

4

1 回答 1

0
[Serializable]
public class MyData 
{
    public int MyNumber { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            MyData data = new MyData() { MyNumber = 11, Name = "StackOverflow" };

            XmlSerializer serializerXML = new XmlSerializer(data.GetType());
            serializerXML.Serialize(stream, data);

            stream.Seek(0, SeekOrigin.Begin);

            data = (MyData)serializerXML.Deserialize(stream);

            // We're cheating here, because I assume the SOAP data
            // will be larger than the previous stream. 
            stream.Seek(0, SeekOrigin.Begin);

            SoapFormatter serializerSoap = new SoapFormatter();
            serializerSoap.Serialize(stream, data);

            stream.Seek(0, SeekOrigin.Begin);

            data = (MyData)serializerSoap.Deserialize(stream);
        }
    }
}

复制自https://stackoverflow.com/a/3256887/87956

于 2013-12-18T09:15:39.590 回答