Link to home
Create AccountLog in
Avatar of bemara57
bemara57

asked on

Why does ReadToNextSibling skip to the end in XmlReader?

I have an XML file that I am reading inner text values from. It is a bunch of users and I am grabbing the values within using ReadToNextSibling. The problem is that if one of the elements doesn't exist, the ReadToNextSibling spins down to the end of the file, but I thought it would've just spin until the end of the parent element. This is how my XML file looks like, my code only returns the first two records, then the beginning of the third, then everything is blank after the password question column.. which means it couldn't find reader.ReadToNextSibling("PasswordQuestion") and skipped down until the end. Can anyone see what's wrong with my code??

<?xml version="1.0"?>
<XmlUsers>
    <User>
        <UserKey>387aefdf-1e04-4a79-a63b-a1ab7bede0f9</UserKey>
        <UserName>admin</UserName>
        <Password>admin</Password>
        <PasswordSalt />
        <Email type="yahoo">admin@test.com</Email>
        <CreationDate>2007-09-21T23:03:09.359375+03:00</CreationDate>
        <LastActivityDate>2007-09-21T23:03:09.859375+03:00</LastActivityDate>
        <LastLoginDate>2007-09-21T23:03:09.859375+03:00</LastLoginDate>
        <LastPasswordChangeDate>2007-09-21T23:03:09.359375+03:00</LastPasswordChangeDate>
        <PasswordQuestion>What is my name?</PasswordQuestion>
        <PasswordAnswer>Admin</PasswordAnswer>
    </User>
    <User>
        <UserKey>d7fdc861-68be-4914-904a-16b60052ae04</UserKey>
        <UserName>member</UserName>
        <Password>member</Password>
        <PasswordSalt />
        <Email type="google">member@test.com</Email>
        <CreationDate>2007-09-21T23:03:31.765625+03:00</CreationDate>
        <LastActivityDate>2007-09-21T23:03:32.140625+03:00</LastActivityDate>
        <LastLoginDate>2007-09-21T23:03:32.140625+03:00</LastLoginDate>
        <LastPasswordChangeDate>2007-09-21T23:03:31.765625+03:00</LastPasswordChangeDate>
        <PasswordQuestion>Who is this?</PasswordQuestion>
        <PasswordAnswer>member</PasswordAnswer>
    </User>
    <User>
        <UserKey>d7fdc861-789mb6v36b60052ae04</UserKey>
        <UserName>member</UserName>
        <Password>member</Password>
        <PasswordSalt />
        <Email type="google">member@test.com</Email>
        <CreationDate>2007-09-21T23:03:31.765625+03:00</CreationDate>
        <LastActivityDate>2007-09-21T23:03:32.140625+03:00</LastActivityDate>
        <LastLoginDate>2007-09-21T23:03:32.140625+03:00</LastLoginDate>
        <LastPasswordChangeDate>2007-09-21T23:03:31.765625+03:00</LastPasswordChangeDate>
    </User>
    <User>
        <UserKey>d7fdc8617n68nm9804a-16b60052ae04</UserKey>
        <UserName>member</UserName>
        <Password>member</Password>
        <PasswordSalt />
        <Email type="google">member@test.com</Email>
        <CreationDate>2007-09-21T23:03:31.765625+03:00</CreationDate>
        <LastActivityDate>2007-09-21T23:03:32.140625+03:00</LastActivityDate>
        <LastLoginDate>2007-09-21T23:03:32.140625+03:00</LastLoginDate>
        <LastPasswordChangeDate>2007-09-21T23:03:31.765625+03:00</LastPasswordChangeDate>
    </User>
<XmlUsers>
using (XmlReader reader = XmlReader.Create(file))
            {
                MembershipUserCollection users = new MembershipUserCollection();
 
                while (reader.Read())
                {
                    string userID = String.Empty;
                    string username = String.Empty;
                    string email = String.Empty;
                    string passwordQuestion = String.Empty;
                    string passwordAnswer = String.Empty;
 
                    if (!reader.ReadToFollowing("User"))
                        break;
 
                    if (reader.ReadToDescendant("UserKey"))
                        userID = reader.ReadElementString("UserKey");
 
                    if (reader.ReadToNextSibling("UserName"))
                        username = reader.ReadElementString("UserName");
 
                    if (reader.ReadToNextSibling("Email"))
                        email = reader.ReadElementString("Email");
 
                    if (reader.ReadToNextSibling("PasswordQuestion"))
                        passwordQuestion = reader.ReadElementString("PasswordQuestion");
 
                    if (reader.ReadToNextSibling("PasswordAnswer"))
                        passwordAnswer = reader.ReadElementString("PasswordAnswer");
 
                    users.Add(new MembershipUser("SiteMembership",
                                                  userID,
                                                  username,
                                                  email,
                                                  passwordQuestion,
                                                  passwordAnswer,
                                                  true,
                                                  false,
                                                  DateTime.Now,
                                                  DateTime.Now,
                                                  DateTime.Now,
                                                  DateTime.Now,
                                                  DateTime.MinValue));
 
 
                }
 
                reader.Close();
 
                return users;
            }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ZachSmith
ZachSmith
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Here's how you would serialize it:
            MembershipUserCollection myCollection = new MembershipUserCollection();
 
            XmlSerializer serializer = new XmlSerializer(typeof(MembershipUserCollection));
 
            using (StreamWriter writer = new StreamWriter(@"C:\mycollection.xml"))
            {
                serializer.Serialize(writer, myCollection);
            }

Open in new window

Here's how to Deserialize it:
            MembershipUserCollection myCollection = null;
 
            XmlSerializer serializer = new XmlSerializer(typeof(MembershipUserCollection));
 
            using (StreamReader reader = new StreamReader(@"C:\mycollection.xml"))
            {
                myCollection = (MembershipUserCollection)serializer.Deserialize(writer);
            }

Open in new window

Avatar of bemara57
bemara57

ASKER

Ah ok that makes a lot of sense. Thanks!
Avatar of Dmitry G
Try this code using XmlDocument, XmlElement etc classes.
You xml document in the file XMLFile1.xml in bin folder
The code is in button handler
            XmlDocument doc = new XmlDocument();
            doc.Load("XMLFile1.xml");
 
            // MembershipUserCollection users = new MembershipUserCollection();
            List<int> l = new List<int>(); // some collection just to check number of users is OK
            XmlNodeList userNodes = doc.GetElementsByTagName("User");
 
            foreach (XmlNode u in userNodes)
            {
                XmlElement userEle = (XmlElement)u;
                string userID = String.Empty;
                string username = String.Empty;
                string email = String.Empty;
                string passwordQuestion = String.Empty;
                string passwordAnswer = String.Empty;
 
                foreach (XmlNode ch in userEle.ChildNodes)
                {
                    XmlElement chEle = (XmlElement)ch;
                    if (chEle.Name == "UserKey")
                        userID = chEle.InnerText;
 
                    if (chEle.Name == "UserName")
                        username = chEle.InnerText;
 
                    if (chEle.Name == "Email")
                        email = chEle.InnerText;
 
                    if (chEle.Name == "PasswordQuestion")
                        passwordQuestion = chEle.InnerText;
 
                    if (chEle.Name == "PasswordAnswer")
                        passwordAnswer = chEle.InnerText;
                }
                //users.Add(new MembershipUser("SiteMembership",
                //              userID,
                //              username,
                //              email,
                //              passwordQuestion,
                //              passwordAnswer,
                //              true,
                //              false,
                //              DateTime.Now,
                //              DateTime.Now,
                //              DateTime.Now,
                //              DateTime.Now,
                //              DateTime.MinValue));
                l.Add(1);
            }
            MessageBox.Show("" + l.Count.ToString());

Open in new window