Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 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>


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.
Posted
Updated 5-Nov-14 18:45pm
v3

1 solution

You're not making a mistake, SOAP is a bit different from standard XML serialization because it contains the needed metadata to reconstruct the object (and how it relates to other objects). Those field values are added in by the SoapFormatter to reassemble objects into a given state.

For what you want use XML serialization instead.

For more details have a look at:
Serialization using different formats in .NET[^]
 
Share this answer
 
Comments
Jagadeeswaran Natarajan 7-Nov-14 0:00am    
Thanks for helping me out,. I was worried about its my mistake,. now I clear,. once again Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900