开发者

Xml Attribute not showing up on ListBox - C#

开发者 https://www.devze.com 2022-12-13 12:52 出处:网络
I\'m getting a null reference exception whenever it tries to add the packages titles info and other attributes but the attributes exist and the proper package is selected

I'm getting a null reference exception whenever it tries to add the packages titles info and other attributes but the attributes exist and the proper package is selected

Heres the code:

private void categorylist_listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            XmlDocument LoadPackageList = new XmlDocument();
            //Removes the text "Select A Category" and refrehes the form
            packagelist_listbox.Items.Remove(SelectaCategory_listbox);

            if (categorylist_listview.SelectedItem == WWW_listviewitem)
            {
                LoadPackageList.Load("www.xml");
                XmlNodeList WWWPackageList = LoadPackageList.SelectNodes("/Packages/*");
                int countthenodes = 0;
                foreach (XmlNode WWWPackages in WWWP开发者_StackOverflowackageList)
                {
                    //Cycles through all the packages and assings them to a string then adds it to the packagelist
                    countthenodes++;
                    PackageTitle[countthenodes] = WWWPackages.Attributes["title"].ToString();
                    PackageInfo[countthenodes] = WWWPackages.Attributes["info"].ToString();
                    PackageDownloadUrl[countthenodes] = WWWPackages.Attributes["downloadurl"].ToString();
                    PackageTags[countthenodes] = WWWPackages.Attributes["tags"].ToString();
                    packagelist_listbox.Items.Add(PackageTitle[countthenodes]);
                }
                Refresh(packagelist_listbox);

            }
        }

It Errors out at PackageTitle[countthenodes] = WWWPackages.Attributes["title"].ToString();

XML File:

<Packages>
  <Firefox title="Mozilla Firefox" tags="www firefox web browser mozilla" info="http://google.com" downloadurl="http://firefox.com"></Firefox>


</Packages>

The Variables are declared

        public string[] PackageTags;
        public string[] PackageTitle;
        public string[] PackageInfo;
        public string[] PackageDownloadUrl;

At the very beginning of the file


Well, the first problem is that calling ToString() on an XmlAttribute isn't going to do what you want. You should use the Value property. However, I don't believe that's causing a NullReferenceException unless the data isn't quite as you showed it. Here's a short but complete program which works fine:

using System;
using System.Xml;

class Test
{
    static void Main()
    {
        XmlDocument doc = new XmlDocument();
        doc.Load("test.xml");        
        XmlNodeList list = doc.SelectNodes("/Packages/*");
        foreach (XmlNode node in list)
        {
            Console.WriteLine(node.Attributes["title"].Value);
        }
    }
}

That displays "Mozilla Firefox" with the XML you gave us.

Options:

  • Your real XML actually contains an element without a title attribute
  • Perhaps PackageTitle is null?

It would help if you could produce a short but complete program demonstrating the problem. Ideally it should avoid using a GUI - I can't see anything here which is likely to be GUI-specific.

If you could tell us more about PackageTitle and how it's being initialized, that would help too. How are you expecting it to just keep expanding for as many elements as you find? Or is it an array which is initialized to a larger size than you ever expect to find elements?

0

精彩评论

暂无评论...
验证码 换一张
取 消