将不同名称的 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>

我已经在我的组合框中获取了以 "connection_" 开头的元素,我想要 <ip><bd><user> 和 [= 中的值16=] 当我 select 组合框上的连接时。

我遇到的问题是它返回的是单词本身,而不是下面代码中的值

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 在这个例子中我的组合框上的第一个选项。

我还想通过名称 ("ip"、"bd") 搜索列值,但我似乎只在使用 Columns[0]、Columns[1] 时才能得到结果。

我遵循了一些我四处查看的指南,但它们似乎不适用于 xml 这种格式,或者我看错了。

感谢任何帮助。

不需要使用DataSetDataTable数据类型。处理 XML 的最佳 API 是 LINQ to XML.

在下面查看。

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

        }
    }
}