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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:38:49+00:00 2026-05-19T01:38:49+00:00

Attempting to recursively add XElements stepping through a category list. XElement dataResponse = new

  • 0

Attempting to recursively add XElements stepping through a category list.

XElement dataResponse = new XElement("Categories",
                                       from c in db.Categories
                                       where c.CatTypeID.Equals(catTypeID) && c.ParentID.Equals(null)
                                       select new XElement("Category",
                                            c.CatID == null ? null : new XAttribute("CatID", c.CatID),
                                            c.ParentID == null ? null : new XAttribute("ParentID", c.ParentID),
                                            c.CatTitle == null ? null : new XAttribute("CatTitle", c.CatTitle),
                                            c.CatTypeID == null ? null : new XAttribute("CatTypeID", c.CatTypeID),
                                            c.shortDesc == null ? null : new XAttribute("shortDesc", c.shortDesc),
                                            c.longDesc == null ? null : new XAttribute("longDesc", c.longDesc),
                                            c.CatImage == null ? null : new XAttribute("CatImage", c.CatImage)));

                    internalData = FillSubCatagories(dataResponse).ToString();

Thats the first list of categories now i want to recursively pull all sub categories and nest them in my Xelements FillSubCatagories() method:

private XElement FillSubCatagories(XElement cat) {
        IEnumerable<XElement> list = cat.Descendants();
        int catTypeID = c.Attribute("CatTypeID") == null ? 1 : Int32.Parse(cat.Attribute("CatTypeID").Value);
        foreach (XElement c in list) {
            int parentID = Int32.Parse(cat.Attribute("CatID").Value);
            XElement sub = new XElement("sub",
                from s in db.Categories
                where s.CatTypeID.Equals(catTypeID) && s.ParentID.Equals(parentID)
                select new XElement("Category",
                     s.CatID == null ? null : new XAttribute("CatID", s.CatID),
                     s.ParentID == null ? null : new XAttribute("ParentID", s.ParentID),
                     s.CatTitle == null ? null : new XAttribute("CatTitle", s.CatTitle),
                     s.CatTypeID == null ? null : new XAttribute("CatTypeID", s.CatTypeID),
                     s.shortDesc == null ? null : new XAttribute("shortDesc", s.shortDesc),
                     s.longDesc == null ? null : new XAttribute("longDesc", s.longDesc),
                     s.CatImage == null ? null : new XAttribute("CatImage", s.CatImage)));
            c.Add(sub);
            FillSubCatagories(c);
        }
        return cat;
    }

Alright so the problem comes when I run through the foreach the second time through the method. On int parentID = Int32.Parse(cat.Attribute("CatID").Value); returns a nullreferenceException – “Object reference not set to an instance of an object”

Still getting used to c# coming from java, so be gentle. I’m sure its a glaring error but I haven’t seen a clean reason why.

<<<<<<<<<>>>>>>>>>>>>>>
EDITED
new FillSubCategories() looks like this

private XElement FillSubCatagories(XElement cat) {
        IEnumerable<XElement> list = cat.Descendants();
        int catTypeID = cat.Attribute("CatTypeID") == null ? 1 : Int32.Parse(cat.Attribute("CatTypeID").Value);
        foreach (XElement c in list) {
            int parentID = Int32.Parse(c.Attribute("CatID").Value);
            XElement sub = new XElement("sub",
                from s in db.Categories
                where s.CatTypeID.Equals(catTypeID) && s.ParentID.Equals(parentID)
                select new XElement("Category",
                     s.CatID == null ? null : new XAttribute("CatID", s.CatID),
                     s.ParentID == null ? null : new XAttribute("ParentID", s.ParentID),
                     s.CatTitle == null ? null : new XAttribute("CatTitle", s.CatTitle),
                     s.CatTypeID == null ? null : new XAttribute("CatTypeID", s.CatTypeID),
                     s.shortDesc == null ? null : new XAttribute("shortDesc", s.shortDesc),
                     s.longDesc == null ? null : new XAttribute("longDesc", s.longDesc),
                     s.CatImage == null ? null : new XAttribute("CatImage", s.CatImage)));
            c.Add(sub);
            if (sub.Descendants() != null) {
                FillSubCatagories(sub);
            }
        }
        return cat;
    }

this got me a lot further but I still end up hitting null.

EDIT WORKING METHOD

private void FillSubCategories(XElement cat) {
        IEnumerable<XElement> list = cat.Descendants();
        foreach (XElement c in list) {
            try {
                int catTypeID = Int32.Parse(c.Attribute("CatTypeID").Value);
                int parentID = Int32.Parse(c.Attribute("CatID").Value);
                XElement sub = new XElement("sub",
                    from s in db.Categories
                    where s.CatTypeID.Equals(catTypeID) && s.ParentID.Equals(parentID)
                    select new XElement("Category",
                         s.CatID == null ? null : new XAttribute("CatID", s.CatID),
                         s.ParentID == null ? null : new XAttribute("ParentID", s.ParentID),
                         s.CatTitle == null ? null : new XAttribute("CatTitle", s.CatTitle),
                         s.CatTypeID == null ? null : new XAttribute("CatTypeID", s.CatTypeID),
                         s.shortDesc == null ? null : new XAttribute("shortDesc", s.shortDesc),
                         s.longDesc == null ? null : new XAttribute("longDesc", s.longDesc),
                         s.CatImage == null ? null : new XAttribute("CatImage", s.CatImage)));
                try {
                    string i = sub.Element("Category").Value;
                    c.Add(sub);
                    FillSubCategories(sub);
                } catch (Exception) {
                    continue;
                }
            } catch (Exception) {
                continue;
            }
        }
    }
  • 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-05-19T01:38:49+00:00Added an answer on May 19, 2026 at 1:38 am

    That doesn’t look good. Are you sure that there are things that are not in any category in your table? If so you shouldn’t be selecting them since you are doing all the operation on the categoryId. I guess as Chris mentioned, you are getting this exception because you are on a parent category which doesn’t have another parent. You need to put an if condition and not parse if it is a parent category. Something like this:

    private XElement FillSubCatagories(XElement cat) {
            IEnumerable<XElement> list = cat.Descendants();
            int catTypeID = c.Attribute("CatTypeID") == null ? 1 : Int32.Parse(cat.Attribute("CatTypeID").Value);
            foreach (XElement c in list) {
            if(!String.IsNullOrEmpty(cat.Attribute("CatID").Value))
            {
                int parentID = Int32.Parse(cat.Attribute("CatID").Value);
                XElement sub = new XElement("sub",
                    from s in db.Categories
                    where s.CatTypeID.Equals(catTypeID) && s.ParentID.Equals(parentID)
                    select new XElement("Category",
                         s.CatID == null ? null : new XAttribute("CatID", s.CatID),
                         s.ParentID == null ? null : new XAttribute("ParentID", s.ParentID),
                         s.CatTitle == null ? null : new XAttribute("CatTitle", s.CatTitle),
                         s.CatTypeID == null ? null : new XAttribute("CatTypeID", s.CatTypeID),
                         s.shortDesc == null ? null : new XAttribute("shortDesc", s.shortDesc),
                         s.longDesc == null ? null : new XAttribute("longDesc", s.longDesc),
                         s.CatImage == null ? null : new XAttribute("CatImage", s.CatImage)));
                c.Add(sub);
                FillSubCatagories(c);
                }
            }
            return cat;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use wget to recursively grab only the .jpg files from a
Attempting to print out a list of values from 2 different variables that are
Attempting to deploy a MOSS solution to a UAT server from dev server for
When attempting to open a project from source control on a newly formatted pc,
I'm attempting to add an event handler for every control on my form. The
I'm attempting at creating a menu from a table using the Suckerfish css menu
I'm attempting to recursively copy all files and directories and here is my class
I'm attempting to use a similar Category implementation to this one in the Django
Im attempting to add voteup/votedown to one of my sites, however Im having a
I am attempting to pull some information from my tnsnames file using regex. I

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.