簡體   English   中英

用不同的名稱讀取 XML 到 Combobox C#

[英]Reading XML with different names to Combobox C#

看起來像一個非常簡單的任務,我對 c# 很陌生,似乎找不到正確的答案

我有以下 xml 結構(元素數量不同)

<config>
<extras>
<dir_update>C:\extension\update.exe</dir_update>
</extras>
<connection_MAIN>
<ip>LOCALHOST,1433</ip>
<bd>DATA</bd>
<user>sa</user>
<password>gzqs=</password>
</connection_MAIN>
<connection_LOBBY>
<ip>10.0.0.2,1433</ip>
<bd>DATA</bd>
<user>sa</user>
<password>I/wqqZIgzqs=</password>
<caixa>5yIz5GPu80s=</caixa>
<printer>cARrmGLQlztLiUDxIJqoPkvJabIiyI9ye4H7t+4muYk=</printer>
</connection_LOBBY>
<connection_FRONT>
<ip>10.0.0.5,1433</ip>
<bd>FIELDS</bd>
<user>sa</user>
<password>I/wqqZIgzqs=</password>
</connection_FRONT>
</config>

我已經在我的 combobox 中獲得了以“connection_”開頭的元素,並且當我在 select 上的連接時,我想要<ip><bd><user><password>中的值

我遇到的問題是它返回的是單詞本身而不是下面代碼中的值

private void Form1_Load(object sender, EventArgs e)
        {
            using(DataSet ds = new DataSet())
            {
                ds.ReadXml(textBox1.Text);
                foreach(DataTable dt in ds.Tables)
                {
                    if (dt.ToString().Contains("conection_"))
                    {
                        comboBox1.Items.Add(dt.ToString().Replace("conection_", ""));
                    }
                }
                comboBox1.SelectedIndex = 0;
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            using(DataSet ds = new DataSet())
            {
                ds.ReadXml(textBox1.Text);
                string v = comboBox1.Text.ToString();
                string ip = ds.Tables[$"conection_{v}"].Columns[0].ToString();
            }
        }

變量 ip 正在獲取值“ip”並且我想要“LOCALHOST,1433”,而在此示例中,當我 select 是我的 combobox 的第一個選項時。

我也想按名稱(“ip”、“bd”)搜索列值,但我似乎只在使用 Columns[0]、Columns[1] 時才能得到結果。

我遵循了一些我環顧四周的指南,但它們似乎不適用於這種格式的 xml 或者我看錯了。

任何幫助表示贊賞。

無需使用DataSetDataTable數據類型。 處理 XML 的最佳 API 是LINQ 到 Z3501BB093D363810B8Z1059B9CFED3

看看下面。

c#

void Main()
{
    const string MAIN = "connection_MAIN";
    string IP = string.Empty;
    string bd = string.Empty;
    string user = string.Empty;
    string password = string.Empty;

    XElement xml = XElement.Parse(@"<config>
    <extras>
        <dir_update>C:\extension\update.exe</dir_update>
    </extras>
    <connection_MAIN>
        <ip>LOCALHOST,1433</ip>
        <bd>DATA</bd>
        <user>sa</user>
        <password>gzqs=</password>
    </connection_MAIN>
    <connection_LOBBY>
        <ip>10.0.0.2,1433</ip>
        <bd>DATA</bd>
        <user>sa</user>
        <password>I/wqqZIgzqs=</password>
        <caixa>5yIz5GPu80s=</caixa>
        <printer>cARrmGLQlztLiUDxIJqoPkvJabIiyI9ye4H7t+4muYk=</printer>
    </connection_LOBBY>
    <connection_FRONT>
        <ip>10.0.0.5,1433</ip>
        <bd>FIELDS</bd>
        <user>sa</user>
        <password>I/wqqZIgzqs=</password>
    </connection_FRONT>
</config>");

    // get needed connection fragment
    IEnumerable<XElement> fragment = xml.Descendants(MAIN);

    // get all needed elements one by one
    IP = fragment.Elements("ip")?.FirstOrDefault().Value;
    bd = fragment.Elements("bd")?.FirstOrDefault().Value;
    user = fragment.Elements("user")?.FirstOrDefault().Value;
    password = fragment.Elements("password")?.FirstOrDefault().Value;
}

使用 Xml Linq 我將結果放入數據表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("ip", typeof(string));
            dt.Columns.Add("bd", typeof(string));
            dt.Columns.Add("user", typeof(string));
            dt.Columns.Add("password", typeof(string));

            XDocument doc = XDocument.Load(FILENAME);

            foreach(XElement connection in doc.Descendants().Where(x => x.Name.LocalName.StartsWith("connection_")))
            {
                dt.Rows.Add(new object[] {
                    ((string)connection.Name.LocalName).Substring(((string)connection.Name.LocalName).IndexOf("_") + 1),
                    (string)connection.Element("ip"),
                    (string)connection.Element("bd"),
                    (string)connection.Element("user"),
                    (string)connection.Element("password")
                });


            }
            Dictionary<string, DataRow> dict = dt.AsEnumerable()
                .GroupBy(x => x.Field<string>("name"), y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
            string ip = dict["LOBBY"].Field<string>("ip");

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM