将不同名称的XML读入组合框C#

将不同名称的XML读入组合框C#,c#,xml,datatable,combobox,dataset,C#,Xml,Datatable,Combobox,Dataset,看起来这是一项非常简单的任务,我对c#很陌生,似乎找不到正确的答案 我有以下xml结构(元素数量不同) 在本例中,当我在组合框上选择第一个选项时,变量ip得到值“ip”,我想要“LOCALHOST,1433” 我还想按名称(“ip”、“bd”)搜索列值,但我似乎只在使用列[0]、列[1]时得到结果 我遵循了一些我看过的指南,但它们似乎对这种xml格式不起作用,或者我看得不对 感谢您的帮助。无需使用数据集和数据表数据类型。处理XML的最佳API是LINQtoXML 看看下面 c# void Mai

看起来这是一项非常简单的任务,我对c#很陌生,似乎找不到正确的答案

我有以下xml结构(元素数量不同)

在本例中,当我在组合框上选择第一个选项时,变量ip得到值“ip”,我想要“LOCALHOST,1433”

我还想按名称(“ip”、“bd”)搜索列值,但我似乎只在使用列[0]、列[1]时得到结果

我遵循了一些我看过的指南,但它们似乎对这种xml格式不起作用,或者我看得不对


感谢您的帮助。

无需使用
数据集
数据表
数据类型。处理XML的最佳API是LINQtoXML

看看下面

c#

void Main()
{
const string MAIN=“连接\u MAIN”;
stringip=string.Empty;
string bd=string.Empty;
string user=string.Empty;
字符串密码=string.Empty;
XElement xml=XElement.Parse(@)
C:\extension\update.exe
本地主机,1433
资料
sa
gzqs=
10.0.0.2,1433
资料
sa
I/WQZIGZQS=
5yIz5GPu80s=
cARrmGLQlztLiUDxIJqoPkvJabIiyI9ye4H7t+4muYk=
10.0.0.5,1433
领域
sa
I/WQZIGZQS=
");
//获取所需的连接片段
IEnumerable fragment=xml.substands(MAIN);
//逐个获取所有需要的元素
IP=fragment.Elements(“IP”)?.FirstOrDefault()值;
bd=fragment.Elements(“bd”)?.FirstOrDefault()值;
user=fragment.Elements(“user”)?.FirstOrDefault()值;
password=fragment.Elements(“password”)?.FirstOrDefault()值;
}

无需使用
数据集
数据表
数据类型。处理XML的最佳API是LINQtoXML

看看下面

c#

void Main()
{
const string MAIN=“连接\u MAIN”;
stringip=string.Empty;
string bd=string.Empty;
string user=string.Empty;
字符串密码=string.Empty;
XElement xml=XElement.Parse(@)
C:\extension\update.exe
本地主机,1433
资料
sa
gzqs=
10.0.0.2,1433
资料
sa
I/WQZIGZQS=
5yIz5GPu80s=
cARrmGLQlztLiUDxIJqoPkvJabIiyI9ye4H7t+4muYk=
10.0.0.5,1433
领域
sa
I/WQZIGZQS=
");
//获取所需的连接片段
IEnumerable fragment=xml.substands(MAIN);
//逐个获取所有需要的元素
IP=fragment.Elements(“IP”)?.FirstOrDefault()值;
bd=fragment.Elements(“bd”)?.FirstOrDefault()值;
user=fragment.Elements(“user”)?.FirstOrDefault()值;
password=fragment.Elements(“password”)?.FirstOrDefault()值;
}

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

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用系统数据;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
DataTable dt=新的DataTable();
添加(“名称”,类型(字符串));
添加(“ip”,类型(字符串));
添加(“bd”,typeof(string));
添加(“用户”,类型(字符串));
添加(“密码”,类型(字符串));
XDocument doc=XDocument.Load(文件名);
foreach(doc.subjections()中的XElement连接,其中(x=>x.Name.LocalName.StartsWith(“连接”))
{
dt.Rows.Add(新对象[]){
((字符串)connection.Name.LocalName).Substring(((字符串)connection.Name.LocalName.IndexOf(“”)+1),
(字符串)连接元素(“ip”),
(字符串)connection.Element(“bd”),
(字符串)connection.Element(“用户”),
(字符串)connection.Element(“密码”)
});
}
Dictionary dict=dt.AsEnumerable()
.GroupBy(x=>x.Field(“名称”),y=>y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
字符串ip=dict[“大厅”]。字段(“ip”);
}
}
}

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

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用系统数据;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
DataTable dt=新的DataTable();
添加(“名称”,类型(字符串));
添加(“ip”,类型(字符串));
添加(“bd”,typeof(string));
添加(“用户”,类型(字符串));
添加(“密码”,类型(字符串));
XDocument doc=XDocument.Load(文件名);
foreach(doc.subjections()中的XElement连接,其中(x=>x.Name.LocalName.StartsWith(“连接”))
{
dt.Rows.Add(新对象[]){
((字符串)connection.Name.LocalName).Substring(((字符串)connection.Name.LocalName.IndexOf(“”)+1),
(字符串)连接元素(“ip”),
(字符串)connection.Element(“bd”),
(字符串)connection.Element(“用户”),
(字符串)connection.Element(“密码”)
});
}
Dictionary dict=dt.AsEnumerable()
.GroupBy(x=>x.Field(“名称”),y=>y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
字符串ip=dict[“大厅”]。字段(“ip”);
}
}
}
IMHO,OP需要
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;
}
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");

        }
    }
}