How to remove xmlns: xsd, xmlns: xsi, xsi: enter from WCF output using XMLSerializer

Using the WCF Restful service with XmlSerializer, I get the answer below.

<?xml version="1.0" encoding="utf-8"?> <availabilityResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xyz.com/ABCService"> <availabilityResult> <title xsi:type="Availability_1"> <titleId>0010327457</titleId> <availability> <purchasable>false</purchasable> <availableCopies>0</availableCopies> <totalCopies>0</totalCopies> </availability> </title> </availabilityResult> </availabilityResponse> 

(I want to remove xmlns: xsd, xmlns: xsi and xsi: type tags)

"Availability_1" is one of my derived type i used in my code. I really do not want to show this in the answer.

I use XmlSerialzer, specifying [XmlSerializerFormat] in the service contract. WCF knows how to serialize my answer correctly, but the only problem I encountered is the extra xmlns tags. Yes, I know that they are useful. But the client is only interested in simple xml.

After looking at the various posts in stackoverflow, I realized that I could do this by overriding some of the XmlTextWriter methods. But the problem is how to let WCF know to use my customXmlWriter (inherited from XmlTextWriter) instead of the general XmlTextWriter and serialization.

How to pass my customXmlTextWriter to an XmlSerializer that I don't have right now.

I just created my data classes and defined service contract methods from my end, but I didn’t have to do any of the serialization tools on my part, since WCF will take care of it.

+4
source share
1 answer

A slightly different idea, but it is in the line of separation of problems, since writing custom serializers actually makes the answers seem to be "invalid", visible from the true principle of correspondence, can be considered as an anti-pattern.

My idea is to develop my own custom IIS HTTP handlers and add them to the IIS processing pipeline. This will allow you to identify both modified answers and fully compatible ones.

A custom HTTP handler can use simple XSLT to remove the required namespaces.

Take a look at this article to get started - http://www.iis.net/learn/develop/runtime-extensibility/developing-iis-modules-and-handlers-with-the-net-framework

0
source

Source: https://habr.com/ru/post/1434291/


All Articles