C#序列化一个包含对象列表的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19992569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:19:01  来源:igfitidea点击:

C# Serialize an object with a list of objects in it

c#xmlserialization

提问by Max Young

In C# if I serialize an object that has a list of objects in it will it also serialize the list?

在 C# 中,如果我序列化一个包含对象列表的对象,它是否也会序列化该列表?

Example

例子

public class Move {
    public string MoveName {get;  set;}

    public List<Tag> oTags = new List<Tag>;
}

public class Tag {
    public string TagName {get; set;}
}

If I serialize move will all the tags stored in move get serialized as well? Also if it will not serialize the list how would I go about making it do that?

如果我序列化 move 中存储的所有标签也会被序列化吗?另外,如果它不会序列化列表,我将如何让它做到这一点?

<Move>
  <MoveName>name</MoveName>
  <Tag>Value</Tag>
  ...
</Move>

采纳答案by Chris Sinclair

Yes, using the XmlSerializerit will serialize a List<T>so long as T(or in your case Tag) is serializable.

是的,只要(或在您的情况下)是可序列化的,使用XmlSerializer它就会序列化。List<T>TTag

Move move = new Move { MoveName = "MyName" };
move.oTags.Add(new Tag { TagName = "Value1" } );
move.oTags.Add(new Tag { TagName = "Value2" } );
move.oTags.Add(new Tag { TagName = "Value3" } );

StringBuilder output = new StringBuilder();
var writer = new StringWriter(output);

XmlSerializer serializer = new XmlSerializer(typeof(Move));
serializer.Serialize(writer, move);

Console.WriteLine(output.ToString());

This outputs using your current class structure as:

这使用您当前的类结构输出为:

<?xml version="1.0" encoding="utf-16"?>
<Move xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <oTags>
    <Tag>
      <TagName>Value1</TagName>
    </Tag>
    <Tag>
      <TagName>Value2</TagName>
    </Tag>
    <Tag>
      <TagName>Value3</TagName>
    </Tag>
  </oTags>
  <MoveName>MyName</MoveName>
</Move>

I'll see if I can find a way to match your current XML schema, but you can look up how to apply XmlAttributesand play around with it yourself.

我会看看我是否能找到一种方法来匹配您当前的 XML 架构,但是您可以查找如何应用XmlAttributes并自己尝试使用它。



EDIT:

编辑:

If you change your class declaration to use the following XmlAttributes, you will achieve the exact XML schema as in your example:

如果您将类声明更改为使用以下 XmlAttributes,您将获得与示例中相同的 XML 架构:

public class Move 
{
    [XmlElement(Order = 1)]
    public string MoveName {get; set;}

    [XmlElement(Order = 2, ElementName = "Tags")]
    public List<Tag> oTags = new List<Tag>();
}

public class Tag 
{
    [XmlText]
    public string TagName {get; set;}
}

Which when serialized will produce:

序列化时将产生:

<?xml version="1.0" encoding="utf-16"?>
<Move xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MoveName>MyName</MoveName>
  <Tags>Value1</Tags>
  <Tags>Value2</Tags>
  <Tags>Value3</Tags>
</Move>

回答by Brendan Hill

By default, no it won't, since the items within the list may not be serializable.

默认情况下,不会,因为列表中的项目可能无法序列化。

If they are, then you may find the following page userful:

如果是,那么您可能会发现以下页面很有用:

XML Serialize generic list of serializable objects

XML 序列化可序列化对象的通用列表

回答by Kas

Are you sure that your class Declarations are right in your Question ? you are just declaring Public Move, It should be Public class Move

你确定你的类声明在你的问题中是正确的吗?你只是声明Public Move,应该是Public class Move

Try this code

试试这个代码

 XmlSerializer serializer = new XmlSerializer(typeof(YourClass)); 

In Your case

在你的情况下

Move m = new Move();
            m.oTags.Add(new Tag() { TagName = "X" });
            m.oTags.Add(new Tag() { TagName = "XX" });

            XmlSerializer x = new XmlSerializer(typeof(Move));
            System.IO.Stream s;

            var xmlwriter = System.Xml.XmlWriter.Create("C:\MXL.txt"); 
            x.Serialize(xmlwriter, m);

OutPut

输出

    <?xml version="1.0" encoding="utf-8"?>
    <Move xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <oTags>
<Tag>
  <TagName>X</TagName>
</Tag>
<Tag>
   <TagName>XX</TagName>
</Tag>
</oTags></Move>