public class Car
{
public string Color
{ get; set; }
public string Model
{ get; set; }
public int NoOfDoors
{ get; set; }
public int Price
{ get; set; }
public string CubicCentimeter
{ get; set; }
public CarType Type
{ get; set; }
}
Car bmw3Series = new Car{Model = "BMW 3 Series", Color = "Blue", NoOfDoors = 4, CubicCentimeter="3500cc"};
using (System.IO.Stream soapStream = new System.IO.FileStream("SoapFormat.xml", System.IO.FileMode.OpenOrCreate))
{
System.Xml.Serialization.SoapReflectionImporter soapRefImp = new System.Xml.Serialization.SoapReflectionImporter();
System.Xml.Serialization.XmlTypeMapping xmlTypeMapping =
soapRefImp.ImportTypeMapping(typeof(Car));
System.Xml.Serialization.XmlSerializer xmlSerializer =
new System.Xml.Serialization.XmlSerializer(xmlTypeMapping);
xmlSerializer.Serialize(soapStream, bmw3Series);
}
<?xml version="1.0" ?>
<Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1">
<Color xsi:type="xsd:string">Blue</Color>
<Model xsi:type="xsd:string">BMW 3 Series</Model>
<NoOfDoors xsi:type="xsd:int">4</NoOfDoors>
<Price xsi:type="xsd:int">0</Price>
<CubicCentimeter xsi:type="xsd:string">3500cc</CubicCentimeter>
</Car>
using (System.IO.Stream soapStream = new System.IO.FileStream("SoapFormatter.xml", System.IO.FileMode.Create))
{
System.Runtime.Serialization.Formatters.Soap.SoapFormatter soapFormatter =
new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
soapFormatter.Serialize(soapStream, bmw3Series);
}
<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:Car id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/
nsassem/SoapSerialization/SerializationSample%
2C%20Version%3D1.0.0.0%2C%20Culture%3Dneutral%
2C%20PublicKeyToken%3Dnull">
<_x003C_Color_x003E_k__BackingField id="ref-3">Blue</_x003C_Color_x003E_k__
BackingField>
<_x003C_Model_x003E_k__BackingField id="ref-4">BMW 3 Series</_x003C_Model_x003E_k__BackingField>
<_x003C_NoOfDoors_x003E_k__BackingField>4
</_x003C_NoOfDoors_x003E_k__BackingField>
<_x003C_Price_x003E_k__BackingField>0
</_x003C_Price_x003E_k__BackingField>
<_x003C_CubicCentimeter_x003E_k__BackingField id="ref-5">3500cc</_x003C_CubicCentimeter_
x003E_k__BackingField>
</a1:Car>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
public class Car
{
[System.Xml.Serialization.SoapAttribute (AttributeName="CarColor", DataType="string", Namespace="http://car")]
public string Color
{ get; set; }
public string Model
{ get; set; }
public int NoOfDoors
{ get; set; }
public int Price
{ get; set; }
public string CubicCentimeter
{ get; set; }
}
<?xml version="1.0"?>
<Car xmlns:xsi="http://www.w3.org/2001/XMLSchema-inst
ance"xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1"d1p1:CarColor="Blue" xmlns:d1p1="http://car">
<Model xsi:type="xsd:string">BMW 3 Series</Model>
<NoOfDoors xsi:type="xsd:int">4</NoOfDoors>
<Price xsi:type="xsd:int">0</Price>
<CubicCentimeter xsi:type="xsd:string">
3500cc</CubicCentimeter>
</Car>
public class Car
{
[System.Xml.Serialization.SoapElement
(ElementName = "CarColor", IsNullable = true)]
public string Color
{ get; set; }
[System.Xml.Serialization.SoapElement (ElementName="CarModel")]
public string Model
{ get; set; }
public int NoOfDoors
{ get; set; }
[System.Xml.Serialization.SoapElement (ElementName = "CarPrice", IsNullable = true)]
public int Price
{ get; set; }
public string CubicCentimeter
{ get; set; }
}
//Car object to be serialized.
Car bmw3Series = new Car{Model = "BMW 3 Series", NoOfDoors = 4, CubicCentimeter="3500cc"};
//Serialized SOAP XML
<?xml version="1.0"?>
<Car xmlns:xsi="http://www.w3.org/2001/XMLSchema
- instance" xmlns:xsd="http://www.w3.org/2001/
XMLSchema" id="id1">
<CarColor xsi:nil="true" />
<CarModel xsi:type="xsd:string">BMW 3 Series</CarModel>
<NoOfDoors xsi:type="xsd:int">4</NoOfDoors>
<CarPrice xsi:type="xsd:int">0</CarPrice>
<CubicCentimeter xsi:type="xsd:string">3500cc</CubicCentimeter>
</Car>
public enum CarType
{
[System.Xml.Serialization.SoapEnum(Name = "CompactCar")]
SmallCar,
[System.Xml.Serialization.SoapEnum(Name= "CompactSedan")]
CS,
Sedan,
SportsCar,
Suv
}
public class Car
{
[System.Xml.Serialization.SoapIgnore]
public string Color
{ get; set; }
[System.Xml.Serialization.SoapElement (ElementName="CarModel")]
public string Model
{ get; set; }
public int NoOfDoors
{ get; set; }
[System.Xml.Serialization.SoapElement(ElementName = "CarPrice", IsNullable = true)]
public int Price
{ get; set; }
public string CubicCentimeter
{ get; set; }
}
//Car object which needs to be serialized
Car bmw3Series = new Car{Model = "BMW 3 Series",
NoOfDoors = 4, Color="Blue", CubicCentimeter="3500cc"};
//Serialized XML with SoapIgnore attribute applied.
<?xml version="1.0"?>
<Car xmlns:xsi="http://www.w3.org/2001/
XMLSchema-instance" xmlns:xsd="http://www.w3.org
/2001/XMLSchema" id="id1">
<CarModel xsi:type="xsd:string">BMW 3 Series</CarModel>
<NoOfDoors xsi:type="xsd:int">4</NoOfDoors>
<CarPrice xsi:type="xsd:int">0</CarPrice>
<CubicCentimeter xsi:type="xsd:string">3500cc</CubicCentimeter>
</Car>500cc</CubicCentimeter>
</Car>
InvalidOperationException outer exception: "There was an error generating the XML document."
InvalidOperationException inner exception: "The type SoapSerialization.CompactCar was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically."
//Derived classes.
public class CompactCar : Car
{
public string SomeProperty
{ get; set; }
}
public class Sedan : Car
{
public int SedanCapacity
{ get; set; }
}
\*Class has a generic collection of type car. One has to use SoapInclude to tell the serializer that the class can have classes of type CompactCar and Sedan.*/
[System.Xml.Serialization.SoapInclude(typeof(CompactCar)),
System.Xml.Serialization.SoapInclude(typeof(Sedan))]
public class CarCollection
{
public System.Collections.Generic.List<Car> CarInstance
{ get; set; }
}
System.InvalidOperationException: "There was an error generating the XML document."
System.InvalidOperationException: "Token StartElement in state Epilog would result in an invalid XML document."
using (System.IO.Stream soapStream = new System.IO.FileStream("SoapFormat.xml",
System.IO.FileMode.Create))
{
using (System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(soapStream, System.Text.Encoding.UTF8))
{
System.Xml.Serialization.SoapReflectionImporter soapRefImp =
new System.Xml.Serialization.SoapReflectionImporter();
System.Xml.Serialization.XmlTypeMapping xmlTypeMapping =
soapRefImp.ImportTypeMapping(typeof(CarCollection));
System.Xml.Serialization.XmlSerializer xmlSerializer =
new System.Xml.Serialization.XmlSerializer(xmlTypeMapping);
xmlWriter.WriteStartElement("carRoot");
xmlSerializer.Serialize(xmlWriter, carCol);
xmlWriter.WriteEndElement();
}
}
System.Xml.Serialization.SoapReflectionImporter soapReflImp =
new System.Xml.Serialization.SoapReflectionImporter();
System.Xml.Serialization.XmlTypeMapping xmlTypeMap =
soapReflImp.ImportTypeMapping(typeof(CarCollection));
System.Xml.Serialization.XmlSerializer xmlSerial =
new System.Xml.Serialization.XmlSerializer(xmlTypeMap);
using (System.Xml.XmlTextReader xmlTextReader = new System.Xml.XmlTextReader("SoapFormat.xml"))
{
/*Needed to read the root element. In the serialization code we have added the "carRoot" element.*/
xmlTextReader.ReadStartElement("carRoot");
CarCollection carColl = (CarCollection)xmlSerial.Deserialize(xmlTextReader);
xmlTextReader.ReadEndElement();
}
System.InvalidOperationException: "There is an error in XML document (1, 2)."
Inner exception - System.InvalidOperationException: "<carRoot xmlns=''> was not expected."
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented: