用C#读取一个简单的XML配置文件

Med*_*000 1 .net c# xml visual-studio

我很难记住如何执行此操作,我看到的大多数示例并未真正涵盖我的问题。我试图读取下面的XML文件,以便如果用户从下拉菜单中选择“工具类型”,则该工具的变量将填充屏幕上的表格。我只是不知道如何收集特定工具的所有元素/属性。

<?xml version="1.0" encoding="UTF-8"?> 
<Tool_menu>
    <tool name="A">
        <emails>
            <email severity="Low">
                <address>reg@test.com</address>
            </email>
            <email severity="High">
                <address>notReg@test.com</address>
            </email>
        </emails>
        <configuration_list>
            <configuration>
                <name>Confg1</name>
            </configuration>
            <configuration>
                <name>Confg2</name>
            </configuration>
            <configuration>
                <name>Confg3</name>
            </configuration>
            <configuration>
                <name>Confg4</name>
            </configuration>
        </configuration_list>
    </tool>
    <tool name="B">
        <emails>
            <email severity="Low">
                <address>reg@test.com</address>
            </email>
            <email severity="High">
                <address>notReg@test.com</address>
            </email>
        </emails>
        <configuration_list>
            <configuration>
                <name>n/a</name>
            </configuration>
            <configuration>
                <name>n/a</name>
            </configuration>
        </configuration_list>
    </tool>
    <tool name="C">
        <emails>
            <email severity="Low">
                <address>reg@test.com</address>
            </email>
            <email severity="High">
                <address>notReg@test.com</address>
            </email>
        </emails>
        <configuration_list>
            <configuration>
                <name>200Scope</name>
            </configuration>
            <configuration>
                <name>300Scope</name>
            </configuration>
            <configuration>
                <name>600Scope</name>
            </configuration>
            <configuration>
                <name>900Scope</name>
            </configuration>
        </configuration_list>
    </tool>
</Tool_menu> 
Run Code Online (Sandbox Code Playgroud)

我想要的是让用户选择“工具C”,并查看工具C上可用的配置列表,工具名称以及向谁发送电子邮件的选项下拉列表(低/高严重性)具体

**使用.net 4.5

Nin*_*ino 5

使用XPath访问节点。在这里这里看一些教程

在您的情况下,访问工具C可以这样实现:

XmlDocument doc = new XmlDocument();
doc.Load(@"c:\temp\tools.xml");

var toolCemails = doc.SelectNodes("//tool[@name='C']/emails/email"); //two nodes with email tag
var toolCconfigs = doc.SelectNodes("//tool[@name='C']/configuration_list/configuration"); //four config nodes
Run Code Online (Sandbox Code Playgroud)