选择 XML 中可能存在也可能不存在的子元素列表

发布于 2024-10-06 10:50:44 字数 2125 浏览 4 评论 0原文

我拥有的是从 Web 服务收到的 XML,它代表问题列表。这些问题按类型细分,表明它们应如何在网络上显示。例如:

<step id="109025">
  <stepinf enabled="Yes" errors="0" id="First Name" mandatory="Yes" name="sti_115511" type="FIL">
    <field_label>First Name</field_label>
    <screen_value></screen_value>
  </stepinf>
  <stepinf enabled="Yes" errors="0" id="Last Name" mandatory="Yes" name="sti_115513" type="FIL">
    <field_label>Last Name</field_label>
    <screen_value></screen_value>
  </stepinf>
  <stepinf enabled="Yes" errors="0" id="State" mandatory="Yes" name="sti_109257" type="STE">
    <field_label>State</field_label>
    <screen_value></screen_value>
    <options_list>
      <option label="AK">AK - Alaska</option>
      <option label="AL">AL - Alabama</option>
      <option label="AR">AR - Arkansas</option>
      <option label="AS">AS - American Samoa (Terr.)</option>
      <option label="AZ">AZ - Arizona</option>
      ...
    </options_list>
  </stepinf>
</step>

“STE”类型表示它将在网络上显示为选择框。

我正在填充一个列表<>我通过执行以下操作创建的自定义类型:

var stepinfList = (from stepinf in xdoc.Descendants("stepinf")
    select new Question
    {
       TextID = stepinf.Attribute("id").Value,
       Type = stepinf.Attribute("type").Value,
       Name = stepinf.Attribute("name").Value,
       Label = stepinf.Element("field_label").Value,
       Required = stepinf.Attribute("mandatory").Value,
       ErrorCount = int.Parse(stepinf.Attribute("errors").Value)
    }).ToList();

我迷失的地方是我不知道如何将选项子元素添加到我的结果中。我尝试在名为 Options 的问题类型中创建一个属性,将其定义为 IDictionary,然后在 LINQ 查询和 ToDictionary 扩展中使用子选择。

Options = (from option in xdoc.Element("options_list").Elements("option")
    select option)
    .ToDictionary(x => x.Attribute("label").Value, x => x.Value)

这不起作用,因为我认为它会轰炸没有子 option_list 元素的 stepinf 记录。无论如何,当我运行页面时,我在 LINQ 语句上收到“对象引用未设置到对象实例”。

恐怕这超出了我当前的 LINQ 技能,因此我们将不胜感激。

What I have is an XML we receive from a web service which represents a list of questions. The questions are broken down by type which indicates how they should be displayed on the web. For instance:

<step id="109025">
  <stepinf enabled="Yes" errors="0" id="First Name" mandatory="Yes" name="sti_115511" type="FIL">
    <field_label>First Name</field_label>
    <screen_value></screen_value>
  </stepinf>
  <stepinf enabled="Yes" errors="0" id="Last Name" mandatory="Yes" name="sti_115513" type="FIL">
    <field_label>Last Name</field_label>
    <screen_value></screen_value>
  </stepinf>
  <stepinf enabled="Yes" errors="0" id="State" mandatory="Yes" name="sti_109257" type="STE">
    <field_label>State</field_label>
    <screen_value></screen_value>
    <options_list>
      <option label="AK">AK - Alaska</option>
      <option label="AL">AL - Alabama</option>
      <option label="AR">AR - Arkansas</option>
      <option label="AS">AS - American Samoa (Terr.)</option>
      <option label="AZ">AZ - Arizona</option>
      ...
    </options_list>
  </stepinf>
</step>

The type "STE" indicates that it will display on the web as a select box.

I am populating a List<> of a custom type I created by doing the following:

var stepinfList = (from stepinf in xdoc.Descendants("stepinf")
    select new Question
    {
       TextID = stepinf.Attribute("id").Value,
       Type = stepinf.Attribute("type").Value,
       Name = stepinf.Attribute("name").Value,
       Label = stepinf.Element("field_label").Value,
       Required = stepinf.Attribute("mandatory").Value,
       ErrorCount = int.Parse(stepinf.Attribute("errors").Value)
    }).ToList();

Where I am getting lost at is I have no idea how to get the option sub-elements into my results. I tried creating a property in the Question type named Options which I defined as an IDictionary and then utilized a sub-select in my LINQ query and the ToDictionary extension.

Options = (from option in xdoc.Element("options_list").Elements("option")
    select option)
    .ToDictionary(x => x.Attribute("label").Value, x => x.Value)

This didn't work as I think it bombs on the stepinf records that do not have child option_list elements. Anyway, I get "Object reference not set to an instance of an object" on the LINQ statement when I run the page.

I'm afraid this is above my current LINQ skill set so any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我乃一代侩神 2024-10-13 10:50:44

这没有用,因为我认为它会爆炸
在不存在的stepinf记录上
有子 option_list 元素。

您的评估是正确的,因此您在查询之前需要先检查option_list是否存在。检查是否为 null 并返回字典或相应地返回 null。

试试这个:

Options = stepinf.Element("options_list") == null ? null :
              stepinf.Element("options_list").Elements("option")
                     .ToDictionary(x => x.Attribute("label").Value, x => x.Value)

This didn't work as I think it bombs
on the stepinf records that do not
have child option_list elements.

Your assessment is correct, so you need to check whether option_list exists before querying it. Check if it's null and return the dictionary or return null accordingly.

Try this:

Options = stepinf.Element("options_list") == null ? null :
              stepinf.Element("options_list").Elements("option")
                     .ToDictionary(x => x.Attribute("label").Value, x => x.Value)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文