Serialization issues with XmlElement and DataContrac  
Author Message
BenK95781





PostPosted: Windows Communication Foundation ("Indigo"), Serialization issues with XmlElement and DataContrac Top

Hello all ,

I just sorted out my inheritance issues and now have an issue where an Xml element when serialized has xmlns="" inserted .


ie

<CommsMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:q1="http://tempuri.org/" xsi:type="q1:AckMessage">
<q1:MessagesToAck><q1:string>urn:uuid:11a072c0-4b78-44b1-b2ec-9ea6cda90b07</q1:string></q1:MessagesToAck>
</CommsMessage>

the proxy changes it to

<CommsMessage xsi:type="q1:AckMessage" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:q1="http://tempuri.org/">
<q1:MessagesToAck>
<q1:string>urn:uuid:11a072c0-4b78-44b1-b2ec-9ea6cda90b07</q1:string>
</q1:MessagesToAck>
</CommsMessage>

as you can see from the log

2006-10-31 05:44:18.030,<MessageLogTraceRecord Time="2006-10-31T16:44:18.0295600+11:00" Source="ServiceLevelReceiveRequest" Type="System.ServiceModel.Channels.MessagePatterns+PatternMessage" xmlns="http://schemas.microsoft.com/2004/06/ServiceModel/Management/MessageTrace"><s:Envelope xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:s="http://www.w3.org/2003/05/soap-envelope"><s:Header><a:Action s:mustUnderstand="1">Store</a:Action><a:MessageID>urn:uuid:c1650b91-ff21-44b3-9d3e-85ec74b56c07</a:MessageID><a:ReplyTo><a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address></a:ReplyTo><a:To s:mustUnderstand="1">net.tcp://adswebsrvr:8032/MessageStorageService</a:To></s:Header><s:Body><StoreMessage xmlns="http://STE.MessageStorage.ServiceContracts/2006/08/"><Topic>HandScanner:SerialNumber.TestScanner</Topic><Message xmlns="http://tempuri.org/" xmlns:b="STE.MessageStorage.DataContract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><b:Identifier>urn:uuid:5e250015-dc7a-4752-9cdb-47dfc0769d9b</b:Identifier><b:MessageBody><CommsMessage xsi:type="q1:AckMessage" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:q1="http://tempuri.org/"><q1:MessagesToAck><q1:string>urn:uuid:6b6cf44a-8ebd-4772-afba-bb36fabef6ec</q1:string></q1:MessagesToAck></CommsMessage></b:MessageBody></Message></StoreMessage></s:Body></s:Envelope></MessageLogTraceRecord>

2006-10-31 05:44:17.983, message to client <CommsMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:q1="http://tempuri.org/" xsi:type="q1:AckMessage"><q1:MessagesToAck><q1:string>urn:uuid:6b6cf44a-8ebd-4772-afba-bb36fabef6ec</q1:string></q1:MessagesToAck></CommsMessage>

I could prob go to XmlSerializer but dont want to change the service.

Any ideas will be appreciated.

Regards,

Ben

PS: sorry for the badly formatted code.

private void SendMessageToClient(CommsMessage message)

{

string deviceId = OperationContext.Current.IncomingMessageProperties[DEVICE_ID] as string;

string topic = new MessageStoreTopic().SerialNumberToTopic(deviceId);

IMessageStorageService client = new MessageStorageServiceClient();

//XmlElement element = XmlHelper.ObjectToXml(message);

System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(CommsMessage),

new Type[3] {

typeof(DriverLoginResponseMessage),

typeof(AckMessage),

typeof (ErrorMessage)

}

);

XmlDocument doc = new XmlDocument();

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())

{

XmlWriter xtw = XmlTextWriter.Create(ms, null);

// XmlSerializer xs = new XmlSerializer(message.GetType());

xs.Serialize(xtw, message);

ms.Position = 0;

doc.Load(ms);

}

Logger.Write(" message to client " + doc.DocumentElement.OuterXml); // Correct

AckMessage ackMessage = new AckMessage();

ackMessage.MessagesToAck = new MessagesList();

ackMessage.MessagesToAck.Add("test123" );

XmlDocument doc2 = new XmlDocument();

using (System.IO.MemoryStream ms = new System.IO.MemoryStream())

{

XmlWriter xtw = XmlTextWriter.Create(ms, null);

// XmlSerializer xs = new XmlSerializer(message.GetType());

xs.Serialize(xtw, ackMessage);

ms.Position = 0;

doc2.Load(ms);

}

Logger.Write(" message to client test " + doc2.DocumentElement.OuterXml);

MessageHolder holder = new MessageHolder();

holder.Identifier = new System.Xml.UniqueId().ToString();

holder.MessageBody = doc.DocumentElement;

// holder.MessageBody.SetAttribute(holder.MessageBody.Name, @"http://tempuri.org/"); // hack could strip for storage

StoreMessage messageToStore = new StoreMessage(topic , holder);

client.StoreMessage(messageToStore);

} // SendMessageToClient

Proxy -----------------------------------------------------------------

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]

[System.Runtime.Serialization.DataContractAttribute(Namespace="STE.MessageStorage.DataContract")]

[System.SerializableAttribute()]

public partial class MessageHolder : object, System.Runtime.Serialization.IExtensibleDataObject

{

[System.NonSerializedAttribute()]

private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

[System.Runtime.Serialization.OptionalFieldAttribute()]

private string IdentifierField;

[System.Runtime.Serialization.OptionalFieldAttribute()]

private System.Xml.XmlElement MessageBodyField;

public System.Runtime.Serialization.ExtensionDataObject ExtensionData

{

get

{

return this.extensionDataField;

}

set

{

this.extensionDataField = value;

}

}

[System.Runtime.Serialization.DataMemberAttribute()]

public string Identifier

{

get

{

return this.IdentifierField;

}

set

{

this.IdentifierField = value;

}

}

[System.Runtime.Serialization.DataMemberAttribute()]

public System.Xml.XmlElement MessageBody

{

get

{

return this.MessageBodyField;

}

set

{

this.MessageBodyField = value;

}

}

}

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]

[System.ServiceModel.MessageContractAttribute(WrapperName="StoreMessage", WrapperNamespace="http://STE.MessageStorage.ServiceContracts/2006/08/", IsWrapped=true)]

public partial class StoreMessage

{

[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://STE.MessageStorage.ServiceContracts/2006/08/", Order=0)]

public string Topic;

[System.ServiceModel.MessageBodyMemberAttribute(Namespace="http://tempuri.org/", Order=1)]

public STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.MessageHolder Message;

public StoreMessage()

{

}

// added namepsace to holder no change

public StoreMessage(string Topic, STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.MessageHolder Message)

{

this.Topic = Topic;

this.Message = Message;

}

}

[System.Diagnostics.DebuggerStepThroughAttribute()]

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]

public partial class MessageStorageServiceClient : System.ServiceModel.ClientBase<STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.IMessageStorageService>, STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.IMessageStorageService

{

public MessageStorageServiceClient()

{

}

public MessageStorageServiceClient(string endpointConfigurationName) :

base(endpointConfigurationName)

{

}

public MessageStorageServiceClient(string endpointConfigurationName, string remoteAddress) :

base(endpointConfigurationName, remoteAddress)

{

}

public MessageStorageServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :

base(endpointConfigurationName, remoteAddress)

{

}

public MessageStorageServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :

base(binding, remoteAddress)

{

}

STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.StoreResponseMessage STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.IMessageStorageService.StoreMessage(STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.StoreMessage request)

{

return base.Channel.StoreMessage(request);

}

public string StoreMessage(string Topic, STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.MessageHolder Message)

{

STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.StoreMessage inValue = new STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.StoreMessage();

inValue.Topic = Topic;

inValue.Message = Message;

STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.StoreResponseMessage retVal = ((STE.HandScanner.BusinessLogic.MessageStorageServiceProxy.IMessageStorageService)(this)).StoreMessage(inValue);

return retVal.UniqueId;

}



Visual Studio 200829  
 
 
BenK





PostPosted: Windows Communication Foundation ("Indigo"), Serialization issues with XmlElement and DataContrac Top

anyone
 
 
Brian McNamara - MSFT





PostPosted: Windows Communication Foundation ("Indigo"), Serialization issues with XmlElement and DataContrac Top

I am unclear what your question is.

The two XML fragments are semantically equivalent - inserting the xmlns="" does not alter the meaning, and indeed it helps protect the meaning when this fragment is embedded in a larger XML document (where outer elements may have an xmlns defined).

Is this causing you a particular problem If so, let's try to address that problem directly.



 
 
BenK





PostPosted: Windows Communication Foundation ("Indigo"), Serialization issues with XmlElement and DataContrac Top

Thanks Brian ,

You are correct that it is still well formed XML and does not alter the meaning however the insertion of the xmlns="" causes the XML serilaizer to complain with an XML errror on line 0 (or 1 if i insert the document start) . Hence i would prefer fro the DataContract serializer not to put it in. I dont think changing to fragment mode helps.

Worst case if this is not possible is there an efficient way to take it out of an XmlElment object without converting it to a string and stripping it . ( Dealing with lots of transactions).

Regards,

Ben


 
 
Brian McNamara - MSFT





PostPosted: Windows Communication Foundation ("Indigo"), Serialization issues with XmlElement and DataContrac Top

When you say you are getting an error from the Xml serializer, does this mean you are using both DataContract and other methods with [XmlFormat] Or how/when is the XmlSerializer being invoked Can you share a little more about your code What is the exact error message