Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9317213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:49:08+00:00 2026-06-19T02:49:08+00:00

My second post in the same evening, apologies! I have an XML file which

  • 0

My second post in the same evening, apologies!

I have an XML file which contains several bits of information for various software entries (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).

I am trying to write a method that iterates through each entry in 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 to the second listbox. The method should look through the XML file, find the entry with the attribute “name” that matches what is in the list, and write the other data (path, type, switches) or set them as the value of strings. So far, after much tweaking, I’ve achieved little. I managed to get some really garbled output.

My code so far is as follows.

private void writeBatchFile()
{
    // Get temp folder location
    string tempPath = Path.GetTempPath();

    // Create batch file
    using (StreamWriter writer = File.CreateText(tempPath + @"\swInstaller.cmd"))
    {
        // Loop through entries in listBox2
        foreach (string item in listBox2.Items)
        {
            var dataFromXML = from data in document.Descendants("software_entry")
                               where data.Element("name").Value == item
                               select new
                               {
                                   fileName = (string) data.Element("name"),
                                   filePath = (string) data.Element("path"),
                                   fileType = (string) data.Element("type"),
                                   fileSwitches = (string) data.Element("switches")
                               };

            // Write to batch file
            writer.WriteLine(fileName + filePath + fileType + fileSwitches);
        }
    }
}

In the code I’ve got a foreach loop for the text values in listBox2. I’m then attempting to find where that value is equal to a “name” value in the XML, and then when it does, set the other attributes for that value to those strings (filePath for path, fileType for type etc.). Finally, I’m trying to write those strings to a text file. With this particular attempt, all I’m getting is 5 blank lines. Can anybody point me in the right direction as to what I’m doing wrong?

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:49:09+00:00Added an answer on June 19, 2026 at 2:49 am

    You are trying to read XML attributes, not elements. Use the Attribute method instead:

    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
        }
    );
    
    writer.WriteLine(dataFromXML.fileName);
    

    You also need to make these additional changes:

    • Use Enumerable.Single() to indicate that you only expect 1 element to match the given name. Otherwise dataFromXML may contain several elements and you would need to use a loop or index to access them.
    • You need to access the fileName and other variables as members of the dataFromXML variable. There aren’t local variables of their own so that’s why you are getting the error message you mentioned.
    • You do not need to cast to a string as values read from XML attributes are already strings by default.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form which I'd like to post to the same page. The
First, a thank you in advance. Second, this is my first post so apologies
I have a query which can be expressed 2 different ways with the same
I have to do a HTTP post in java every second after building a
I know of at least one post which has same words like this. But
I have searched, and this post is closest, but not exactly the same. I
good evening everone , i have phpmailer code which allow me to connect to
The formula mentioned in post Google's Leap Second Smear Techinque :Modulating lie over a
UPDATE: First problem solved, second one described at the bottom of this post. UPDATE2:
(second question today - must be a bad day) I have a dataframe with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.