【问题标题】:Not able to access a element using LINQ and XDocument无法使用 LINQ 和 XDocument 访问元素
【发布时间】:2012-06-15 06:56:26
【问题描述】:

我有一段 xml 作为 Web 服务的响应。

 <Production>
    <creator>...</creator>
    <creator.date_of_birth/>
    <creator.date_of_death/>
    <creator.history/>
    <creator.lref>426</creator.lref>
    <creator.qualifier/>
    <creator.role/>
    <creator.role.lref/>
    <production.notes/>
    <production.place>France</production.place>
    <production.place.lref>30</production.place.lref>
  </Production>
  <production.period>19th century</production.period>
  <production.period.lref>13</production.period.lref>
  <Production_date>
    <production.date.end>1863</production.date.end>
    <production.date.end.prec>circa.</production.date.end.prec>
    <production.date.start>1863</production.date.start>
    <production.date.start.prec>dated</production.date.start.prec>
  </Production_date>

下面是我获取值的 LINQ 代码:

allrecs = (from XElement c in recordSet.Descendants("recordList")
                where c.HasElements
                from x in c.Elements("record")
                select new CollectionRecord()
                {
                  production_place =  x.Element("Production").Element("production.place").Value,
                  production_period =  x.Element("production.period").Value,
                  production_start_date = x.Element("Production_date").Element("production.date.start").Value,
                  production_end_date = x.Element("Production_date").Element("production.date.end").Value,
                }).ToList <CollectionRecord>();

我发现的问题是 - 我无法获得“production.date.start”和“production.date.end”的值,但上述代码适用于“production.period”。 xml 结构和这些元素中的数据没有任何问题。根据我的理解,它不适用于任何类似 - “a.b.c”的元素,但它适用于 - “a.b”,因为我已经检查过其他类似元素并且它不起作用!

【问题讨论】:

  • 请再次检查您的 XML。 b'cas 我在粘贴 XMLFile 时遇到错误,它显示错误“XML 文档不能有多个根级别元素”也在这里验证您的 XML validome.org/xml/validate
  • @Shankar - 它只是粘贴在这里的文档的一个片段 - 只需将它包装在一个节点中就可以了
  • @SidP 好的...下面发布的答案已解决您的查询...或仍在搜索?

标签: c# asp.net linq linq-to-xml


【解决方案1】:

对我有用(我已将您的 Xlinq 代码保持不变 - 只是将 new 换成了匿名类型):

[TestMethod]
public void TestMethod1()
{
  XElement x = XElement.Parse(@"<record><Production> 
<creator>...</creator> 
<creator.date_of_birth/> 
<creator.date_of_death/> 
<creator.history/> 
<creator.lref>426</creator.lref> 
<creator.qualifier/> 
<creator.role/> 
<creator.role.lref/> 
<production.notes/> 
<production.place>France</production.place> 
<production.place.lref>30</production.place.lref> 
</Production> 
<production.period>19th century</production.period> 
<production.period.lref>13</production.period.lref> 
<Production_date> 
<production.date.end>1863</production.date.end> 
<production.date.end.prec>circa.</production.date.end.prec> 
<production.date.start>1863</production.date.start> 
<production.date.start.prec>dated</production.date.start.prec> 
</Production_date></record>");
  var rec = new 
              {
                production_place = x.Element("Production").Element("production.place").Value,
                production_period = x.Element("production.period").Value,
                production_start_date = x.Element("Production_date").Element("production.date.start").Value,
                production_end_date = x.Element("Production_date").Element("production.date.end").Value,
              };
  Assert.AreEqual("France", rec.production_place);
  Assert.AreEqual("19th century", rec.production_period);
  Assert.AreEqual("1863", rec.production_start_date);
  Assert.AreEqual("1863", rec.production_end_date);
}

我想知道您的 CollectionRecord 类是否正在使用属性,以及您是否为这两个违规值正确编写了 set 方法?

(更新)由于您的 get/sets 是“标准的”,因此它可能是一些简单的代码,例如某处将这些属性物理设置为 null 的某处。

【讨论】:

  • 下面是我对这两个值的获取/设置方法。 '公共字符串 Production_start_date { get { return production_start_date; } 设置 { production_start_date = value; } } 公共字符串 Production_end_date { 获取 { 返回 production_end_date; } 设置 { production_end_date = 值; } }'
  • 那么在您发布的代码之外肯定还有其他内容。我的代码在这里使用与您相同的 XML 和相同的 Xlinq 操作;我所做的只是使用CollectionRecord,因为我没有那个课程;哦,这不是一个集合。我建议您将我的代码复制并粘贴到测试中,然后将该匿名类型更改为CollectionRecord 实例。如果测试通过,那么我敢打赌,在你设置这些属性之后,它们会被清空。
  • 我再次尝试了上面的代码,但没有使用我的 CollectionRecord `var query = (from XElement c in recordSet.Descendants("recordList") where c.HasElements from x in c.Elements("record" ) 选择新的{ production_start_date = x.Element("Production_date").Element("production.date.start").Value, production_end_date = x.Element("Production_date").Element("production.date.end")。 Value, });` 它不起作用并说“对象引用未设置为对象的实例”
  • 这是我们正在讨论的问题之外的问题 - 这个NullReferenceException 一定与您的 XML 有关 - 请将我的代码完全粘贴到单元测试类中并替换匿名新的使用您的 CollectionRecord 并验​​证它是否有效。 当它通过时(它会),您会发现问题不是您的 Linq to XML 代码(我已经复制了完全)但是除了您在此处发布的内容之外的其他内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多