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 9260407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:53:03+00:00 2026-06-18T12:53:03+00:00

I am trying to create a function to parse an XML file like this:

  • 0

I am trying to create a function to parse an XML file like this:

<?xml version="1.0" encoding="utf-8"?>
<list name="Grocery List" author="Ian" desc="Saturday grocery list">

        <item color="black" done="false">Milk</item>
        <item color="black" done="false">Eggs</item>
        <item color="blue" done="false">Water</item>

</list>

It parses the attributes correctly, but it fails to return the values of the list items. Here is the function and class it uses:

class List
{
    public string[] listItems;
    public string[] colorArray;
    public string[] doneArray;
    public string listName;
    public string listAuthor;
    public string listDesc;
    public string err;       
}

Reader definition:

class ListReader
{
    public List doListParse(string filename)
    {
         List l = new List();

         int arrayCount = 0;

         try
         {
             XmlReader r = XmlReader.Create(filename);

             while (r.Read())
             {
                 if (r.NodeType == XmlNodeType.Element && r.Name == "list")
                 {
                     //Get the attributes of the list
                     l.listName = r.GetAttribute("name");
                     l.listAuthor = r.GetAttribute("author");
                     l.listDesc = r.GetAttribute("desc");

                     while (r.NodeType != XmlNodeType.EndElement)
                     {
                         r.Read();
                         if (r.Name == "item")
                         {
                             r.Read();
                             if (r.NodeType == XmlNodeType.Text)
                             {
                                 //Get The Attributes
                                 l.colorArray[arrayCount] = r.GetAttribute("color");
                                 l.doneArray[arrayCount] = r.GetAttribute("done");

                                 //Get The Content
                                 l.listItems[arrayCount] = r.Value.ToString();

                                 arrayCount++;
                             }
                             r.Read();
                         }
                     }
                 }
             }
         }
         catch (Exception e)
         {
             l.err = e.ToString();
         }

         return l;
    }
}

When I execute the program, it gives this exception:

System.NullReferenceException: Object reference not set to an instance of an object.

What is going on here?

  • 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-18T12:53:04+00:00Added an answer on June 18, 2026 at 12:53 pm

    I’d recommend you using a serializer. The XmlSerializer class is pretty decent. It will simplify your code.

    So start by defining the models that will map to this XML structure:

    [XmlRoot("list")]
    public class GroceryList
    {
        [XmlAttribute("name")]
        public string Name { get; set; }
    
        [XmlAttribute("author")]
        public string Author { get; set; }
    
        [XmlAttribute("desc")]
        public string Description { get; set; }
    
        [XmlElement("item")]
        public Item[] Items { get; set; }
    }
    
    public class Item
    {
        [XmlAttribute("color")]
        public string Color { get; set; }
    
        [XmlAttribute("done")]
        public bool Done { get; set; }
    
        [XmlText]
        public string Value { get; set; }
    }
    

    and then simply deserialize the XML:

    class Program
    {
        static void Main()
        {
            var serializer = new XmlSerializer(typeof(GroceryList));
            using (var reader = XmlReader.Create("groceriesList.xml"))
            {
                var list = (GroceryList)serializer.Deserialize(reader);
                // you could access the list items here
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a sort of generic xml parser, like this: Part
I am trying to use Python's xml.etree.ElementTree.parse() function to parse an XML file I
I am trying to create a function that given a divid, and a list
I have an xml feed at this url Now im trying parse the content,
I have an xml file that looks like this: <!DOCTYPE ROOT SYSTEM zombie.dtd> <ROOT>
I am trying to parse a an xml file. I am creating an array
I'm trying to create a function to parse out all values in a multidimensional
I'm trying to create a class that receives a URL to an XML file.
I am trying to create a function that would parse php code and return
I am trying to create a temporary xml file with php that contains a

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.