lamarmora
lamarmora

Reputation: 1126

Customization of the attributes of the xml deserialized from wcf

The one that will use my wcf web service has 2 requests: He want another attribute in the first element of the xml deserialized from the object returned from the service. And he wants the existing attribute of that xml element is modified.

In order, this is the deserializzation:

ServiceReference1.MyClient c = new ServiceReference1.MyClient();

ServiceReference1.MyRequest req = new ServiceReference1.MyRequest();

// setting the request

// richiedo la lista - prezzi
ServiceReference1.MyResponse res = c.GetPricesWithTecdocFormat(req);

// serializzation
string OutputPath = this.Request.PhysicalApplicationPath;
System.Runtime.Serialization.DataContractSerializer x = new System.Runtime.Serialization.DataContractSerializer(res.GetType());
using (FileStream fs = new FileStream(OutputPath + "MyResponse.xml", FileMode.Create))
{
    x.WriteObject(fs, res);
}

The generated XML file begins like this:

<?xml version="1.0"?>
 <MyResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   ...

The request is to have this:

<?xml version="1.0"?>
 <MyResponse xsi:noNamespaceSchemaLocation="genericResponse.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

Then...

I tried and tried with a MessageInspector but with no result - it made me crazy ...

How can I do?

Pileggi

Upvotes: 1

Views: 281

Answers (1)

tom redfern
tom redfern

Reputation: 31760

You are going to have difficulty using DataContractSerializer. If you switch to using XmlSerializer, you have much more control over the serialization process.

Then you need to do two things:

  1. Use the XmlSerializerNamespaces type to manipulate the namespace prefix as described here
  2. Use the XmlAttributeOverrides type to add the noNamespaceSchemaLocation attribute into the root node.

Upvotes: 1

Related Questions