2

I am creating a REST service using asp.net mvc4 web api. My service returns xml as output. I want to change some apsects of the xml response including: - The xml root node - Add namespaces - Remove xsi:nil in the xml I am using a datacontext file (Linq to sql dbml file) in my model and not a user defined class. I have read from this link http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization that I can use DataContract to do so but don't know how to implement that in my case. I do not want to use message handlers since this will require loading the entire xml in a string and might affect performance regarding that the xml output returned might be big

please help...

Sam
  • 69
  • 1
  • 7
  • Posting relevant code snippets showing what you *have tried* will encourage people to help. – EBarr Jun 28 '12 at 14:53

1 Answers1

1

This example should be helpful:

[DataContract(Name = "Customer", Namespace = "http://www.contoso.com")]
class Person : IExtensibleDataObject
{
    // To implement the IExtensibleDataObject interface, you must also 
    // implement the ExtensionData property. 
    private ExtensionDataObject extensionDataObjectValue;
    public ExtensionDataObject ExtensionData
    {
        get
        {
            return extensionDataObjectValue;
        }
        set
        {
            extensionDataObjectValue = value;
        }
    }

    [DataMember(Name = "CustName")]
    internal string Name;

    [DataMember(Name = "CustID")]
    internal int ID;

    public Person(string newName, int newID)
    {
        Name = newName;
        ID = newID;
    }

}

You can read more on MSDN

Maksymilian Majer
  • 2,956
  • 2
  • 29
  • 42
  • How can we define that a property converts to an Attribute instead of a separate node. For example in your above code, I want "ID" to be an attribute of `Person` node when it converts to xml; like: `` – Tohid Aug 27 '12 at 18:55
  • 1
    I believe it can't be done using `DataContract`. See here for clues: http://stackoverflow.com/a/1644041/310274 – Maksymilian Majer Aug 28 '12 at 05:48