嗨,我對 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)
{ }
}
}
}
uj5u.com熱心網友回復:
該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每次迭代。
uj5u.com熱心網友回復:
C# 有一個System.VersionAPI,可用于決議版本字串值并進行比較。
您可以更改您當前的應用程式來執行此操作。
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改用以確保版本號正確。
uj5u.com熱心網友回復:
在您的代碼中,您已在回圈LPEVersion之外不存在的范圍內定義。foreach
就像這樣寫:
{
string name = "Foo";
}
Console.WriteLine(name);
你得到同樣的錯誤。
LPEVersion您需要在嘗試使用它的同一范圍內定義 of 。
您可能需要類似這樣的代碼來比較版本:
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.
}
}
}
uj5u.com熱心網友回復:
我找到了一個方法
我做了什么,它起作用了......
我將我的 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");
}
}
}
}
}
}
感謝您的幫助!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/482435.html
