Click here to Skip to main content
15,868,002 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi everyone!

I am trying to compare XmlTextReader values to determine which is greater in value.

This is the code I have so far:

C#
int newVersion1 = 0;
            string xmlUrl1 = @"C:\Users\Programming\Desktop\update.xml";
            XmlTextReader reader1 = null;

            int newVersion2 = 0;
            string xmlUrl2 = @"C:\Users\Programming\Desktop\update2.xml";
            XmlTextReader reader2 = null;

            try
            {
                reader1 = new XmlTextReader(xmlUrl1);
                reader1.MoveToContent();
                string elementName = "";
                if ((reader1.NodeType == XmlNodeType.Element) && (reader1.Name == "sqldbfile"))
                {                    
                    while (reader1.Read())
                    {
                        if (reader1.NodeType == XmlNodeType.Element)
                        {                                                     
                            elementName = reader1.Name;
                        }
                        else
                        {                            
                            if (reader1.NodeType == XmlNodeType.Text)
                            {                                
                                switch (elementName)
                                {
                                    case "version":
                                        newVersion1 = Convert.ToInt32(reader1.Value);
                                        break;                                    
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (reader1 != null)
                {
                    reader1.Close();
                }
            }
            //////////////////////////////////////////////////////////////////////////////////////
            try
            {
                reader2 = new XmlTextReader(xmlUrl2);
                reader2.MoveToContent();
                string elementName = "";
                if (reader2.NodeType == XmlNodeType.Element)
                {
                    while (reader2.Read())
                    {
                        if ((reader2.NodeType == XmlNodeType.Element) && (reader2.Name == "sqldbfile"))
                        {
                            elementName = reader2.Name;
                        }
                        else
                        {
                            if (reader2.NodeType == XmlNodeType.Text)
                            {
                                switch (elementName)
                                {
                                    case "version":
                                        newVersion2 = Convert.ToInt32(reader2.Value);                                        
                                        break;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (reader2 != null)
                {
                    reader2.Close();
                }
            }

            //string s1 = reader1.Value;
            //string s2 = reader2.Value;

            MessageBox.Show("reader 1 : " + reader1.Value);
            MessageBox.Show("reader 2 : " + reader2.Value);

Current Output: The MessageBoxes at the bottom are not presenting any output for reader1.value or reader2.value.

Intended Output:
The XML files have: <version>2.0 in each of the 2 files. I am wanting to compare these 2 to determine whether or not a program is up to date. I should be getting values for reader1.value and reader2.value.

The error I'm getting is "Input String is not in correct format!" Does anyone know what I'm missing?
Posted
Updated 15-Nov-12 9:11am
v2

Convert?! What's wrong with System.Xml.XmlReader.ReadElementContentAsInt or System.Xml.XmlReader.ReadContentAsInt? Anyway, if you need to "convert" (right term would be "parse"), you can always use int.Parse or int.TryParse:
http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.int32.parse.aspx[^],
http://msdn.microsoft.com/en-us/library/system.int32.tryparse.aspx[^].

Besides, why are you so sure that your really need to work with XML directly? You could consider serialization, and the best approach to serialization would be using Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

See also my past answers:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 15-Nov-12 19:41pm    
5'ed! Good points :-D
Sergey Alexandrovich Kryukov 15-Nov-12 22:04pm    
Thank you, Espen.
--SA
I am not reading all of this code. However, to convert the string you get from XML to an int, use int.TryParse. Convert.ToInt32 will blow up if it's not an int.
 
Share this answer
 
Comments
Espen Harlinn 15-Nov-12 19:40pm    
5'ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900