[英]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.