1

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...

  • xmlns:i has to be xmlns:xsi

  • in the element there mast be the attribute with the reference to the .xsd file

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

How can I do?

Pileggi

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
lamarmora
  • 1,116
  • 4
  • 16
  • 32

1 Answers1

1

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.
Community
  • 1
  • 1
tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • Thank you very much. In a first time I had used the XmlSerializer then I switched to DataContractSerializer because with the XmlSerializer I had some strange additional xml elements: – lamarmora Sep 05 '12 at 09:32
  • @lamarmora - Any reason why you first awarded and then un-awarded this as the answer? – tom redfern May 19 '15 at 18:01