c# command 讀取 XML 能對應嗎?!

xml檔內容如下:

<?xml version="1.0" encoding="GB2312" ?> <Root><name>Jack</name><aname>text</aname><name>Jack2</name><aname>text2</aname><name>Jack3</name><aname>text3</aname></Root>

---------------------------------------

private void button1_Click_1(object sender, EventArgs e)

{

string FileName = "XMLTest.xml";

if (System.IO.File.Exists(FileName))

{

comboBoxTEXT.Items.Clear();

comboBoxTEXT1.Items.Clear();

checkupdate.Enabled = true;

}

else

{

comboBoxTEXT.Items.Clear();

comboBoxTEXT1.Items.Clear();

checkupdate.Enabled = false;

}

try

{

XmlDocument doc = new XmlDocument();

XmlDocument docs = new XmlDocument(); doc.Load(Application.StartupPath + "\\XMLTest.xml"); //讀取的xml文件路徑

docs.Load(Application.StartupPath + "\\XMLTest.xml"); //讀取的xml文件路徑

XmlNodeList node = doc.GetElementsByTagName("name");

XmlNodeList nodes = docs.GetElementsByTagName("aname"); for (int a = 0; a < nodes.Count; a++)

{

comboBoxTEXT1.Items.Add(nodes[a].InnerText);

{

comboBoxTEXT1.SelectedIndex = 0;

}

}

for (int i = 0; i < node.Count; i++)

{

comboBoxTEXT.Items.Add(node[i].InnerText); }

comboBoxTEXT.SelectedIndex = 0;

} catch (Exception ge)

{

MessageBox.Show("啟動自動升級檢查程序失敗" + FileName + " 檔案不存在" + ge.Message);

}

----------------------------------------------------------

private void comboBoxTEXT_SelectedIndexChanged(object sender, EventArgs e)

{ SERVER_URL.Text = "http://dragonpg.myweb.hinet.net/";

RegistryKey rk = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\LuckerSoft\AutoUpdate");

string Server_URL1 = rk.GetValue("ServerURL", "http://dragonpg.myweb.hinet.net/").ToString();

SERVER_URL.Text = SERVER_URL.Text + comboBoxTEXT.Items[comboBoxTEXT.SelectedIndex].ToString();

}

}

}

已更新項目:

當我comboBoxTEXT 下拉顯示

Jack

Jack2

Jack3

當我comboBoxTEXT1 下拉顯示

text

text2

text3

由於我這兩個都是分別讀取name 和 aname 節點我該怎麼讓comboBoxTEXT選擇Jack

comboBoxTEXT1 換自動選擇對應text選項,我要的不是單純的對應,因為我檔案是讀取xml檔的內容,所以我在修改xml內容也會更變...簡單的說 我該怎麼讓comboBoxTEXT選擇第一個選項

comboBoxTEXT1就自動對應第一個選項 以此類推...拜託大大幫小弟解答^^

1 個解答

評分
  • 9 年前
    最佳解答

    this works for me:

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void Form1_Load( object sender, EventArgs e )

    {

    var filename = "../../" + "Test.xml";

    if ( System.IO.File.Exists(filename) )

    {

    this.comboBoxTEXT.Items.Clear();

    this.comboBoxTEXT1.Items.Clear();

    XmlDocument doc = new XmlDocument();

    doc.Load(filename);

    var jackList = doc.SelectNodes("Root/name");

    foreach ( var n in jackList )

    {

    this.comboBoxTEXT.Items.Add(( (XmlNode)n ).InnerText);

    }

    var textList = doc.SelectNodes("Root/aname");

    foreach ( var t in textList )

    {

    this.comboBoxTEXT1.Items.Add(( (XmlNode)t ).InnerText);

    }

    }

    }

    private void comboBoxTEXT_SelectedIndexChanged( object sender, EventArgs e )

    {

    this.comboBoxTEXT1.SelectedIndex = this.comboBoxTEXT.SelectedIndex;

    }

    }

    I was using Visual Studio 2010 sp1

    • Commenter avatar登入以對解答發表意見
還有問題?馬上發問,尋求解答。