XML 序列化空集合不会自动关闭 [英] XML Serialization empty collections not self closing

查看:27
本文介绍了XML 序列化空集合不会自动关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道是否有人可以帮助我解决这个烦人但微不足道的(就需要而言)问题.我有一个对象,里面有一个对象集合

Wondering if anyone can help me with this annoying but trivial (in terms of need) question. I have an object which has inside it a collection of object

 public class OuterClass
  {
    InnerClasses innerClasses= new InnerClasses();
     public InnerClasses InnerClasses
    {
      get {return innerClasses; }
    }
    public string Name
   {
     get;set;
   }
  }

  public class InnerClasses:List<InnerClass>
  {

  }
  public class <InnerClass>
  {

  }

基本上我遇到的问题是,如果我通过 xml 序列化程序传递它

basically my problem I'm experiencing is that if I pass it through an xml serializer

    var outer = new OuterClass(){Name="Name"}
    var xmlSerializer = new XmlSerializer(GetType());
    var stringBuilder = new StringBuilder();
    var stringWriter = new StringWriter(stringBuilder);    
    xmlSerializer.Serialize(stringWriter, this);
    return stringBuilder.ToString();

我想知道为什么当我没有内部类时它会输出

I'm wondering why when I have no inner classes it puts out

<OuterClass>
 <Name>Name</Name>
 <InnerClasses ></InnerClasses>
</OuterClass>

为什么不把 InnerClasses 作为自闭标签?

why does it not put InnerClasses as a self closed tag ?

我意识到上面的代码会放,但我不能放完整的实际代码清单.(我知道用处不大)我只是在寻找可能导致它的原因的指示.

I realize the code above will put but I can't put the full actual code listing. (not much use I know) I'm just looking for pointers as to what could cause it.

我终其一生都无法弄清楚为什么默认情况下它不这样做.

I can't for the life of me work out why it's not doing this by default.

非常感谢您提供有关在哪里查看的任何意见.

Many thanks for any input as to where to look.

推荐答案

我一直注意到同样的事情,但据我所知,这正是 Serialization 类的实现方式.他们只是选择不使用自闭合标签.希望这会在未来的实施中得到改变.

I've noticed the same thing all the time, but as far as I've found it's just the way the Serialization class was implemented. They simply chose to not use self closing tags. Hopefully this will be changed in future implementations.

这篇关于XML 序列化空集合不会自动关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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