![](/img/trans.png)
[英]C# ASP.NET: 'Object reference not set to an instance of an object.' System.Xml.Linq.XElement.Attribute(…) returned null
[英]C# Recursive method returning null reference, object reference not set to an instance of an object. XElement Objects
尝试递归地添加XElement,从而逐步遍历类别列表。
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时,问题就来了。 在int parentID = Int32.Parse(cat.Attribute("CatID").Value);
返回nullreferenceException-“对象引用未设置为对象的实例”
仍然习惯于来自Java的c#,因此请保持柔和。 我敢肯定这是一个明显的错误,但我还没有看到一个明确的理由。
<<<<<<<<< >>>>>>>>>>>>>>>>编辑的新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;
}
}
}
“ cat”指定的节点上没有属性“ CatId”。 也许您已经达到了层次结构的顶端?
问题是cat.Attribute("CatID")
为null。 您正在执行cat.Attribute("CatID").Value
null上的值会给您该错误。
您可以检查一下是否不为空,然后继续。 还是还有其他问题?
不好看。 您确定表中没有任何类别的东西吗? 如果是这样,您就不应该选择它们,因为您正在对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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.