加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在C#中使用XML序列化包含元素(而不是根)的属性

发布时间:2020-12-15 21:39:56 所属栏目:百科 来源:网络整理
导读:我需要生成一个如下所示的 XML: ?xml version="1.0" encoding="UTF-8" standalone="yes"?inboundMessage xmlns="http://www.myurl.net" header passwordmypwd/password subscriberIdmyuser/subscriberId /header message xmlns:xsi="http://www.w3.org/2001
我需要生成一个如下所示的 XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<inboundMessage xmlns="http://www.myurl.net">
  <header>
    <password>mypwd</password>
    <subscriberId>myuser</subscriberId>
  </header>
  <message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="myType">
    <eventDate>2012-09-05T12:13:45.561-05:00</eventDate>
    <externalEventId />
    <externalId>SomeIdC</externalId>
  </message>
</inboundMessage>

问题是我不知道如何在我的标签中包含xmlns:xsi =“http://www.w3.org/2001/XMLSchema-instance”xsi:type =“myType”.我需要序列化的类是这样的:

[XmlType("inboundMessage")]
[XmlRoot(Namespace = "http://www.myurl.net")]
public class InboundMessage
{
    [XmlElement(ElementName = "header")]
    public Header _header;
    [XmlElement(ElementName = "message")]
    public List<MyType> _messages;
}

我需要将哪些XmlAttributes添加到我的“_messages”成员中,以便按照我想要的方式进行序列化?

TIA,
埃德

解决方法

像这样使用XmlAttribute:

public class MyType
{
    [XmlAttribute("type",Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Type { get; set; }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读