简体   繁体   English

将XML文档反序列化为实现IEnumerable / IEnumerable的对象<T>

[英]Deserialize XML document to object implementing IEnumerable / IEnumerable<T>

I have a XML file like that: 我有一个像这样的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<UserData>
    <Users>
        <User>
            <Name>TestName1</Name>
            <Password>Password1</Password>
            <Level>1</Level>
        </User>
        <User>
            <Name>TestName2</Name>
            <Password>PAssword2</Password>
            <Level>2</Level>
        </User>
    </Users>
</UserData>

Based on that XML structure i deserialize it using this two classes: 基于该XML结构,我使用以下两个类对其进行反序列化:

[Serializable()]
public sealed class User
{
    [System.Xml.Serialization.XmlElement("Name")]
    public string Name { get; set; }

    [System.Xml.Serialization.XmlElement("Password")]
    public string Password { get; set; }

    [System.Xml.Serialization.XmlElement("Level")]
    public string Level { get; set; }
}

[System.Xml.Serialization.XmlRoot("UserData")]
public class UserData
{
    [XmlArray("Users")]
    [XmlArrayItem("User", typeof(User))]
    public User[] Users { get; set; }
}

The implementation for the deserialization looks like: 反序列化的实现如下所示:

private void ParseXMLCfg()
{
    StreamReader reader = new StreamReader(_cfgFileDir);
    try
    {
        XmlSerializer serializer = new XmlSerializer(typeof(UserData));
        _users = (UserData)serializer.Deserialize(reader);
    }
    catch (InvalidOperationException e)
    {
        _logger.DebugFormat("{0}", e);
    }

    reader.Close();

   }

Everything works fine so far. 到目前为止一切正常。 But my problem is that my class does not implement IEnumerable. 但是我的问题是我的类没有实现IEnumerable。 So i can not iterate through the array using foreach. 所以我不能使用foreach遍历数组。 How can i implement IEnumerable to my UserData class? 如何对我的UserData类实现IEnumerable?

One of my approaches was like that: 我的方法之一是这样的:

   [System.Xml.Serialization.XmlRoot("UserData")]
    public class UserData:IEnumerable
    {
        [XmlArray("Users")]
        [XmlArrayItem("User", typeof(User))]
        public User[] Users { get; set; }

        // IEnumerable Member
        public IEnumerator GetEnumerator()
        {
            foreach (object o in Users)
            {
                if(o == null)
                {
                    break;
                }
                yield return o;
            }
        }
    }

But it does not work. 但这行不通。

What would be the way to implement IEnumerable for this class? 为此类实现IEnumerable的方法是什么? Is there also a way to implement IEnumerable, because finally i would like to deserialize to a List instead of an array of users. 还有一种实现IEnumerable的方法,因为最后我想反序列化为List而不是用户数组。

Users[] already implements IEnumerable , so you don't have to make your own implementation. Users[]已经实现了IEnumerable ,因此您不必自己进行实现。 Just call the one of the underlying object: 只需调用基础对象之一:

// IEnumerable Member
public IEnumerator GetEnumerator()
{
    return Users.GetEnumerator();
}

While you're at it, it's better to implement the generic version of IEnumerable : 当您使用它时,最好实现IEnumerable的通用版本:

[System.Xml.Serialization.XmlRoot("UserData")]
public class UserData : IEnumerable<User>
{
    [XmlArray("Users")]
    [XmlArrayItem("User", typeof(User))]
    public User[] Users { get; set; }

    // IEnumerable Member
    public IEnumerator GetEnumerator()
    {
        return this.Users.GetEnumerator();
    }

    IEnumerator<User> IEnumerable<User>.GetEnumerator()
    {
        return ((IEnumerable<User>)this.Users).GetEnumerator();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM