2

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>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> 

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

Medic3000
  • 786
  • 4
  • 20
  • 44
  • Perhaps with [Linq](http://stackoverflow.com/questions/670563/linq-to-read-xml). – Jasen Feb 27 '17 at 21:42
  • what specific problems are you having? how are you trying to read in the xml? can you post what you have tried so far? – geekzster Feb 27 '17 at 21:42
  • I dont know how to get at info from the file, so thats the issue I guess. I've tried so far: XDocument.Load() but I dont know what to do next... how do I say "if the Tool='C', then grab everything under it" – Medic3000 Feb 27 '17 at 21:46
  • This [SO answer](http://stackoverflow.com/questions/8948847/bind-xml-data-to-a-dropdownlist-c-sharp) may be helpful to you. – zx485 Feb 27 '17 at 21:47
  • Is this xml structure something that you can change or you get it from third-party? – Yuri S Feb 27 '17 at 22:38

2 Answers2

12

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
Nino
  • 6,931
  • 2
  • 27
  • 42
1

You could take a look at this post on SO: LINQ to read XML

edit: also, in the comments below your question, @zx485 posted a link to another helpful SO post, here: Bind XML data to a Dropdownlist c#

That might help get you started. If not, please try to narrow your question to something more specific - preferably with some code that we can help you with.

As your question is written right now, it seems that you are asking us to do this for you rather than help you with a specific question.

Community
  • 1
  • 1
geekzster
  • 559
  • 8
  • 21