Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get rid of d2p1 in XML created with DataContract in Serialized Class

i have the following class in C#.

using System;
using System.Collections.Generic;
using Iesi.Collections.Generic;
using System.Runtime.Serialization;

namespace PostIt.Models.Core
{
    [Serializable]
    [DataContract(Name = "Order")]
    public partial class Order
    {

        [DataMember]
        public virtual DateTime CreationDate
        {
            get;
            set;
        }

        [DataMember]
        public virtual string ReceiverName
        {
            get;
            set;
        }

        [DataMember]
        public virtual string Status
        {
            get;
            set;
        }

        [DataMember]
        public virtual int TrackingNumber
        {
            get;
            set;
        }

        [DataMember]
        public virtual Address Origin
        {
            get;
            set;
        }

        [DataMember]
        public virtual Address Destiny
        {
            get;
            set;
        }

        [DataMember]
        public virtual IList<Invoice> Invoices
        {
            get;
            set;
        }

        [DataMember]
        public virtual IList<Package> Packages
        {
            get;
            set;
        }

        [DataMember]
        public virtual IList<Stop> Stops
        {
            get;
            set;
        }

        /// <summary>
        /// Creador de la orden
        /// </summary>
        [DataMember]
        public virtual int IdPerson
        {
            get;
            set;
        }

        /// <summary>
        /// Ruta a la que pertenece
        /// </summary>
        [DataMember]
        public virtual int? IdRoute
        {
            get;
            set;
        }

        public Order()
        {
            Invoices = new List<Invoice>();
            Packages = new List<Package>();
            Stops = new List<Stop>();
        }

        public Order(int idRoute)
        {
            IdRoute = idRoute;
            Invoices = new List<Invoice>();
            Packages = new List<Package>();
            Stops = new List<Stop>();
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj))
                return true;

            return Equals(obj as Order);
        }

        public virtual bool Equals(Order obj)
        {
            if (obj == null) return false;

            if (Equals(CreationDate, obj.CreationDate) == false) return false;
            if (Equals(ReceiverName, obj.ReceiverName) == false) return false;
            if (Equals(Status, obj.Status) == false) return false;
            if (Equals(TrackingNumber, obj.TrackingNumber) == false) return false;
            return true;
        }

        public override int GetHashCode()
        {
            int result = 1;

            result = (result * 397) ^ (CreationDate != null ? CreationDate.GetHashCode() : 0);
            result = (result * 397) ^ (ReceiverName != null ? ReceiverName.GetHashCode() : 0);
            result = (result * 397) ^ (Status != null ? Status.GetHashCode() : 0);
            result = (result * 397) ^ TrackingNumber.GetHashCode();
            return result;
        }

        /// <summary>
        /// Retorna la suma del volumen de los paquetes de la orden
        /// </summary>
        /// <returns>Volumen total</returns>
        public virtual double TotalVolume()
        {
            double volume = 0;

            foreach(Package item in Packages)
            {

                volume += (double) item.Volume();
            }

            return volume;

        }

        /// <summary>
        /// Retorna la suma del peso de los paquetes de la orden
        /// </summary>
        /// <returns>Peso total</returns>
        public virtual double TotalWeight()
        {
            double weight = 0;

            foreach (Package item in Packages)
            {

                weight += (double)item.Weight;
            }

            return weight;

        }

    }
}

I'm building a REST web service with ASP.NET MVC 3. So i need to serialize this class in a XML and send it to the client. The problem is that i'm getting an annoying "d2p1" in every element of the XML. Here is an example of the response:

<RequestResult xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/PostIt.Result">
<Data i:type="d2p1:Order" xmlns:d2p1="http://schemas.datacontract.org/2004/07/PostIt.Models.Core">
    <d2p1:CreationDate>2013-04-27T00:00:00</d2p1:CreationDate>
    <d2p1:Destiny>
        <d2p1:Alias>La Casa del Lago</d2p1:Alias>
        <d2p1:City>Caracas</d2p1:City>
        <d2p1:Country>Venezuela</d2p1:Country>
        <d2p1:HouseName>Qta. Frescia</d2p1:HouseName>
        <d2p1:HouseNumber>20</d2p1:HouseNumber>
        <d2p1:IdAddress>2</d2p1:IdAddress>
        <d2p1:IdPerson>1</d2p1:IdPerson>
        <d2p1:InUse>false</d2p1:InUse>
        <d2p1:Latitude>10.49102</d2p1:Latitude>
        <d2p1:Longitude>-66.90206</d2p1:Longitude>
        <d2p1:Street>Av. Principal Santa Marta</d2p1:Street>
        <d2p1:Urbanization>Santa Marta</d2p1:Urbanization>
        <d2p1:ZipCode>1061</d2p1:ZipCode>
    </d2p1:Destiny>
    <d2p1:IdPerson>1</d2p1:IdPerson>
    <d2p1:IdRoute>1</d2p1:IdRoute>
    <d2p1:Invoices/>
    <d2p1:Origin>
        <d2p1:Alias>Mi casa</d2p1:Alias>
        <d2p1:City>Caracas</d2p1:City>
        <d2p1:Country>Venezuela</d2p1:Country>
        <d2p1:HouseName>Qta. Frescia</d2p1:HouseName>
        <d2p1:HouseNumber>20</d2p1:HouseNumber>
        <d2p1:IdAddress>1</d2p1:IdAddress>
        <d2p1:IdPerson>1</d2p1:IdPerson>
        <d2p1:InUse>true</d2p1:InUse>
        <d2p1:Latitude>48.89364</d2p1:Latitude>
        <d2p1:Longitude>2.33739</d2p1:Longitude>
        <d2p1:Street>Av. Principal Santa Marta</d2p1:Street>
        <d2p1:Urbanization>Santa Marta</d2p1:Urbanization>
        <d2p1:ZipCode>1061</d2p1:ZipCode>
    </d2p1:Origin>
    <d2p1:Packages>
        <d2p1:Package>
        <d2p1:Content>Electronicos</d2p1:Content>
        <d2p1:Height>10.2</d2p1:Height>
        <d2p1:IdPackage>1</d2p1:IdPackage>
        <d2p1:OrderTrackingNumber>1957437</d2p1:OrderTrackingNumber>
        <d2p1:Thicknes>1.3</d2p1:Thicknes>
        <d2p1:Weight>2</d2p1:Weight>
        <d2p1:Width>5.3</d2p1:Width>
        </d2p1:Package>
        <d2p1:Package>
        <d2p1:Content>Electronicos</d2p1:Content>
        <d2p1:Height>4.8</d2p1:Height>
        <d2p1:IdPackage>2</d2p1:IdPackage>
        <d2p1:OrderTrackingNumber>1957437</d2p1:OrderTrackingNumber>
        <d2p1:Thicknes>3.4</d2p1:Thicknes>
        <d2p1:Weight>8</d2p1:Weight>
        <d2p1:Width>9.2</d2p1:Width>
        </d2p1:Package>
        <d2p1:Package>
        <d2p1:Content>Electronicos</d2p1:Content>
        <d2p1:Height>15.1</d2p1:Height>
        <d2p1:IdPackage>3</d2p1:IdPackage>
        <d2p1:OrderTrackingNumber>1957437</d2p1:OrderTrackingNumber>
        <d2p1:Thicknes>2.6</d2p1:Thicknes>
        <d2p1:Weight>6</d2p1:Weight>
        <d2p1:Width>3.3</d2p1:Width>
        </d2p1:Package>
    </d2p1:Packages>
    <d2p1:ReceiverName>Pepito</d2p1:ReceiverName>
    <d2p1:Status>Recolectada</d2p1:Status>
    <d2p1:Stops>
        <d2p1:Stop>
        <d2p1:Alias>La esquina del mono</d2p1:Alias>
        <d2p1:City>Barcelona</d2p1:City>
        <d2p1:Country>Espana</d2p1:Country>
        <d2p1:IdOrder>1957437</d2p1:IdOrder>
        <d2p1:IdStop>1</d2p1:IdStop>
        <d2p1:State>Catalunia</d2p1:State>
        </d2p1:Stop>
        <d2p1:Stop>
        <d2p1:Alias>Al lado de Farmatodo</d2p1:Alias>
        <d2p1:City>Caracas</d2p1:City>
        <d2p1:Country>Venezuela</d2p1:Country>
        <d2p1:IdOrder>1957437</d2p1:IdOrder>
        <d2p1:IdStop>2</d2p1:IdStop>
        <d2p1:State>Distrito Capital</d2p1:State>
        </d2p1:Stop>
        <d2p1:Stop>
        <d2p1:Alias>La parada Loca</d2p1:Alias>
        <d2p1:City>San Diego</d2p1:City>
        <d2p1:Country>USA</d2p1:Country>
        <d2p1:IdOrder>1957437</d2p1:IdOrder>
        <d2p1:IdStop>3</d2p1:IdStop>
        <d2p1:State>California</d2p1:State>
        </d2p1:Stop>
    </d2p1:Stops>
    <d2p1:TrackingNumber>1957437</d2p1:TrackingNumber>
</Data>
<ErrorCode>1</ErrorCode>
<Message>SUCCESS</Message>
</RequestResult>

How do i get rid of the d2p1?

like image 449
OscarVGG Avatar asked Jun 14 '11 23:06

OscarVGG


1 Answers

Qualify your DataContract with the "Namespace" Attribute, and then use the same namespace name. Like this:

namespace PostIt
{
    [DataContract(Name = "ResultRequest", Namespace = "YourNamespace")]
    public class Result
    {
        [DataMember]
        Models.Core.Order order;
        [DataMember]
        int ErrorCode;
        [DataMember]
        String Message;
        ...
    }
    namespace Models.Core
    {
        [Serializable]
        [DataContract(Name = "Order", Namespace = "YourNamespace")]
        public partial class Order
        {
            ...
like image 96
jkf2 Avatar answered Sep 22 '22 02:09

jkf2