简体   繁体   English

在c#中序列化嵌套类?

[英]Serialize Nested Classes in c#?

What I'm trying to do is serialize Nested classes. 我要做的是序列化嵌套类。 My code first: 我的代码首先:

[Serializable]
public class SampleClass
{
    [Serializable]
    public class Person
    {
        [XmlElement("Name")]
        public string Name { get; set; }
        [XmlElement("Age")]
        public ushort Age { get; set; }
    }
    [Serializable]
    public class Adress 
    {
        [XmlElement("Street")]
        public string Street { get; set; }
        [XmlElement("House number")]
        public int Number { get; set; }
    }
    public SampleClass()
    { 

    }
    public SampleClass(string _name, byte _age, string _street, int _number)
    {
        Person p = new Person();
        p.Name = _name;
        p.Age = _age;
        Adress a = new Adress();
        a.Street = _street;
        a.Number = _number;
    }
}

I want to get xml like this 我想得到像这样的xml

<?xml version="1.0"?>
<SampleClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<Person>
    <Name></Name>
    <Age></Age>
</Person>
<Adress>
    <Street></Street>
    <HouseNumber></HouseNumber>
</Adress>
</SampleClass>

When I serialize this SimleClass: 当我序列化这个SimleClass时:

using (Stream str = new FileStream(@"C:/test.xml", FileMode.Create))
            {
                XmlSerializer serial = new XmlSerializer(typeof(SampleClass));
                SampleClass sClass = new SampleClass("John",15,"Street",34);
                serial.Serialize(str, sClass);
                label1.ForeColor = Color.Black;
                label1.Text = "Ok";
            }

It's give me test.xml file but inside of that file is : 它给我test.xml文件,但该文件内部是:

<?xml version="1.0"?>
 <SampleClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

What am I doing wrong? 我究竟做错了什么?

Thanks for advance:) 谢谢你提前:)

What you really want serialize is this : 你真正想要序列化的是:

    Person p = new Person();
    p.Name = _name;
    p.Age = _age;
    Adress a = new Adress();

But these variables are local. 但这些变量是本地的。 Create a property of each one and decorate them with the serializable attribute too. 创建每个属性并使用serializable属性装饰它们。 Now it will work. 现在它会起作用。

public SampleClass(string _name, byte _age, string _street, int _number)
{
    this.Person = new Person();
    Person p = this.Person;
    p.Name = _name;
    p.Age = _age;
    this.Adress = new Adress();
    Adress a = this.Adress;
    a.Street = _street;
    a.Number = _number;
}

[Serializable]
public Person Person { get; set; }
[Serializable]
public Adress Adress { get; set; }

BTW: Address takes 2 d. BTW:地址需要2天。

If you serialize an instance of the main class, the serializer will serialize an instance of the nested class if and only if the object graph contains one. 如果序列化主类的实例,则当且仅当对象图包含一个实例时,序列化程序才会序列化嵌套类的实例。 In this respect, nested classes are exactly the same as all other classes. 在这方面,嵌套类与所有其他类完全相同。

Basically you have to create properties for the nested class in the main one 基本上,您必须在主要类中为嵌套类创建属性

This line is invalid: 这一行无效:

[XmlElement("House number")] 

As an element name can't have a space in it. 由于元素名称不能包含空格。

The reason you are getting an empty XML file is that your SampleClass has no properties to serialize. 获取空XML文件的原因是您的SampleClass没有要序列化的属性。

In the constructor you are creating a Person and Address which are thrown away as soon as the method exists as you are not using them for anything. 在构造函数中,您创建了一个PersonAddress ,当方法存在时,它们会被丢弃,因为您没有使用它们。 Change your code as follows and you should have more success. 如下更改您的代码,您应该取得更大的成功。

[Serializable]
public class SampleClass
{
    [Serializable]
    public class Person
    {
        [XmlElement("Name")]
        public string Name { get; set; }

        [XmlElement("Age")]
        public ushort Age { get; set; }
    }

    [Serializable]
    public class Adress 
    {
        [XmlElement("Street")]
        public string Street { get; set; }

        [XmlElement("HouseNumber")]
        public int Number { get; set; }
    }

    public SampleClass()
    { 
    }

    public SampleClass(string name, byte age, string street, int number)
    {
        this.Person = new Person
        {
            Age = age,
            Name = name    
        };

        this.Adress = new Adress
        {
            Street = street,
            Number = number
        }
    }

    public Person Person { get; set; }
    public Address Address { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM