【问题标题】:Serializing XML to file将 XML 序列化为文件
【发布时间】:2018-11-20 03:43:00
【问题描述】:

我有以下课程:

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);
                }

但是它给出了错误,我尝试了其他变体但仍然不成功。

我已经用谷歌搜索并看到了多个 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 上的示例 -

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

如果我简化了很多类,如果只有几个元素,它就可以工作,但是由于有嵌套元素,这可能会导致问题吗?是不是不可能实例化这一层的xml?

【问题讨论】:

  • 你确定这个:X.Serialize(stream, X);你想在哪里使用f:)
  • 天哪...应该是f....
  • SETUP f = new SETUP();项目 x = 新项目();列表 u = 新列表(); u.Add(新 XY() { });使用不当我遇到错误,基本上我无法设置值..我将基于 stackoverflow 上的示例
  • 您应该编辑问题,而不是将更新作为 cmets :)
  • I'm getting an error, basically i can't set the values. 提问时不要分享太多信息。人们可以滥用它:)

标签: c# xml serialization


【解决方案1】:

尝试以下:

        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);
            }
        }

【讨论】:

  • 这成功了……这帮助我理解了序列化 xmls……其他例子很简单,这个让我有点失望,但这太棒了!谢谢!!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 2016-01-23
相关资源
最近更新 更多