我正在尝试创建RESTful WCF服务。我想要Service.svc上的XML,如下
<Organization>
<Employees>
<Employee>Test1</Employee>
<Employee>Test2</Employee>
</Employees>
</Organization>
我想使用WCF C#类属性创建如上所述的XML结构
以下是我在下面尝试的内容
Public class Organization
{
Public List<Employee> Employees {get;set;}
}
Public class Employee
{
Public string Name {get;set;}
}
我做错什么了吗。
检查DataContract
属性和相应的DataMember
:https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractattribute?view=netcore-3.1
示例:
namespace MyTypes
{
[DataContract]
public class PurchaseOrder
{
private int poId_value;
// Apply the DataMemberAttribute to the property.
[DataMember]
public int PurchaseOrderId
{
get { return poId_value; }
set { poId_value = value; }
}
}
}
或者在您的情况下:
[DataContract]
Public class Organization
{
[DataMember]
Public List<Employee> Employees {get;set;}
}
[DataContract]
Public class Employee
{
[DataMember]
Public string Name {get;set;}
}
不过,这将在XML中添加一个名为name的额外节点。如果您想要定义的格式,那么删除Employee
类并将列表声明为字符串列表,如下所示:
[DataContract]
Public class Organization
{
[DataMember]
Public Employees Employees {get;set;}
}
[CollectionDataContract(ItemName="Employee")]
public class Employees: List<string> {}