简体   繁体   English

尝试获取 XML 值并与字符串值 C# 进行比较

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

Hi I'm a little bit lost with xml.嗨,我对 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".我尝试从位于我的 github 上的 xml 中获取一个值,然后我想将该值与“LPEVersionCust”的字符串值进行比较。

On github I add a xml file to my repo在 github 上,我将一个 xml 文件添加到我的 repo

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

On my winforms (MainForm.cs)在我的 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.在您的代码中,您在foreach循环之外不存在的范围内定义了LPEVersion

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.您需要在尝试使用它的同一范围内定义LPEVersion


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. string LPEVersion变量的作用域是 foreach 循环。

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.请注意,您可能不想每次迭代都解析LPEInstVersionCust

C# has a System.Version API that you can use for parsing version string values and comparing them. C# 有一个System.Version API,可用于解析版本字符串值并进行比较。

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.只要您确定node.Attributes[1].Value是定义一个版本号,它就可以工作。 If not, you may want to use Version.TryParse instead to ensure that the version number is correct.如果不是,您可能希望使用Version.TryParse来确保版本号正确。

I found a way我找到了一个方法

What I did and it works...我做了什么,它起作用了......

I changed my xml file to:我将我的 xml 文件更改为:

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

And in MainForm.cs在 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!感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM