CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2011
    Posts
    6

    Reading an XML file using C#

    Hey all

    Been writing an application which needs to read an XML file, and then use its contents to populate a listbox and certain other variables. In this case, I wish to populate a Listbox with all elements called 'name'. I had this working with the previous way in which my XML file was set up, but I was told that it wasn't a good way of writing it, so I re-wrote it as follows.

    Code:
    <root>
    	<software>
    		<software_entry
    		name="Adobe Acrobat X Standard"
    		path="Applications\Acrobat\Acrobat X Standard - M3\AcroStan.msi" 
    		type="msi"
    		switches="/qn ALLUSERS=1"
    		/>
    		
    		<software_entry
    		name="Adobe Acrobat X Professional"
    		path="Applications\Acrobat\Acrobat X Pro - M3\AcroPro.msi"
    		type="msi"
    		switches="/qn ALLUSERS=1"
    		/>
    	</software>
    </root>
    And now, the C# code that will read it and populate a List<>, and therefore a listbox.

    Code:
            private void ReadXMLFile()
            {
                var doc = new XmlDocument();
                doc.Load(Application.StartupPath + @"\config.xml");
                foreach (XmlElement match in doc.SelectNodes("/software/software_entry_name"))
                    _unselected.Add(match.InnerText);
            }
    
            private void HookComponentEvents()
            {
                Load += (sender, e) =>
                {
                    listBox1.DataSource = _unselected;
                    listBox2.DataSource = _selected;
                };
                addSoftwareBtn.Click += (sender, e) => { MoveSelectedItem(_unselected, _selected); };
                removeSoftwareBtn.Click += (sender, e) => { MoveSelectedItem(_selected, _unselected); };
            }
    It compiles fine, but nothing is added to listBox1. Am I missing something blindingly obvious?

    Thanks.
    Last edited by CSF90; December 6th, 2011 at 10:58 AM. Reason: Added code tags.

  2. #2
    Join Date
    Dec 2011
    Posts
    6

    Re: Reading an XML file using C#

    Sorry, forgot to say I am using .NET 3.5

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Reading an XML file using C#

    Put a breakpoint on the line
    Code:
    _unselected.Add(match.InnerText);
    Press F5, does your breakpoint get hit?

  4. #4
    Join Date
    Dec 2011
    Posts
    6

    Re: Reading an XML file using C#

    Quote Originally Posted by Arjay View Post
    Put a breakpoint on the line
    Code:
    _unselected.Add(match.InnerText);
    Press F5, does your breakpoint get hit?
    Thanks for the reply, but nope, it doesn't.

    Edit: Sorry, by hit, you mean does it run? Because it's not flagged up, it just runs as normal.
    I took 'hit' to mean flagged up and the program stops, but you could've meant does the program 'hit' it and then continue to run.
    Last edited by CSF90; December 6th, 2011 at 12:45 PM.

  5. #5
    Join Date
    Aug 2008
    Posts
    902

    Re: Reading an XML file using C#

    maybe I'm missing something here, but shouldn't it be

    Code:
    foreach (XmlElement match in doc.SelectNodes("root/software/software_entry_name"))

  6. #6
    Join Date
    Dec 2011
    Posts
    6

    Re: Reading an XML file using C#

    Quote Originally Posted by Chris_F View Post
    maybe I'm missing something here, but shouldn't it be

    Code:
    foreach (XmlElement match in doc.SelectNodes("root/software/software_entry_name"))
    My understanding is that "/" looks in the root automatically, whereas // is used to define a starting node.

    So "/software" is the same as "root/software". Just in case, I tested as root/software and still nothing. Thanks for the suggestion.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Reading an XML file using C#

    Looking at the xml, it should be
    Code:
    /root/software/software_entry
    not
    Code:
    /root/software/software_entry_name

  8. #8
    Join Date
    Dec 2011
    Posts
    61

    Re: Reading an XML file using C#

    Code:
                    foreach (XmlElement match in doc.SelectNodes("/root/software/software_entry"))
                    {
                        _unselected.Add(match.OuterXml);
                    }
    Tested working under .net 3.5

  9. #9
    Join Date
    Dec 2011
    Posts
    6

    Re: Reading an XML file using C#

    Quote Originally Posted by Silent Sojourner View Post
    Code:
                    foreach (XmlElement match in doc.SelectNodes("/root/software/software_entry"))
                    {
                        _unselected.Add(match.OuterXml);
                    }
    Tested working under .net 3.5
    Thanks, we're a step closer but it adds all of the XML, rather than just the 'name' of the software (e.g. Adobe Acrobat X Professional).

    Screenshot.

  10. #10
    Join Date
    Dec 2011
    Posts
    61

    Re: Reading an XML file using C#

    Sorry I didn't read your post carefully. In that case, just replace the statement in the for loop with this:
    Code:
    _unselected.Add(match.Attributes["name"].Value);

  11. #11
    Join Date
    Dec 2011
    Posts
    6

    Re: Reading an XML file using C#

    Quote Originally Posted by Silent Sojourner View Post
    Sorry I didn't read your post carefully. In that case, just replace the statement in the for loop with this:
    Code:
    _unselected.Add(match.Attributes["name"].Value);
    Ah, that's the one! I just needed to know the syntax used to access an attribute within a node when the format was at it is in my XML file.

    Thanks very much!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured