未将XML数据加载到c#类中
我试图将XML文件中的数据加载到c#类中,但没有在通知中加载数据。类的其余部分(未显示)已正确填充,因此我假设我的类定义不正确。有人能解释一下吗未将XML数据加载到c#类中,c#,xml,C#,Xml,我试图将XML文件中的数据加载到c#类中,但没有在通知中加载数据。类的其余部分(未显示)已正确填充,因此我假设我的类定义不正确。有人能解释一下吗 public partial class ISTimetables { [XmlElement] public List<ISNotification> Notifications { get; set; } } [Serializable()] public partial class ISNotification {
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
}
public分部类
{
[XmlElement]
公共列表通知{get;set;}
}
[可序列化()]
公共部分类通知
{
公共信息通报()
{
On=新列表();
通知=新列表();
}
[XmlElement]
{get;set;}上的公共列表
[XmElement]
公共列表通知{get;set;}
}
[可序列化()]
公共部分类ISNotify
{
公共字符串电子邮件{get;set;}
公共字符串SimpleEmail{get;set;}
公共字符串SMS{get;set;}
}
[可序列化()]
公共枚举ISProcessStep
{
[XmlEnum(“计算”)]
计算
[XmlEnum(“已报告”)]
报道,
[XmlEnum(“客户批准”)]
客户批准,
[XmlEnum(“拒绝”)]
拒绝
}
我尝试加载的数据如下所示:
<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>
计算
报道
客户认可
试试这个
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
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.IO;
使用System.Xml.Serialization;
命名空间控制台应用程序21
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XmlSerializer serializer=新的XmlSerializer(typeof(istiteMetables));
XmlTextReader=新的XmlTextReader(文件名);
ISTimetables tables=(ISTimetables)序列化程序。反序列化(读取器);
}
}
[XmlRoot(“通知”)]
公共部分类
{
[XmlElement(“通知”)]
公共列表通知{get;set;}
}
[XmlRoot(“通知”)]
公共部分类通知
{
公共信息通报()
{
On=新列表();
通知=新列表();
}
[XmlElement]
{get;set;}上的公共列表
[XmlElement]
公共列表通知{get;set;}
}
[可序列化()]
公共部分类ISNotify
{
公共字符串电子邮件{get;set;}
公共字符串SimpleEmail{get;set;}
公共字符串SMS{get;set;}
}
[可序列化()]
公共枚举ISProcessStep
{
[XmlEnum(“计算”)]
计算
[XmlEnum(“已报告”)]
报道,
[XmlEnum(“客户批准”)]
客户批准,
[XmlEnum(“拒绝”)]
拒绝
}
}