【问题标题】:xsi type and xsd tag [duplicate]xsi类型和xsd标签[重复]
【发布时间】:2016-05-14 09:04:28
【问题描述】:

我只想要一个像这样的 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 结果:

【问题讨论】:

  • 如果你能明确地解释这些差异,它会有所帮助。然后,您也可以对其进行搜索。参见例如How to add xsi:type attribute to an XML element
  • 嗨@CodeCaster 谢谢,老实说我不知道​​你的意思是什么,但我只想添加这个 xsi:type="xsd:string",就像说实体是字符串数据类型。谢谢
  • @CharlesMager 是的,更像这样,但我需要将它放在 下的每个元素中

标签: c# xml wcf


【解决方案1】:

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

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 以获得工作演示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-22
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    相关资源
    最近更新 更多