简体   繁体   中英

Trying to get a XML value and compare with a string value C#

Hi I'm a little bit lost with xml.

I try to get a value from an xml located on my github, then I want to compare the value with the string value of "LPEVersionCust".

On github I add a xml file to my repo

<?xml version="1.0" encoding="utf-8"?>
<Definition>
        <Version name="0.0.2" />
</Definition>

On my winforms (MainForm.cs)

private string LPEVersionCust = "0.0.1";
private string LPEUrlPathX = "https://raw.githubusercontent.com/username/myrepo/main/lpeCL.xml"; //not the real address

So I try:

using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Reflection;

namespace LPEApp
{
    public partial class mainApp : Form
    {
        private string LPEUrlPathX = "https://raw.githubusercontent.com/username/myrepo/main/lpeCL.xml";
        private string LPEInstVersionCust= "0.0.1";
        
        
        public mainApp()
        {
            InitializeComponent();
        }
        
        private void mainApp_Load(object sender, EventArgs e)
        {
            GetUpdateLPEVersion();
        }
        
        private void GetUpdateLPEVersion()
        {
            try
            {
                XmlDocument GetLPEVersion = new XmlDocument();
                GetLPEVersion.Load(LPEUrlPathX);
                XmlNodeList lpnodes = GetLPEVersion.DocumentElement.SelectNodes("/Definition");
                
                foreach (XmlNode node in lpnodes)
                {
                    string LPEVersion = node.Attributes[1].Value;
                }
                double LPEVersionCust = double.Parse(LPEInstVersionCust);

                if (LPEVersion > LPEVersionCust)
                {
                    // Error message CS0103 : The name 'LPEVersion' does not exist in the current context

                }

            }
            catch (Exception)
            { }

        }
   }
}

In your code you have defined LPEVersion in a scope that doesn't exist outside of the foreach loop.

It's like writing this:

{
    string name = "Foo";
}

Console.WriteLine(name);

You get the same error.

You need the definition of LPEVersion in the same scope you're trying to use it in.


You probably need some sort of code like this to do your comparison of versions:

private void GetUpdateLPEVersion()
{
    string versionText = XDocument.Load(LPEUrlPathX).Root?.Element("Version")?.Attribute("name")?.Value;
    if (Version.TryParse(versionText, out Version version))
    {
        var custom = Version.Parse(LPEInstVersionCust);
        if (version.CompareTo(custom) == 1)
        {
            // You have a newer version.
        }
    }
}

The string LPEVersion variable is scoped to the foreach loop.

foreach (XmlNode node in lpnodes)
{
    string LPEVersion = node.Attributes[1].Value;
    double LPEVersionCust = double.Parse(LPEInstVersionCust);
    if (LPEVersion > LPEVersionCust)
    {
        //Do something
    }
}

This should work.

Note you probably don't want to parse LPEInstVersionCust every iteration.

C# has a System.Version API that you can use for parsing version string values and comparing them.

You can alter your current application to do this.

using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Reflection;

namespace LPEApp
{
    public partial class mainApp : Form
    {
        private string LPEUrlPathX = "https://raw.githubusercontent.com/username/myrepo/main/lpeCL.xml";
        private Version LPEInstVersion = new Version("0.0.1");

        public mainApp()
        {
            InitializeComponent();
        }

        private void mainApp_Load(object sender, EventArgs e)
        {
            GetUpdateLPEVersion();
        }

        private void GetUpdateLPEVersion()
        {
            try
            {
                XmlDocument GetLPEVersion = new XmlDocument();
                GetLPEVersion.Load(LPEUrlPathX);
                XmlNodeList lpnodes = GetLPEVersion.DocumentElement.SelectNodes("/Definition");

                foreach (XmlNode node in lpnodes)
                {
                    Version LPEVersion = new Version(node.Attributes[1].Value);

                    if (LPEVersion > LPEInstVersion)
                    {
                        // Do thing
                    }
                }
            }
            catch (Exception) { }
        }
    }
}

This works as long as you are certain that the node.Attributes[1].Value is definition a version number. If not, you may want to use Version.TryParse instead to ensure that the version number is correct.

I found a way

What I did and it works...

I changed my xml file to:

<?xml version="1.0" encoding="utf-8"?>
<LPEDefinition>
    <Version>0.0.1</Version>
</LPEDefinition>
    

And in MainForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Xml.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Configuration;

namespace LPEApp
{
    public partial class mainApp : Form
    {
        private string LPEInstVersionCust = "0.0.0";
        private string LPEVersionAvailable = "";
        
        public mainApp()
        {
            InitializeComponent();
        }

        private void mainApp_Load(object sender, EventArgs e)
        {
            GetUpdateLPEVersion();
        }

        private void GetUpdateLPEVersion()
        {
            XmlDocument GetLPEVersion = new XmlDocument();
            GetLPEVersion.Load("https://raw.githubusercontent.com/.../.../main/lpeCL.xml");
            XmlNode root = GetLPEVersion.DocumentElement.SelectSingleNode("/LPEDefinition");
            foreach (XmlNode modXml in root.ChildNodes)
            {
                string ver = modXml.Attributes["name"].InnerText;
                LPEVersionAvailable = ver;
                
                if (LPEVersionAvailable != LPEInstVersionCust)
                {
                    DialogResult dr = MessageBox.Show("There is a new version available....", "... is out dated", MessageBoxButtons.YesNo);
                    if (dr == System.Windows.Forms.DialogResult.Yes)
                    {
                        Process.Start("https://github.com/.../.../releases/latest");
                    }
                }
            }
        }
    }
}

Thanks for your helps!

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