Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading in a simple XML config file in C#

Im having a hard time remembering how to do this, and most of the examples I'm seeing dont really cover my issue. I'm trying to read in the below XML file, so that if a user selects a Tool Type from a drop-down menu, the variables for said tool will populate a form on screen. I just have no clue how to collect all the elements/attributes for a specific tool.

<?xml version="1.0" encoding="UTF-8"?> 
<Tool_menu>
    <tool name="A">
        <emails>
            <email severity="Low">
                <address>[email protected]</address>
            </email>
            <email severity="High">
                <address>[email protected]</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>[email protected]</address>
            </email>
            <email severity="High">
                <address>[email protected]</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>[email protected]</address>
            </email>
            <email severity="High">
                <address>[email protected]</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> 

what I'd want is for a user to select 'tool C' and see a list of configurations available on tool C, name of the tool, and a dropdown of options for who to email (low/high severity) that would be tool specific

**using .net 4.5

like image 305
Medic3000 Avatar asked Nov 25 '25 12:11

Medic3000


1 Answers

Access nodes using XPath. Take a look at some tutorials here or here

In your case, accessing tools C can be achieved like this:

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
like image 178
Nino Avatar answered Nov 28 '25 01:11

Nino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!