将XML序列化为文件

时间:2018-06-10 21:58:25

标签: c# xml serialization

我有以下课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Super
{

    [XmlRoot(ElementName = "V")]
    public class V
    {
        [XmlAttribute(AttributeName = "value")]
        public string Value { get; set; }
        [XmlAttribute(AttributeName = "value2")]
        public string Value2 { get; set; }
    }

    [XmlRoot(ElementName = "L")]
    public class L
    {
        [XmlAttribute(AttributeName = "item")]
        public string item { get; set; }
        [XmlAttribute(AttributeName = "upc")]
        public string Upc { get; set; }
        [XmlAttribute(AttributeName = "decimals")]
        public string decimals { get; set; }
        [XmlAttribute(AttributeName = "prod")]
        public string Prod { get; set; }
        [XmlAttribute(AttributeName = "Cclass")]
        public string Cclass { get; set; }
    }

    [XmlRoot(ElementName = "P_B")]
    public class P_B
    {
        [XmlAttribute(AttributeName = "pb")]
        public string pb { get; set; }
        [XmlAttribute(AttributeName = "udf_value")]
        public string Udf_value { get; set; }
    }

    [XmlRoot(ElementName = "W")]
    public class W
    {
        [XmlElement(ElementName = "P_B")]
        public List<P_B> P_B { get; set; }
    }

    [XmlRoot(ElementName = "P_PRICE")]
    public class P_PRICE
    {
        [XmlAttribute(AttributeName = "lvl")]
        public string lvl { get; set; }
        [XmlAttribute(AttributeName = "price")]
        public string Price { get; set; }
        [XmlAttribute(AttributeName = "A_req")]
        public string A_req { get; set; }
        [XmlAttribute(AttributeName = "season")]
        public string Season { get; set; }
        [XmlAttribute(AttributeName = "season")]
        public string season { get; set; }
    }

    [XmlRoot(ElementName = "P_R")]
    public class P_R
    {
        [XmlElement(ElementName = "P_PRICE")]
        public P_PRICE P_PRICE { get; set; }
    }

    [XmlRoot(ElementName = "P_A")]
    public class P_A
    {
        [XmlAttribute(AttributeName = "store")]
        public string Store { get; set; }
        [XmlAttribute(AttributeName = "A")]
        public string A { get; set; }
        [XmlAttribute(AttributeName = "m")]
        public string M { get; set; }
        [XmlAttribute(AttributeName = "ma")]
        public string Ma { get; set; }
        [XmlAttribute(AttributeName = "transfer")]
        public string Transfer { get; set; }
        [XmlAttribute(AttributeName = "Oout")]
        public string Oout { get; set; }
    }

    [XmlRoot(ElementName = "P_AS")]
    public class P_AS
    {
        [XmlElement(ElementName = "P_A")]
        public List<P_A> P_A { get; set; }
    }

    [XmlRoot(ElementName = "P_T")]
    public class P_T
    {
        [XmlAttribute(AttributeName = "came_from")]
        public string Came_from { get; set; }
    }

    [XmlRoot(ElementName = "P")]
    public class P
    {
        [XmlElement(ElementName = "W")]
        public W W { get; set; }
        [XmlElement(ElementName = "P_YY")]
        public string P_YY { get; set; }
        [XmlElement(ElementName = "P_R")]
        public P_R P_R { get; set; }
        [XmlElement(ElementName = "P_AS")]
        public P_AS P_AS { get; set; }
        [XmlElement(ElementName = "P_T")]
        public P_T P_T { get; set; }
        [XmlElement(ElementName = "D")]
        public string D { get; set; }
        [XmlElement(ElementName = "P_G")]
        public string P_G { get; set; }
        [XmlAttribute(AttributeName = "no")]
        public string no { get; set; }

    }

    [XmlRoot(ElementName = "XY")]
    public class XY
    {
        [XmlElement(ElementName = "V")]
        public V V { get; set; }
        [XmlElement(ElementName = "L")]
        public L L { get; set; }
        [XmlElement(ElementName = "P")]
        public P P { get; set; }
    }

    [XmlRoot(ElementName = "ITEMS")]
    public class ITEMS
    {
        [XmlElement(ElementName = "XY")]
        public List<XY> XY { get; set; }
    }

    [XmlRoot(ElementName = "SETUP")]
    public class SETUP
    {
        [XmlElement(ElementName = "ITEMS")]
        public ITEMS ITEMS { get; set; }
    }



}

我正在尝试使用以下方法序列化此数据:

SETUP f = new SETUP();
f.ITEMS.XY = new List<XY>();

    using(var stream = new FileStream(Directory.GetCurrentDirectory() + "\\file.xml", FileMode.Create))
                {
                    XmlSerializer X = new XmlSerializer(typeof(SETUP));
                    X.Serialize(stream, X);
                }

然而,这是错误的,我尝试了其他的变化,但仍然不成功。

我已经google了,看过多个stackoverflow帖子,但我似乎无法将其写入我给的文件。

我得到的错误是:

  

无法转换'System.Xml.Serialization.XmlSerializer'类型的对象   输入'tool.SETUP'。

就像我指出的那样,我应该使用“f”而不是X我改变了它,但是当我尝试序列化时,我已经失去了它的记录:

SETUP f = new SETUP(); ITEMS x = new ITEMS(); List<XY> u = new List<XY>(); u.Add(new XY() { });

是不是正确的用法我得到一个错误,基本上我无法设置值..我将基于stackoverflow上的示例 -

“有一些无效的参数”是确切的错误

如果我简化了类A LOT,如果只有少数几个元素,那么它有效,但是由于存在嵌套元素,这可能会导致问题?是不是可能实例化这个级别的xml?

1 个答案:

答案 0 :(得分:0)

请尝试以下操作:

        static void Main(string[] args) {
            Super.SETUP super = new Super.SETUP() {
                ITEMS = new Super.ITEMS() {
                    XY = new List<Super.XY>() {
                       new Super.XY() { L = new Super.L() { Cclass = "ABC"}}
                   }
                }
            };


            using (var stream = new FileStream(Directory.GetCurrentDirectory() + "\\file.xml", FileMode.Create))
            {
                XmlSerializer X = new XmlSerializer(typeof(Super.SETUP));
                X.Serialize(stream, super);
            }
        }