Hi all

Hope you can help!

I have an XML file which contains several bits of information for each software entry (snippet seen below).

<software>
    <software_entry
    name="Adobe Acrobat X Standard"
    path="Applications\Acrobat\Acrobat X Standard\AcroStan.msi" 
    type="msi"
    switches="/qn ALLUSERS=1"
    />

    <software_entry
    name="Adobe Acrobat X Professional"
    path="Applications\Acrobat\Acrobat X Pro\AcroPro.msi"
    type="msi"
    switches="/qn ALLUSERS=1"
    />
</software>

I am using LINQ to XML to load the XML file. In my application I have two listboxes, the first of which (listBox1) is populated with the names of each entry in this XML file. The user can then move these entries from one listbox to another (listBox2).

Now for the challenge!

I am trying to write a method that iterates through each entry in the listBox2, finds the matching entry in the XML file and then retrieves all of the other attributes for that entry.

For example, a user has added Adobe Acrobat X Professional and Adobe Flash Player into listBox2. Upon clicking a finish button, this method should then look in the XML file, find the two entries that have the 'name' field matching what is in the listbox, and then (for example) write the data from the other fields (path, type and switches) to console. In reality I'll be writing it to a batch file but I know how to do that.

So far, all I've got is the basic foreach loop (as below).

foreach (string item in listBox2.Items)
{
    writer.WriteLine("Testing!");
}

Not asking for the code to be completely written out for me, just some tips/examples would be amazing. It's the finding the matching entry in the XML file that's the main issue. Google has been largely fruitless. Thanks!

I've actually nearly got it I think!

foreach (string item in listBox2.Items)
{
    var dataFromXML = Enumerable.Single
    (
    from data in document.Descendants("software_entry")
    where data.Attribute("name").Value == item
    select new
    {
        fileName = data.Attribute("name").Value,
        filePath = data.Attribute("path").Value,
        fileType = data.Attribute("type").Value,
        fileSwitches = data.Attribute("switches").Value
     }
     );
}

Yep, ignore this topic. Solved it myself. Code is as above. To write the data, you use the StreamWriter and the strings dataFromXML.fileName, dataFromXML.filePath etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.