c#xml将节点的子节点反序列化为对象列表

时间:2014-02-05 12:08:50

标签: c# xml xml-serialization

我在XmlDocument c#object

中有以下xml
    <CR2CmdAck RequestID="DD477311-7129-4FE0-8E14-AC4AFA995DBC" ErrorCode="0">
        <CR2Rules>
            <Rule RuleID="53" CallType="All" TargetType="All" DurationType="From" EffectiveFrom="2013-11-15" Tag="All targets, all calls, From" Status="Active"/>
            <Rule RuleID="56" CallType="All" TargetType="All" DurationType="Calls" EffectiveFrom="2013-11-13" CallCount="2" Tag="All targets, all calls, 100 Calls" Status="Active"/>
            <Rule RuleID="58" CallType="All" TargetType="Agent" DurationType="Calls" AgentID="1595" EffectiveFrom="2014-01-29" CallCount="10" Status="Active"/>
            <Rule RuleID="59" CallType="All" TargetType="Agent" DurationType="From" AgentID="1595" EffectiveFrom="2014-01-29" Tag="" Status="Active"/>
            <Rule RuleID="60" CallType="Inbound" TargetType="Agent" DurationType="Calls" AgentID="1615" EffectiveFrom="2014-01-29" CallCount="10" Status="Active"/>
            <Rule RuleID="61" CallType="All" TargetType="Agent" DurationType="Calls" AgentID="9954" EffectiveFrom="2014-01-29" CallCount="10" Tag="" Status="Active"/>
            <Rule RuleID="62" CallType="All" TargetType="AgentQ" DurationType="Calls" QueueID="247" EffectiveFrom="2014-01-29" CallCount="10" Tag="" Status="Active"/>
            <Rule RuleID="63" CallType="Inbound" TargetType="Agent" DurationType="Between" AgentID="2161" EffectiveFrom="2014-01-29" EffectiveTo="2014-02-05" Tag="" Status="Active"/>
        </CR2Rules>
    </CR2CmdAck>

我正在尝试将此xml反序列化为c#对象

课程如下

[DataContract]
[Serializable]
[XmlInclude(typeof(SavedRule))]
public class Rule
{
    [DataMember]
    [XmlElement("TargetType")]
    public virtual string TargetType { get; set; }

    [DataMember]
    [XmlElement("CallType")]
    public string CallType { get; set; }

    [DataMember]
    [XmlElement("EffectiveFrom")]
    public DateTime EffectiveFrom { get; set; }

    [DataMember]
    [XmlElement(ElementName = "EffectiveTo", IsNullable = true)]
    public DateTime? EffectiveTo { get; set; }

    [DataMember]
    [XmlElement(ElementName = "CallCount", IsNullable = true)]
    public int? CallCount { get; set; }

    [DataMember]
    [XmlElement("Tag")]
    public string Tag { get; set; }
}

[XmlRoot("Rule")]
[Serializable]
public class SavedRule : Rule
{
    private string _targetType;

    [XmlElement("RuleID")]
    public int ID { get; set; }

    [XmlChoiceIdentifier("TargetIDName")]
    [XmlElement(ElementName = "AgentID", IsNullable = true), XmlElement(ElementName = "QueueID",  IsNullable = true)]
    public int? TargetID { get; set; }

    [XmlElement("Status")]
    public string Status { get; set; }

    [XmlIgnore] public TargetIDNameSelector TargetIDName;

    public override string TargetType
    {
        get { return _targetType; }
        set
        {
            _targetType = value;
            switch (_targetType)
            {
                case "Agent":
                {
                    TargetIDName = TargetIDNameSelector.AgentID;
                }
                    break;
                case "AnonQ":
                case "AgentQ":
                {
                    TargetIDName = TargetIDNameSelector.QueueID;
                }
                    break;
                default:
                {
                    TargetIDName = TargetIDNameSelector.None;
                }
                    break;
            }
        }
    }
}

public enum TargetIDNameSelector
{
    None,
    AgentID,
    QueueID
}

请告诉我如何将<CR2Rules>节点的内容去世List<SavedRule>

提前致谢

1 个答案:

答案 0 :(得分:0)

如何使用Linq To Xml

var rules = XDocument.Load(filename)
            .Descendants("Rule")
            .Select(x => x.Attributes().ToDictionary(e => e.Name, e => e.Value))
            .ToList();

foreach(var rule in rules)
{
    Console.WriteLine(rule["EffectiveFrom"]);
}

var rules = XDocument.Load(filename)
                .Descendants("Rule")
                .Select(r => new SavedRule
                {
                    CallType = (string)r.Attribute("CallType"),
                    EffectiveFrom = (DateTime)r.Attribute("EffectiveFrom"),
                    EffectiveTo = (DateTime?)r.Attribute("EffectiveTo"),
                    ID = (int)r.Attribute("RuleID"),
                    Status = (string)r.Attribute("Status"),
                    TargetID = (int?)r.Attribute("TargetID"),
                    TargetType = (string)r.Attribute("TargetType"),
                    CallCount = (int?)r.Attribute("CallCount"),
                    Tag = (string)r.Attribute("Tag")
                })
                .ToList();