-2

I'm trying to load data from the nodes in my xml file to get them to post in a listbox. Here is what my xml file looks like.

<?xml version="1.0" encoding="utf-8"?>
<MovieData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Movie>
    <Name>Death Race</Name>
    <Type>Action</Type>
    <Type>Adventure</Type>
    <Rating>R</Rating>
    <Disk>Blu-Ray</Disk>
  </Movie>
  <Movie>
    <Name>Death Race 2</Name>
    <Type>Action</Type>
    <Type>Adventure</Type>
    <Rating>R</Rating>
    <Disk>Blu-Ray</Disk>
  </Movie>
</MovieData>

Here is what i am trying to do.

try
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(movieListXML);
        XmlNodeList nodeList = doc.SelectNodes("/MovieData[@*]");
        foreach (XmlNode xn in nodeList)
        {
            XmlNode movie = xn.SelectSingleNode("Movie");
            if (movie != null)
            {
                movieTypeListBox.Items.Add(movie["Name"].InnerText);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Updated code still has problem. It only shows one movie name instead of all of the movie names.

try
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(movieListXML);
        XmlNodeList nodeList = doc.SelectNodes("/MovieData");
        foreach (XmlNode xn in nodeList)
        {
            XmlNode movie = xn.SelectSingleNode("Movie");
            if (movie != null)
            {
                movieTypeListBox.Items.Add(movie["Name"].InnerText);
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

Can anyone tell me where my problem is ?

Another question: Can someone show me how to organize the data alphabetically by the name of the Movie ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
deathismyfriend
  • 2,182
  • 2
  • 18
  • 25
  • what line(of your code) do you get the error at is it at the `doc.loadXML` method? – Sam I am says Reinstate Monica Oct 08 '13 at 20:20
  • I suspect the issue is that it is trying to read this data: but failing since it has the ? at the start. My advice would be to use a DataGrid instead, then get the data from that as you want to :) – XtrmJosh Oct 08 '13 at 20:21
  • by the way, where is `movieListXML` defined? – Sam I am says Reinstate Monica Oct 08 '13 at 20:21
  • It is defined at the start. In the initialize method. It is the path of the xml file. It is correct since it lets me add stuff and doesn't throw an error. – deathismyfriend Oct 08 '13 at 20:29
  • possible duplicate of [Data at the root level is invalid](http://stackoverflow.com/questions/5748668/data-at-the-root-level-is-invalid). You are using `LoadXml` when you should be using `Load`. – John Saunders Oct 08 '13 at 20:38
  • Yup that has been mentioned and fixed so far. I have a new problem look above at updated code and problem. – deathismyfriend Oct 08 '13 at 20:55
  • @user2860193 if you have a new problem, than please post a new question, and only do so if you can't figure it out on your own. We don't need more [chameleon questions](http://meta.stackexchange.com/questions/43478/exit-strategies-for-chameleon-questions) – Sam I am says Reinstate Monica Oct 08 '13 at 20:58
  • @user2860193 by the way, you've already used `SelectSingleNode`, `SelectNodes`, and a `foreach` loop in your code sample. you just have to use `SelectNodes` to get your `Movie`s and use `SelectSingleNode` to get your `MovieData` instead of the other way around. – Sam I am says Reinstate Monica Oct 08 '13 at 21:05
  • BTW, you really should start using LINQ to XML. It's a lot easier for the kinds of things you need to do, like sorting the XML. – John Saunders Jan 16 '14 at 20:05
  • I've had no problem with the way I do it now but I will look into LINQ to XML when I can. – deathismyfriend Jan 16 '14 at 20:06

2 Answers2

1

check your xml file - it's likely got a Bite order marker in it. open the file in a hex editor and delete the non printing characters from the start of the file.

As your xml looks fine - particularly the xml declaration - I'm pretty sure this will be your problem.

depending on the character encoding used when the file was created it'll be somehting like : 0xFEFF (if its utf-8 - think that ones little endian)

Here's how to get all the movie names out :

           XmlNodeList nodeList = doc.SelectNodes("/MovieData/Movie");
            foreach (XmlNode xn in nodeList)
            {
                    Console.WriteLine(xn["Name"].InnerText);
            }

if you want to sort them too then its probably easier to XDocument.

mp3ferret
  • 1,183
  • 11
  • 16
  • I'm not sure what you mean i posted the whole xml file in the above post. I'm using visual studio to look at the file. How do i do it in that ? I'm very new to this only been doing it about 2 days now. Thanks for your help – deathismyfriend Oct 08 '13 at 20:23
  • yup thats what I thought when i first encountered this problem - the file looks fine. But, honestly, look at the file in a hex editor - I bet there are 2 or 3 non printable characters at the start of it. – mp3ferret Oct 08 '13 at 20:26
  • in vs right click the xml file in your project and click open with .... Then select binary editior. In fact I've just done exactly that for any random xml file i have - and the file starts with 0xEFBBBF - can;t remember which unicode encoding thats for. While editing an xml file - just adding the encoding='' attribute can set this value again inside of vs – mp3ferret Oct 08 '13 at 20:28
  • I looked at it through the binary editor like you said. This is what it shows EF BB BF is that what i delete ? I deleted that and still nothing. – deathismyfriend Oct 08 '13 at 20:32
  • Yup - thats what you need to delete. You didn't reopen the file in vs text editor did you - It'll write the BOM back if you do. – mp3ferret Oct 08 '13 at 20:36
  • It showed the same error. But i did what suggested below by changing doc.LoadXml to doc.Load and i get no error but nothing is shown in the list box – deathismyfriend Oct 08 '13 at 20:38
  • the doc.load is the correct method if the parameter is a filename. if its not getting the error then the xml file has loaded. does your for loop iterate through any data -or just skip straight past. – mp3ferret Oct 08 '13 at 20:42
  • It now shows only one movie name in the list box. Updated code is above. – deathismyfriend Oct 08 '13 at 20:49
  • oops - I'd assumed you were loading the xml as a string - so i guess you can ignore the bom stuff. updated answer to get you all the movie names – mp3ferret Oct 08 '13 at 21:07
0

My psychic debugging powers tell me that movieListXML might be a filename in which case you want to call

//movieListXML = @"c:\xmlFile.xml";
doc.Load(movieListXML);

instead of

//movieListXML = @"<MovieData><Movie></Movie></MovieData>";
doc.LoadXml(movieListXML);

which takes the actual XML in as a string