2

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;
            }
        }
    }
Fozz
  • 167
  • 3
  • 16
  • Its possible, however the actual data I should be able to go at least 3 more levels for categories. What I did notice though is that the first time I go through the list my XElement is a category element with attributes beneath it, however the second time through i add the sub element along with its category element, meaning on cat i wouldnt have the attribute. So I'm guessing either just pass in the subbed category element or set up some kind of first run pull attribute from cat, second and all subsequent runs pull the descendents attribute. However I think that will get confusing fast. – Fozz Jan 10 '11 at 22:42
  • See http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net – John Saunders Jan 11 '11 at 19:53

3 Answers3

0

There is no attribute "CatId" on the node specified by "cat". Maybe you've hit the top of the hierarchy?

Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71
0

The problem is that cat.Attribute("CatID") is null. You are doing a cat.Attribute("CatID").Value on a null gives you that error.

You could put in a check saying that if it is not null then proceed. Or was there another question?

VoodooChild
  • 9,776
  • 8
  • 66
  • 99
0

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;
    }
Pabuc
  • 5,528
  • 7
  • 37
  • 52
  • Doesn't your if statement break the recursion though, and simply return? At least I'm pretty sure thats what happens in java.(I'd have to test that. - And I know c# isn't java its just where I came from before c#) – Fozz Jan 10 '11 at 22:46