简体   繁体   中英

Deserialize XML document to object implementing IEnumerable / IEnumerable<T>

I have a XML file like that:

<?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:

[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. So i can not iterate through the array using foreach. How can i implement IEnumerable to my UserData class?

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? Is there also a way to implement IEnumerable, because finally i would like to deserialize to a List instead of an array of users.

Users[] already implements IEnumerable , so you don't have to make your own implementation. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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