C#递归方法返回空引用,对象引用未设置为对象的实例。 XElement对象

时间:2011-01-10 21:58:17

标签: c# .net recursion nullreferenceexception xelement

尝试递归添加逐步执行类别列表的XElements。

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();

这是第一个类别列表,现在我想递归拉出所有子类别并将它们嵌套在我的Xelements FillSubCatagories()方法中:

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;
    }

好吧,当我第二次通过方法运行foreach时问题就出现了。 On int parentID = Int32.Parse(cat.Attribute("CatID").Value);返回nullreferenceException - “对象引用未设置为对象的实例”

仍然习惯于来自java的c#,所以要温柔。我确定这是一个明显的错误,但我没有看到一个干净的理由。

&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&LT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;&GT;        EDITED 新的FillSubCategories()看起来像这样

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;
    }

这让我更进了一步,但我仍然最终命中了空。

编辑工作方法

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;
            }
        }
    }

3 个答案:

答案 0 :(得分:0)

“cat”指定的节点上没有属性“CatId”。也许你已经达到了层次结构的顶端?

答案 1 :(得分:0)

问题是cat.Attribute("CatID")为空。你在null上做cat.Attribute("CatID").Value给你的错误。

您可以输入一张支票,说明如果它不为空则继续。或者还有其他问题吗?

答案 2 :(得分:0)

看起来不太好看。您确定表中有任何类别的东西吗?如果是这样,您不应该选择它们,因为您正在对categoryId进行所有操作。我想正如克里斯提到的那样,你得到了这个例外,因为你所在的父类没有另一个父类。如果是父类,则需要设置if条件而不解析。像这样:

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;
    }