xsi类型和xsd标记

时间:2016-05-14 09:04:28

标签: c# xml wcf

我只想要像这样的xml输出:

<ranzcp_user xmlns:ns1="urn:logon">
<user_id  xsi:type="xsd:string">12345678</user_id>
<user_name xsi:type="xsd:string">JTestFloor</user_name>
<title xsi:type="xsd:string">Dr</title>
<first_name xsi:type="xsd:string">TestJoni</first_name>
<last_name xsi:type="xsd:string">TestFloor</last_name>
<email xsi:type="xsd:string">Joni.Floor2@test.com </email>
<organisation_identifier xsi:type="xsd:string">RANZCPAU</organisation_identifier>

我已经有了一个wcf服务,目前这是用于创建那些xml的基本模型:

public class user
{
    public string user_id { get; set; }
    public string user_name { get; set; }
    public string title { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
    public string email { get; set; }
    public string organisation_identifier { get; set; }
}

并简单调用该服务:

public class Service1 : IService1
{
    public ranzcp_user UserData()
    {
        ranzcp_user data = new ranzcp_user();
        data.user_id = "12345678";
        data.user_name = "JTestFloor";
        data.title = "Dr";
        data.first_name = "TestJoni";
        data.last_name = "TestFloor";
        data.email = "Joni.Floor2@test.com";
        data.organisation_identifier = "RANZCPAU";

        return data;
    }
}

界面:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
    ranzcp_user UserData();

    // TODO: Add your service operations here
}

然后它给了我一个像这样的xml结果: enter image description here

1 个答案:

答案 0 :(得分:3)

最简单的方法是将属性的类型更改为不太具体的属性 - 然后序列化程序将插入类型属性以标识类型。

public class ranzcp_user
{
    public object user_id { get; set; }
    public object user_name { get; set; }
    public object title { get; set; }
    public object first_name { get; set; }
    public object last_name { get; set; }
    public object email { get; set; }
    public object organisation_identifier { get; set; }
}

有关正常工作的演示,请参阅this fiddle