Click here to Skip to main content
12,396,047 members (64,576 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# XML XmlReader VS2012
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:53am
Updated 15-Nov-12 9:11am
ridoy57.9K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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:41pm
   
5'ed! Good points :-D
Sergey Alexandrovich Kryukov 15-Nov-12 22:04pm
   
Thank you, Espen.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:40pm
   
5'ed!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100