繁体   English   中英

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

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

嗨,我对 xml 有点迷茫。

我尝试从位于我的 github 上的 xml 中获取一个值,然后我想将该值与“LPEVersionCust”的字符串值进行比较。

在 github 上,我将一个 xml 文件添加到我的 repo

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

在我的 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

所以我尝试:

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)
            { }

        }
   }
}

在您的代码中,您在foreach循环之外不存在的范围内定义了LPEVersion

就像这样写:

{
    string name = "Foo";
}

Console.WriteLine(name);

你得到同样的错误。

您需要在尝试使用它的同一范围内定义LPEVersion


您可能需要类似这样的代码来比较版本:

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.
        }
    }
}

string LPEVersion变量的作用域是 foreach 循环。

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

这应该有效。

请注意,您可能不想每次迭代都解析LPEInstVersionCust

C# 有一个System.Version API,可用于解析版本字符串值并进行比较。

您可以更改您当前的应用程序来执行此操作。

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) { }
        }
    }
}

只要您确定node.Attributes[1].Value是定义一个版本号,它就可以工作。 如果不是,您可能希望使用Version.TryParse来确保版本号正确。

我找到了一个方法

我做了什么,它起作用了......

我将我的 xml 文件更改为:

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

在 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");
                    }
                }
            }
        }
    }
}

感谢您的帮助!

暂无
暂无

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

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