WCF REST Service - XML response indentation - wcf

I have WCF rest service returning XML/RSS feeds. Some methods return data contracts, some Rss20FeedFormatter, and some are streams.
In all cases XML returns without any indentation. But I have requirement to return it formatted with indentations.
Is it possible to enable XML response indentation for WCF service?

You can load the xml output onto an xmlDocument and use XmlTextWriter to add "indent" settings.
Or you can use a simple xslt to transform the output xml onto the format that you want. for indent use
<xsl:output indent="yes" method="xml"/>

I'm not sure if WCF services can return formatted responses but here's a link to a nice method for formatting XML easily that you could implement from the calling source code.

Related

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

Using WCF Restful service with XmlSerializer I get the below response.
<?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 wish to remove xmlns:xsd, xmlns:xsi and xsi:type tags)
"Availability_1" is one my derived type i used in my code. I really do not want to show this in the response.
I am using XmlSerialzer by specifying [XmlSerializerFormat] at the service contract.
WCF is able to serialize my response properly but the only issue i have is with the extra xmlns tags. Yes, I know they are useful stuff there. But, the client is interested only in the plain xml.
By looking at various posts in stackoverflow i understood i could do this by overriding few of the methods of XmlTextWriter. But the problem I have is how to let the WCF know to use my customXmlWriter (inherited from XmlTextWriter) instead of generic XmlTextWriter while serialization.
How to pass my customXmlTextWriter to the XmlSerializer which i do not have any control at this point.
I just created my data classes and defined the service contract methods from my end but did not have to do any of the serialization stuff from my side as the WCF takes care of it on its own.
A slightly different idea, but it's in the line of separation of concerns, as writing custom serialisers to actually make the responses sort of "invalid" seen from a true compliancy principe can be seen as an anti-pattern.
My idea is to develop your own IIS custom HTTP handlers and add it to the IIS processing pipeline. Doing so, will allow you to expose both the tweaked responses and the fully compliant ones.
The custom HTTP handler could use a simple XSLT to remove the required namespaces.
Have 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

Handling wcf Rest web service using KissXML - how should I handle namespace issue

I'm using KissXML on iOS to parse the XML data returned from a wcf REST web service (written in c#)
The trouble I have is that KissXML doesn't like the namespace in the XML data that I get from the web service.
In a trivial example the XML data I get looks like this:
<ArrayOfItem xmlns="http://schemas.datacontract.org/2004/07/DataUpdateService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Item>Test</Item></ArrayOfItem>
KissXML can't seem to use xpath to parse this XML
resultNodes = [xmlDoc nodesForXPath:#"//Item" error:&error];
If I remove the default namespace from the XML string - then it works fine.
<ArrayOfItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Item>Test</Item></ArrayOfItem>
So this XML is fine and using the objective-c above I get 1 object in my array (as expected).
So the question is: What should I be doing about this? I have control of both the service and the iOS app so I can either change the service to try to remove or change the namespace (pointers to how to do this appreciated), or I can rip it out of my string once I get the data to the phone.
Both of these options feel a bit like a hack - are there better options?
I would recommend changing the defaultOutgoingResponseFormat to Json,
and handling it instead of xml.
Why? read here:
http://www.json.org/xml.html
and also it will solve your problem :)

Best way to parse unknown XML fom web service

I am working on a project where I required to parse XML response coming from web service. I don't know anything about the structure of response. I am given XML elements to gather data for, and the data is relational so elements represents columns in relational table.
I tried the WSDL approach where I can parse for method name and its response type. Based on that I can parse with NSXMLParser. The problem was I didn't find any WSDL parser which does this for me.
Any suggestions?!
Thanks,
Jignesh
I like to use this OpenSource library on GitHub
It will allow you to turn the XML string into an NSDictionary which you can the use dynamically.

How to customize XmlResponse in WCF service using WebHttpBinding

I need to intercept the response of a service created using WCF with webHttpBinding (REST based service).
Now I want the XML emitted in a customized way. For e.g, currently a service method returns output like this:
<User xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<UserCellInfo>+91-98102239</UserCellInfo>
<UserDepartment>.NET Projects</UserDepartment>
<UserID>A10129</UserID>
<UserName>Jeff Thomson</UserName>
</User>
but I want it in a different custom way.Something like below
<?xml version="1.0" encoding="UTF-8"?>
<rsp>
<User>
<UserCellInfo>+91-98102239</UserCellInfo>
<UserDepartment>.NET Projects</UserDepartment>
<UserId>A10129</UserId>
<UserName>Jeff Thomson</UserName>
</User>
</rsp>
In MSDN, I read that I need to use IDispatchMessageInspector to intercept the XML that goes out. But I couldn't find any code reference or example for it.
Any good ideas how to achieve this?
So I assume your WCF service method returns an instance of a User object in this method call, right?
Why not just define a wrapper class called rsp that contains the User instance? Then returning the rsp object would render in the desired style. Just a nice clean wrapper - no messy XML manipulation on the fly......

In WCF, how do I convert a Datatable to a format that will output in JSON store format without classes

So here's the problem - I have a DataTable I want WCF (.NET 3.5) to send out in a JSON store format commonly used in ExtJS, etc - basically "Rows[{"Field1":value,"Field2":value},{...}]" but I cannot find the right structure to feed back to the Operation contract to send it out in this format.
So any ideas, or any further info needed.
Thanks, in advance!
AndyPC, unfortunately, you're out of luck.
If you're dealing with an object whose type is an IXmlSerializable, the WCF JSON serializer delegates to IXmlSerializable methods first, gets the serialized XML out of them, wraps the XML in a JSON string, and just passes that on. This is one of the major weaknesses of the WCF JSON model in .NET 3.5. I think the entity framework (WCF Data Services) technologies try to handle this more elegantly, but not sure. I'd recommend manually using the JSON serializer and crafting up a string or a manual serialization mechanism that does what you want...