gpt4 book ai didi

asp.net - 摆脱使用序列化类中的 DataContract 创建的 XML 中的 d2p1

转载 作者:行者123 更新时间:2023-12-04 17:40:01 28 4
gpt4 key购买 nike

我在 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;

}

}
}

我正在用 ASP.NET MVC 3 构建一个 REST web 服务。所以我需要在 XML 中序列化这个类并将它发送到客户端。问题是我在 XML 的每个元素中都遇到了烦人的“d2p1”。以下是响应示例:
<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>

我如何摆脱 d2p1?

最佳答案

使用“命名空间”属性限定您的 DataContract,然后使用相同的命名空间名称。
像这样:

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

关于asp.net - 摆脱使用序列化类中的 DataContract 创建的 XML 中的 d2p1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6351425/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com