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

  • Home
  • SEARCH
  • 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 9141651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:40:24+00:00 2026-06-17T09:40:24+00:00

I’m writing an xml reader where a class structure will represent the data from

  • 0

I’m writing an xml reader where a class structure will represent the data from an xml file..

for an example:

<catalog>
<book id="bk101">
  <author>Gambardella, Matthew</author>
  <title>XML Developer's Guide</title>
  <genre>Computer</genre>
  <price>44.95</price>
  <publish_date>2000-10-01</publish_date>
  <description>An in-depth look at creating applications 
  with XML.</description>
</book>
</catalog> 

how can I find that a node contains child nodes! I used the isempty element method! but I get an exception.

 public element read(XmlReader xml)
    {
        element elem = new element();
        while (xml.Read())
        {
            if (xml.IsEmptyElement)
            {
                elem.ElemName = xml.Name;
                if (xml.HasAttributes)
                {
                    for (int i = 0; i <= xml.AttributeCount; i++)
                    {
                        xml.MoveToNextAttribute();
                        attribute attrib = new attribute();
                        attrib.AttName = xml.Name;
                        attrib.AttValue = xml.Value;
                        elem.Attributes.Add(attrib);
                    }
                }
                return elem;
            }
            else
            {
                elem.ElemName = xml.Name;

                if (xml.HasAttributes)
                {
                    for (int i = 1; i < xml.AttributeCount; i++)
                    {
                        xml.MoveToNextAttribute();
                        attribute attrib = new attribute();
                        attrib.AttName = xml.Name;
                        attrib.AttValue = xml.Value;
                        elem.Attributes.Add(attrib);
                    }
                }

                elem.subElems.Add(read(xml)); -> Object reference not set to an instance of an object.
                return elem;
            }
        }
        return elem;
    }

I use two classes. one called element and one called attributes. what I do here is I go line by line and when I find an element I create an element type object. then I check whether that element contains any attribute. if so, I create attribute type objects for the attributes I encounter and add them to the attribute list which is in the element object. then I check for sub elements and if I find any, I add them as separate element objects to a list in the main element object.

  class element
{
    private String elemName;

    public String ElemName
    {
        get { return elemName; }
        set { elemName = value; }
    }
    private String elemValue;

    public String ElemValue
    {
        get { return elemValue; }
        set { elemValue = value; }
    }
    private List<attribute> attributes;

    internal List<attribute> Attributes
    {
        get { return attributes; }
        set { attributes = value; }
    }

    private List<element> SubElems;

    internal List<element> subElems
    {
        get { return SubElems; }
        set { SubElems = value; }
    }

}

class attribute
{
    private String Name;

    public String AttName
    {
        get { return Name; }
        set { Name = value; }
    }
    private String value;

    public String AttValue
    {
        get { return this.value; }
        set { this.value = value; }
    }
}
  • 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-17T09:40:25+00:00Added an answer on June 17, 2026 at 9:40 am

    When you create element elem = new element(); you never assign values to subElems, only to elem.ElemName = xml.Name. So as result you get NullReferenceException because on subElems you have null after object creation. I have refactored your code (Note that I don’t know anything about element class)

    public element read(XmlReader xml)
    {
        element elem = new element();
        while (xml.Read())
        {
            elem.ElemName = xml.Name;
            if (xml.HasAttributes)
            {
                for (int i = 0; i <= xml.AttributeCount; i++)
                {
                    xml.MoveToNextAttribute();
                    attribute attrib = new attribute();
                    attrib.AttName = xml.Name;
                    attrib.AttValue = xml.Value;
                    elem.Attributes.Add(attrib);
                }
            }
            if (xml.IsEmptyElement)
            {   
                return elem;
            }
            else
            {
                elem.subElems = new List<element>(); //create new List of subelements
                elem.subElems.Add(read(xml)); 
                return elem;
            }
        }
        return elem;
    

    OR

    define in your constructor of element initialization of subElems and Attributes like

    public element()
    {
        this.subElems = new List<element>();
        this.Attributes = new List<attribute>();
    }
    

    OR

    Change your field declaration to

    private List<element> SubElems = new List<element>();
    private List<attribute> attributes = new List<attribute>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I am using jsonparser to parse data and images obtained from json response. When
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I am doing a simple coin flipping experiment for class that involves flipping 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.