XML数据未加载到c#类中

时间:2016-10-20 15:58:50

标签: c# xml

我正在尝试将XML文件中的数据加载到c#类中,但是没有在Notifications中加载数据。正确填充了类的其余部分(未显示),因此我假设我的类定义不正确。任何人都可以对此有所了解吗?

public partial class ISTimetables
{
    [XmlElement]
    public List<ISNotification> Notifications { get; set; }
}

[Serializable()]
public partial class ISNotification
{
    public ISNotification()
    {
        On = new List<ISProcessStep>();
        Notify = new List<ISNotify>();
    }

    [XmlElement]
    public List<ISProcessStep> On { get; set; }

    [XmElement]
    public List<ISNotify> Notify { get; set; }
}

[Serializable()]
public partial class ISNotify
{

    public string Email { get; set; }
    public string SimpleEmail { get; set; }
    public string SMS { get; set; }
}

[Serializable()]

public enum ISProcessStep
{
    [XmlEnum("Calculated")]
    Calculated,
    [XmlEnum("Reported")]
    Reported,
    [XmlEnum("Customer Approved")]
    CustomerApproved,
    [XmlEnum("Rejected")]
    Rejected
}

我尝试加载的数据如下:

<Notifications>
  <Notification>
    <On>Calculated</On>
    <On>Reported</On>
    <Notify SimpleEmail="me@company.com"/>
    <Notify Email="you@company.com"/>
    <Notify SMS="0123456789"/>
  </Notification>

  <Notification>
    <On>Customer Approved</On>
    <Notify Email="him@company.com"/>
  </Notification>
</Notifications>

1 个答案:

答案 0 :(得分:0)

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;

namespace ConsoleApplication21
{
    class Program
    {
        const string FILEName = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(ISTimetables));
            XmlTextReader reader = new XmlTextReader(FILEName);
            ISTimetables tables = (ISTimetables)serializer.Deserialize(reader);

        }
    }
    [XmlRoot("Notifications")]
    public partial class ISTimetables
    {
        [XmlElement("Notification")]
        public List<ISNotification> Notifications { get; set; }
    }

    [XmlRoot("Notification")]
    public partial class ISNotification
    {
        public ISNotification()
        {
            On = new List<ISProcessStep>();
            Notify = new List<ISNotify>();
        }

        [XmlElement]
        public List<ISProcessStep> On { get; set; }

        [XmlElement]
        public List<ISNotify> Notify { get; set; }
    }

    [Serializable()]
    public partial class ISNotify
    {

        public string Email { get; set; }
        public string SimpleEmail { get; set; }
        public string SMS { get; set; }
    }

    [Serializable()]

    public enum ISProcessStep
    {
        [XmlEnum("Calculated")]
        Calculated,
        [XmlEnum("Reported")]
        Reported,
        [XmlEnum("Customer Approved")]
        CustomerApproved,
        [XmlEnum("Rejected")]
        Rejected
    }
}