本文介绍了在序列化C#对象时获得垃圾邮件格式Soap XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将一个示例C#对象序列化为soap XML消息。但我得到了Junk soap XML。



**样本C#类 - **



I have serialize one sample C# object into soap XML message. But I got Junk soap XML.

**Sample C# Class -**

[Serializable]
public class Sample
{
   public CarDetails CarDetails { get; set; }
   public OwnerDetails OwnerDetails { get; set; }
}
    
[Serializable]
public class CarDetails
{
  public string Name { get; set; }
  public string Model { get; set; }
  public string Color { get; set; }
  public string Number { get; set; }
}
    
[Serializable]
public class OwnerDetails
{
  public string Name { get; set; }
  public string LastName { get; set; }
  public string Address { get; set; }
  public string City { get; set; }
  public string PhoneNumber { get; set; }           
}





**序列号 - **





**Serialization Code -**

Sample ss = new Sample();
ss.CarDetails = new CarDetails();
ss.CarDetails.Name = "Tata Nano";
ss.CarDetails.Model = "Z 500";
ss.CarDetails.Color = "Red";
ss.CarDetails.Number = "Tn 08 5222";

ss.OwnerDetails = new OwnerDetails();
ss.OwnerDetails.Name = "Jagadees";
ss.OwnerDetails.LastName = "Natrajan";
ss.OwnerDetails.Address = "Kattuputhur";
ss.OwnerDetails.City = "Trichy";
ss.OwnerDetails.PhoneNumber = "91000000000";

var r = new Random();
SoapFormatter formatter = new SoapFormatter();

string myTempFile = Path.Combine(Path.GetTempPath(), r.Next() + "xml.txt");

GC.SuppressFinalize(myTempFile);

Stream objfilestream = new FileStream(myTempFile, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(objfilestream, ss);
objfilestream.Close();



**我期待的是什么 - **




**What I Expect -**

<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>
   <sample xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SoapwbApp/SoapwbApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
       <cardetails>
           <name>Tata Nano</name>
           <model>Z 500</model>
           <color>Red</color>
           <number>Tn 08 5222</number>
       </cardetails>
       <ownerdetails>
           <name>Jagadees</name>
           <lastname>Natrajan</lastname>
           <address>Kattuputhur</address>
           <city>Trichy</city>
           <phonenumber>91000000000</phonenumber>
       </ownerdetails>
   </sample>
   </soap-env:body>
   </soap-env:envelope>





**我得到的SOAP XML - **





**SOAP XML that I got -**

<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>
    <a1:Sample id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SoapwbApp/SoapwbApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
    <_x003C_CarDetails_x003E_k__BackingField href="#ref-3"/>
    <_x003C_OwnerDetails_x003E_k__BackingField href="#ref-4"/>
    </a1:Sample>
    <a1:CarDetails id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SoapwbApp/SoapwbApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
    <_x003C_Name_x003E_k__BackingField id="ref-5">Tata Nano</_x003C_Name_x003E_k__BackingField>
    <_x003C_Model_x003E_k__BackingField id="ref-6">Z 500</_x003C_Model_x003E_k__BackingField>
    <_x003C_Color_x003E_k__BackingField id="ref-7">Red</_x003C_Color_x003E_k__BackingField>
    <_x003C_Number_x003E_k__BackingField id="ref-8">Tn 08 5222</_x003C_Number_x003E_k__BackingField>
    </a1:CarDetails>
    <a1:OwnerDetails id="ref-4" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/SoapwbApp/SoapwbApp%2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
    <_x003C_Name_x003E_k__BackingField id="ref-9">Jagadees</_x003C_Name_x003E_k__BackingField>
    <_x003C_LastName_x003E_k__BackingField id="ref-10">Natrajan</_x003C_LastName_x003E_k__BackingField>
    <_x003C_Address_x003E_k__BackingField id="ref-11">Kattuputhur</_x003C_Address_x003E_k__BackingField>
    <_x003C_City_x003E_k__BackingField id="ref-12">Trichy</_x003C_City_x003E_k__BackingField>
    <_x003C_PhoneNumber_x003E_k__BackingField id="ref-13">91000000000</_x003C_PhoneNumber_x003E_k__BackingField>
    </a1:OwnerDetails>
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>





我收到了上面的肥皂信息。它没有明确的字段_x003c我不包括在代码anywhare?我不知道为什么会这样?帮我找出错误。谢谢。



I got soap message like above,. its not have clear field _x003c something i am not including in code anywhare?. I don't know why it happen?. help me to find my mistake. thanks.

推荐答案


这篇关于在序列化C#对象时获得垃圾邮件格式Soap XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 14:02