简体   繁体   English

解析此XML时,出现Null Reference异常(包括C#代码)

[英]Null Reference exception when parsing this XML (C# code included)

I'm trying to get familiar with parsing an XML document with C#. 我正在尝试熟悉使用C#解析XML文档。 My XML file looks like this: 我的XML文件如下所示:

<title>
  <titledata titletype="standard">
    <currentid>18097</currentid>
  </titledata>
  <resourcedata>
    <resource id="36" resourcetype="image">
        <name>nextBtn.gif</name>
        <relativelink>images/nextBtn.gif</relativelink>
        <resourceflags>0</resourceflags>
        <lastupdated>1367612131</lastupdated>
    </resource>
    <resource id="37" resourcetype="image">
        <name>nextOver.gif</name>
        <relativelink>images/nextOver.gif</relativelink>
        <resourceflags>0</resourceflags>
        <lastupdated>1367612131</lastupdated>
    </resource>
    <resource id="38" resourcetype="image">
        <name>nextDown.gif</name>
        <relativelink>images/nextDown.gif</relativelink>
        <resourceflags>0</resourceflags>
        <lastupdated>1367612131</lastupdated>
    </resource>
  <resourcedata>
</title>

My code looks like this: 我的代码如下所示:

private void button1_Click(object sender, EventArgs e)
    {
        var ofd = new OpenFileDialog { Filter = "XML|*.xml" };
        if (ofd.ShowDialog() != DialogResult.OK) return;

        var xdoc = XDocument.Load(ofd.FileName);

        foreach (var element in xdoc.Descendants("resourcedata"))
        {
            var id = Convert.ToInt32(element.Attribute("id").Value);
            var resourceType = element.Attribute("resourcetype").Value;
            var name = element.Element("name").Value;
            var relativeLink = element.Element("relativeLink").Value;
            var resourceFlag = Convert.ToInt32(element.Element("resourcetype").Value);
            var lastUpdated = Convert.ToInt32(element.Element("lastupdated").Value);
            resourceFlag, lastUpdated);

            textBox1.Text += "ID: " + id + "\r\n";
            textBox1.Text += "Resource Type: " + resourceType + "\r\n";
            textBox1.Text += "Name: " + name + "\r\n";
            textBox1.Text += "Relative Link: " + relativeLink + "\r\n";
            textBox1.Text += "Resource Flag: " + resourceFlag + "\r\n";
            textBox1.Text += "Last Updated: " + lastUpdated + "\r\n";

        }
    }

The error I'm getting is "Null Reference Exception" on the following line: 我收到的错误是以下行上的“空引用异常”:

var id = Convert.ToInt32(element.Attribute("id").Value);

It's almost like I'm trying to access the wrong element at that point, because it seems that the attribute id doesn't exist. 这几乎就像我正在尝试访问错误的元素一样,因为似乎属性id不存在。 If that's the case, what do I need to do to fix my code? 如果是这种情况,我该怎么做才能修复我的代码? I'm just basically wanting to print the information for each resource in the XML file. 我只是想在XML文件中为每个资源打印信息。

Change your foreach source to: 将您的foreach来源更改为:

foreach (var element in xdoc.Descendants("resourcedata").Elements("resource"))

Otherwise you're iterating over <resourcedata> elements, which do not have attribute named id . 否则,您将遍历<resourcedata>元素,这些元素没有名为id属性。

PS. PS。 If the file looks like you showed us, you can try following as well: 如果该文件看起来像您向我们展示的,也可以尝试执行以下操作:

foreach (var element in xdoc.Root.Element("resourcedata").Elements("resource"))

Should be slightly faster than using Descendants . 应该比使用Descendants快一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM