首页 > 解决方案 > C# 试图获取嵌套节点的 XML 内部文本

问题描述

我可以成功加载 XMl 文档并遍历节点。一旦我得到我想要的节点,我就开始设置值。如何处理嵌套节点?

这是xml:

<incident>
<id>1234</id>
<number>5678</number>
<name>This is a name</name>
<state>Awaiting Input</state>
<priority>Medium</priority>
<category>
    <id>99999</id>
    <name>Applications</name>
    <default_tags>applications</default_tags>
    <parent_id nil="true" />
    <default_assignee_id nil="true" />
</category>

这是一些C#:

   id = node.SelectSingleNode("id").InnerText;  //works fine
   number = node.SelectSingleNode("number").InnerText;  //works fine

   name = node.SelectSingleNode("name").InnerText;  //works fine
   descHTML = node.SelectSingleNode("description").InnerText;  //works fine
   desc = node.SelectSingleNode("description_no_html").InnerText;  //works fine
   state = node.SelectSingleNode("state").InnerText;  //works fine
   priority = node.SelectSingleNode("priority").InnerText;  //works fine

   catagoryID = node.SelectSingleNode("category/id").InnerText; // null reference error
   catagoryName = node.SelectSingleNode("category/name").InnerText; // null reference error
   catagoryTags = node.SelectSingleNode("category/default_tags").InnerText; // null reference error

标签: c#xml

解决方案


如果您正在阅读可能存在或不存在的不同元素,请?.在方法之后使用SelectSingleNode。这将确保您不会收到错误消息Object Reference Not Set to an Instance of an object

?.本质上,在继续评估下一个方法或属性之前检查是否存在值。

string xml = @"<incident>
<id>1234</id>
<number>5678</number>
<name>This is a name</name>
<state>Awaiting Input</state>
<priority>Medium</priority>
<category>
    <id>99999</id>
    <name>Applications</name>
    <default_tags>applications</default_tags>
    <parent_id nil=""true"" />
    <default_assignee_id nil=""true"" />
</category>
</incident>";

        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        var node = doc.DocumentElement;

        var id = node.SelectSingleNode("id")?.InnerText;  //works fine
       var number = node.SelectSingleNode("number")?.InnerText;  //works fine

       var name = node.SelectSingleNode("name")?.InnerText;  //works fine
       var descHTML = node.SelectSingleNode("description")?.InnerText;  //ERRORS because there is no description.
       var desc = node.SelectSingleNode("description_no_html")?.InnerText;  //works fine
       var state = node.SelectSingleNode("state")?.InnerText;  //works fine
       var priority = node.SelectSingleNode("priority")?.InnerText;  //works fine

       var catagoryID = node.SelectSingleNode("//category/id")?.InnerText; // null reference error
       var catagoryName = node.SelectSingleNode("//category/name")?.InnerText; // null reference error
       var catagoryTags = node.SelectSingleNode("//category/default_tags")?.InnerText; // null reference error

        Console.WriteLine($"name: {name}");
        Console.WriteLine($"descHTML: {descHTML}");
        Console.WriteLine($"desc: {desc}");
        Console.WriteLine($"state: {state}");
        Console.WriteLine($"priority: {priority}");
        Console.WriteLine($"catagoryID: {catagoryID}");
        Console.WriteLine($"catagoryName: {catagoryName}");
        Console.WriteLine($"catagoryTags: {catagoryTags}");

输出它打印出来

name: This is a name
descHTML: 
desc: 
state: Awaiting Input
priority: Medium
catagoryID: 99999
catagoryName: Applications
catagoryTags: applications

#dotnetfiddle 上的代码


推荐阅读