将xml反序列化为多个对象列表

时间:2016-07-01 04:16:38

标签: c# xml serialization uwp

我在UWP项目中反序列化时出现问题,我已成功创建了一个包含来自我的两个对象列表的XElements的xml文档,我需要反序列化这些值并填充两个对象列表。

我的课程:

public class Values_Body: BindableBase
{
    private bool _ToAcero;
    public bool ToAcero
    {
        get { return _ToAcero; }
        set { SetProperty(ref _ToAcero, value); }
    }
    private double? _Parcial;
    public double? Parcial
    {
        get { return _Parcial; }
        set { SetProperty(ref _Parcial, value); }
    }
    private double? _Total;
    public double? Total
    {
        get { return _Total; }
        set { SetProperty(ref _Total, value); }
    }        
}

我的代码:

                Values_Head list1 = new Values_Head();
                list1.Add(new Head()
                {
                    A = "text1",
                    B = "text2",
                    C = "text3",
                });
                list1.Add(new Head()
                {
                    A = "text4",
                    B = "text5",
                    C = "text6",
                });
                list1.Add(new Head()
                {
                    A = "text7",
                    B = "text8",
                    C = "text9",
                });
                Values_Body list2 = new Values_Body();
                list2.Add(new Body()
                {
                    Parcial = 1,
                    Total=1,
                    ToAcero = false,

                });
                list2.Add(new Body()
                {
                    Parcial = 10,
                    Total = 10,
                    ToAcero = false,

                });

                var xml = new XElement("XmlSerialize", new[]
                {
                    new XElement("list1", list1.Select(t => new XElement("name", t.A))),
                    new XElement("list1", list1.Select(t => new XElement("long", t.B))),
                    new XElement("list1", list1.Select(t => new XElement("fixable", t.C))),

                    new XElement("list2", list2.Select(t => new XElement("name", t.Parcial))),
                    new XElement("list2", list2.Select(t => new XElement("name", t.Total))),
                    new XElement("list2", list2.Select(t => new XElement("name", t.ToAcero))),
                });
                File.WriteAllText("NewSave.xml", xml.ToString());

xml是:

<XmlSerialize>
  <list1>
    <name>text1</name>
    <name>text4</name>
    <name>text7</name>
  </list1>
  <list1>
    <long>text2</long>
    <long>text5</long>
    <long>text8</long>
  </list1>
  <list1>
    <fixable>text3</fixable>
    <fixable>text6</fixable>
    <fixable>text9</fixable>
  </list1>
  <list2>
    <parcial>1</parcial>
    <parcial>10</parcial>
  </list2>
  <list2>
    <total>1</total>
    <total>10</total>
  </list2>
  <list2>
    <acero>false</acero>
    <acero>false</acero>
  </list2>
</XmlSerialize>

1 个答案:

答案 0 :(得分:0)

要将对象实例序列化为xml,您可以执行以下操作:

var x = new XmlSerializer(typeof(Values_Body));
var document = new XDocument();
{
    using (var writer = document.CreateWriter())
    {
        x.Serialize(writer, new Values_Body
        {
            ToAcero = true,
            Parcial = 1000,
            Total = 2000
        });
    }
    // example method, which displays the document in Linqpad
    // https://www.linqpad.net
    document.Dump();
}

会给你

<Values_Body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <ToAcero>true</ToAcero>
  <Parcial>1000</Parcial>
  <Total>2000</Total>
</Values_Body>

要回读对象,您可以使用x.Deserialize...

要更改xml结构,例如使用序列化属性。

[XmlRoot("MyValues")]
public class Values_Body
{    
    private bool _ToAcero;
    [XmlElement("TOACEROITEM")]
    public bool ToAcero
    {
        ...

会将写入/读取的xml更改为:

<MyValues xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <TOACEROITEM>true</TOACEROITEM>
  <Parcial>1000</Parcial>
  <Total>2000</Total>
</MyValues>