Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq到xml查询:尝试检索子元素时发生对象引用未设置错误_C#_Linq To Xml - Fatal编程技术网

C# linq到xml查询:尝试检索子元素时发生对象引用未设置错误

C# linq到xml查询:尝试检索子元素时发生对象引用未设置错误,c#,linq-to-xml,C#,Linq To Xml,我正在尝试检索所有子元素,但获取System.Collections.ListDictionaryInternal。对象引用未设置为对象错误的实例 我的c代码根据通过的测试id和类别id检索所有问题:- public static List<Questions> GetQuestion_Catgy(int test_id, int ctgy_id) { try { XDocument da

我正在尝试检索所有子元素,但获取System.Collections.ListDictionaryInternal。对象引用未设置为对象错误的实例

我的c代码根据通过的测试id和类别id检索所有问题:-

public static List<Questions> GetQuestion_Catgy(int test_id, int ctgy_id)
        {
            try
            {
                XDocument data = XDocument.Load(docurl);
                return (from exm in data.Descendants("test_details")
                        where exm.Attribute("id").Value.Equals(test_id.ToString())
                        from ctgy in exm.Descendants("category")
                        where ctgy.Attribute("id").Value.Equals(ctgy_id.ToString())
                        orderby (int)ctgy.Attribute("id")
                        select new Questions
                        {
                            quesID = Convert.ToInt32(ctgy.Attribute("id").Value),
                            quesSTRING = ctgy.Attribute("ques").Value,
                            quesRATE = Convert.ToInt32(ctgy.Attribute("rating").Value),
                            quesOPT1 = (string)ctgy.Element("opt1").Value,
                            quesOPT2 = (string)ctgy.Element("opt2").Value,
                            quesOPT3 = (string)ctgy.Element("opt3").Value,
                            quesOPT4 = (string)ctgy.Element("opt4").Value,
                            quesANS = Convert.ToInt32(ctgy.Element("ans").Value),
                            quesIMG = (string)ctgy.Element("img").Value
                        }).ToList();
            }
            catch (Exception ex)
            {
                throw new ArgumentException(ex.Data + "\n" + ex.Message);
            }
        }
我的xml

<test_details id="1" name="test exam" time="30" marks="100" difficulty="1">
    <category id="1" name="HTML">
      <question id="1" ques="what is HTML ?" rating="5">
        <opt1>Markup Language</opt1>
        <opt2>Scripting Language</opt2>
        <opt3>Server-Side Lanugae</opt3>
        <opt4>Client-Side Language</opt4>
        <ans>1</ans>
        <img>null</img>
      </question>
      <question id="2" ques="what is LMTH ?" rating="5">
        <opt1>Markup Language</opt1>
        <opt2>Scripting Language</opt2>
        <opt3>Server-Side Lanugae</opt3>
        <opt4>Client-Side Language</opt4>
        <ans>2</ans>
        <img>null</img>
      </question>
    </category>
    <category id="2" name="C#" />
  </test_details>
没有这样的属性

在执行以下操作之前,还要执行空检查

(string)ctgy.Element("opt2").Value,

您的错误在这一行:

from ctgy in exm.Descendants("category")
exm元素与您的类别处于同一级别。您需要用数据替换exm

例如:

from ctgy in data.Descendants("category")

如果您想访问ques属性,那么看起来您需要进一步访问'question'元素。ctgy将不会有问题。

通过重新调用包装的异常,您将丢失原始堆栈跟踪。如果你没有处理异常,让它自己传播。原始堆栈跟踪在查找null ref时可能也很有用。查询检索问题元素,但问题在选择和分配部分。
from ctgy in data.Descendants("category")