Line Breaks in Managed Web Service Proxy Classes
Matt, Rick, and I were working on an issue recently where when an application using EWS would set a contact’s Street address to a value containing a carriage return and line feed, like this:
physicalAddress.Street = "1234 56 Ave NE\r\nc/oPatrick Creehan";
the address card control in Outlook would render it like this:
Ugly, right? The problem was that the XMLSerializer would strip out the line feed and leave the carriage return, which the address card didn’t like.
We could prove by sending raw XML requests in a separate application that sending 
 for the carriage return line feed would make everything right, however, if we set the street address like this:
physicalAddress.Street = "1234 56 Ave NE
c/oPatrick Creehan";
then the contact’s address card would look like this:
Even uglier! It seems that the .net framework, in an attempt to help us out is encoding our string for XML but it wasn’t letting us specify the value we knew was right.
So – the solution is to implement your own class which can handle the XmlSerialization yourself, and replace the auto-generated proxy class’s decision for the type to yours.
Here’s my simple class:
[SoapTypeAttribute(Namespace = "https://schemas.microsoft.com/exchange/services/2006/types",TypeName="text")]
public class mstring:IXmlSerializable
{
private string m_string = string.Empty;
public override string ToString()
{
return m_string;
}
public static mstring CreateMString(string str){
mstring newmstring = new mstring();
newmstring.m_string = str;
return newmstring;
}
public static implicit operator mstring(string str)
{
return CreateMString(str);
}
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return new System.Xml.Schema.XmlSchema();
}
public void ReadXml(XmlReader reader)
{
m_string = reader.ReadContentAsString();
}
public void WriteXml(XmlWriter writer)
{
string outString = m_string;
outString = HttpUtility.HtmlEncode(outString);
outString = outString.Replace("\r", "
");
outString = outString.Replace("\n", "
");
writer.WriteRaw(outString.ToString());
\
A few things to point out is that I decorated this class with the XML namespace for the Exchange Web Services so that it doesn’t fail schema validation. Also, I didn’t really test whether this works when binding to an existing contact – there may be more work needed in the ReadXML section. In order to support still setting the Street property to a string, I had to override the implicit operator. That allows me to set Street to a string even though technically, now Street is an “mstring.” You’ll notice that the work of actually writing the correct value occurs in WriteXml which we got by implementing IXmlSerializable. Now when the SOAP infrastructure goes to build the request, it will call into our interface to serialize this class.
That reminds me, the last thing you need to do to hook all this up is to go into the web service proxy class Reference.cs and modify the PhysicalAddressDictionaryEntryType so the street properties use your new mstring class instead of string:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4918")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="https://schemas.microsoft.com/exchange/services/2006/types")]
public partial class PhysicalAddressDictionaryEntryType {
private mstring streetField;
private string cityField;
private string stateField;
private string countryOrRegionField;
private string postalCodeField;
private PhysicalAddressKeyType keyField;
/// <remarks/>
public mstring Street {
get {
return this.streetField;
}
set {
this.streetField = value;
}
}
/// <remarks/>
public string City {
get {
return this.cityField;
}
set {
this.cityField = value;
}
}
/// <remarks/>
public string State {
get {
return this.stateField;
}
set {
this.stateField = value;
}
}
/// <remarks/>
public string CountryOrRegion {
get {
return this.countryOrRegionField;
}
set {
this.countryOrRegionField = value;
}
}
/// <remarks/>
public string PostalCode {
get {
return this.postalCodeField;
}
set {
this.postalCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public PhysicalAddressKeyType Key {
get {
return this.keyField;
}
set {
this.keyField = value;
}
}
\
and (after removing the email address so the address will fit on the card) it looks like this:
Cake.