Click here to Skip to main content
Sign Up to vote bad
good
Hi everyone!
 
I am trying to compare XmlTextReader values to determine which is greater in value.
 
This is the code I have so far:
 
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: 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 15 Nov '12 - 8:53
Edited 15 Nov '12 - 9:11
ridoy22.7K


2 solutions

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
  Permalink  
Comments
Espen Harlinn - 15 Nov '12 - 19:41
5'ed! Good points :-D
Sergey Alexandrovich Kryukov - 15 Nov '12 - 22:04
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.
  Permalink  
Comments
Espen Harlinn - 15 Nov '12 - 19:40
5'ed!

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 414
1 Arun Vasu 223
2 OriginalGriff 190
3 CPallini 163
4 Aarti Meswania 158
0 Sergey Alexandrovich Kryukov 10,169
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid