I have to be missing something really easy, but alas, cant seem to figure it out
True XMLSerialization to a XSD
Posted on: 04/22/2006 04:20:42
I am having trouble configuring my service to not use DataContract serialization and instead use true XML Serialization. For example, my schema has a element defined as <XSD:ELEMENT type="xsd:string" name="CustomerName"> <XSD:ANNOTATION> <XSD:DOCUMENTATION>The name of the customer, will be in the format First Middle Last for Personal, ex John Paul Jones, or for commercial the entity name, ex Joe's Painting Services</XSD:DOCUMENTATION> </XSD:ANNOTATION>
<xsd:element name="CustomerName" type="xsd:string"> <xsd:annotation> <xsd:documentation>The name of the customer, will be in the format First Middle Last for Personal, ex John Paul Jones, or for commercial the entity name, ex Joe's Painting Services</xsd:documentation> </xsd:annotation> </xsd:element> </XSD:ELEMENT> XSD.exe created the below to support the schema private string customerNameField; /// public string CustomerName { get { return this.customerNameField; } set { this.customerNameField = value; } }
I created a IDispatchMessageInspector and peek at the xml coming and going and expect to see CustomerName elements, but instead see customerNameField elements. On my service implementation I added a [XmlSerializerFormat(Style=OperationFormatStyle.Document)] and cannot seem to effect the type of serialization being used.
If you're using the XmlSerializerFormat attribute then you're using the XmlSerializer.
You can verify by doing this after opening your servicehost:
foreach (ChannelDispatcher cd in host.ChannelDispatchers) { foreach (EndpointDispatcher endpd in cd.Endpoints) { Console.WriteLine(endpd.EndpointAddress.Uri.ToString()); foreach(DispatchOperation dspo in endpd.DispatchRuntime.Operations) {
if (dspo.Formatter != null) Console.WriteLine(dspo.Name + " uses this formatter: " + dspo.Formatter.ToString()); } }
}
If you want to control the name of the element being passed you can use this attribute:
Serialization to support XSD
m_tejas777
If you're using the XmlSerializerFormat attribute then you're using the XmlSerializer.
You can verify by doing this after opening your servicehost:
foreach (ChannelDispatcher cd in host.ChannelDispatchers)
{
foreach (EndpointDispatcher endpd in cd.Endpoints)
{
Console.WriteLine(endpd.EndpointAddress.Uri.ToString());
foreach(DispatchOperation dspo in endpd.DispatchRuntime.Operations)
{
if (dspo.Formatter != null)
Console.WriteLine(dspo.Name + " uses this formatter: " + dspo.Formatter.ToString());
}
}
}
If you want to control the name of the element being passed you can use this attribute:
[System.Xml.Serialization.
XmlElement("MyElementName")]Thanks!
Scott