XML序列化 - 删除命名空间 [英] xml serialization - remove namespace

查看:167
本文介绍了XML序列化 - 删除命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我序列化使用C#的对象。 ?XML版本=1.0>我在波纹管

给出

 <此格式得到的结果; 
<用户的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema>
<用户>
<使用者ID为11005477969327CREATEDATE =2008.02.04LastSendDate =1/1/0001/>
<使用者ID为11034688201594CREATEDATE =2012年4月22日LastSendDate =1/1/0001/>
<用户
< /使用者名称>



不过,我想造成这种格式。

 <?XML版本=1.0>?; 
<用户>
<使用者ID为11005477969327SendDate =1/1/0001NextSendDate =2012年2月7日上午11时十三分30秒/>
<使用者ID为11034688201594SendDate =1/1/0001NextSendDate =2012年2月7日上午11时十三分30秒/>

< /用户>



在这里我的代码



 公共类用户
{
[XmlArray(用户)]
公开名单<使用者> {的ListData获取;组; }

公共字符串的getXML()
{
System.IO.MemoryStream毫秒​​=新System.IO.MemoryStream();

XmlSerializer的SR =新的XmlSerializer(typeof运算(用户));
sr.Serialize(MS,这一点);

ms.Position = 0;
返回System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
}

公共类用户
{
[XmlAttribute(ID)]
公众的Int64用户ID {搞定;组; }

[XmlAttribute(CREATEDATE)]
公共字符串CREATEDATE {搞定;组; }

[XmlAttribute(LastSendDate)]
公共字符串LastSendDate {搞定;组; }


}


解决方案

您应该简单地替换 [XmlArray(用户)] [XmlElement的(用户)]



这属性告诉序列化,你要存储在特定节点用户所有这些用户的项目,如果你用的XmlElement ,serialzer将存储所有这些用户的内联(右下一批用户标签),就像你喜欢它。



至于的xmlns :XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema namesspaces。他们会自动添加,但它们是无害的,因为所有的元素都在默认名称空间。您可以添加以下代码到你的的XmlSerializer 调用,以删除这些:

  VAR XNS =新XmlSerializerNamespaces(); 
无功序列化=新的XmlSerializer(users.GetType());
xns.Add(的String.Empty,的String.Empty);
// ...
serializer.Serialize(流,用户,XNS);


I am serializing an object using c#. I am getting result in this format given bellow

  <?xml version="1.0"?>
    <Users xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Users>
        <User Id="11005477969327" CreateDate="06/03/2011" LastSendDate="1/1/0001" />
        <User Id="11034688201594" CreateDate="04/22/2012" LastSendDate="1/1/0001" />
       <Users 
    </User>

But I want result in this format.

<?xml version="1.0"?>
<Users>
  <User Id="11005477969327" SendDate="1/1/0001" NextSendDate="2/7/2012 11:13:30 AM" />
  <User Id="11034688201594" SendDate="1/1/0001" NextSendDate="2/7/2012 11:13:30 AM" />

</Users>

here my code

public class Users
    {
        [XmlArray("Users")]
        public List<User> ListData { get; set; }

        public string GetXML()
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();

            XmlSerializer sr = new XmlSerializer(typeof(Users));
            sr.Serialize(ms, this);

            ms.Position = 0;
            return System.Text.Encoding.UTF8.GetString(ms.ToArray());
        }
    }

    public class User
    {
        [XmlAttribute("Id")]
        public Int64 UserId { get; set; }

        [XmlAttribute("CreateDate")]
        public string CreateDate { get; set; }

        [XmlAttribute("LastSendDate")]
        public string LastSendDate { get; set; }


    }

解决方案

You should simply replace [XmlArray("Users")] with [XmlElement("User")]

This attribute tell serializer, that you want to store all those User items under particular node "Users", if you replace it with XmlElement, serialzer will store all those users inline (right under first Users tag) just as you like it.

As for xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namesspaces. They are added automatically, but they are harmless, since all your elements are in the default namespace. You may add following code to your XmlSerializer call in order to remove those:

var xns = new XmlSerializerNamespaces();
var serializer = new XmlSerializer(users.GetType());
xns.Add(string.Empty, string.Empty);
//...
serializer.Serialize(stream, users, xns);

这篇关于XML序列化 - 删除命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆