使用XML获取XML元素值到Linq

时间:2014-11-03 20:38:06

标签: c# xml linq linq-to-xml

XML im查询的片段是

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<metadata created="2014-11-03T18:13:02.769Z" xmlns="http://example.com/ns/mmd-2.0#" xmlns:ext="http://example.com/ns/ext#-2.0">
    <customer-list count="112" offset="0">
        <customer id="5f6ab597-f57a-40da-be9e-adad48708203" type="Person" ext:score="100">
            <name>Bobby Smith</name>
            <gender>male</gender>
            <country>US</country>
            <birth-span>
                <start>1965-02-18</start>
                <end>false</end>
            </birth-span>

为获取元素而编写的代码是

    GetCustomer = from c in XDoc.Descendants(ns + "customer")
                      select
                      new Customer
                      {
                          Name = c.Element(ns + "name").ToString(),
                          Gender = Convert.ToString(c.Element(ns + "gender")),
                          BeginDate = c.Elements("birth-span").Any() ? c.Element("start").Value.ToString() : "No data found"

我遇到的问题是birth-span,如果存在跨越范围,我似乎永远不会得到这个值(有些记录不包含出生范围元素)。对于包含birth-span元素的记录,我添加了不起作用的命名空间变量(抛出了未将对象设置为实例的错误)

 BeginDate = c.Elements(ns + "birth-span").Any() ? c.Element(ns + "start").Value.ToString() : "No data found"

我添加了不同的变体但是我得到的值没有找到数据或错误(似乎是对象未设置为实例)。谁能看到我做错了什么?

1 个答案:

答案 0 :(得分:3)

您通过这样做将start视为c(或customer)的直接后裔:

BeginDate = c.Elements("birth-span").Any() ?
    c.Element("start").Value.ToString() :
    "No data found"

如果你真的希望将start视为birth-span元素的直接后代,请执行以下操作:

BeginDate = c.Elements("birth-span").Any() ?
    c.Element("birth-span").Element("start").Value.ToString() :
    "No data found"

但仍有改进的余地。是零还是一个birth-span元素,还是零或多个?由于它似乎是零或一,你可以做到这一点并让它更清晰:

var customers = from c in doc.Descendants("customer")
                let birthSpan = c.Element("birth-span")
                select new
                {
                    Name = c.Element("name").Value,
                    Gender = c.Element("gender").Value,
                    BeginDate = birthSpan == null ?
                        "No data found" :
                        birthSpan.Element("start").Value
                };

此外,无需在其中任何一个上致电Convert.ToString.ToString()。如果您想要string.Value()会这样做。如果您确实希望将其键入其他内容,请直接转换它并让转换运算符执行此操作:

var customers = from c in doc.Root.Descendants("customer")
                let birthSpan = c.Element("birth-span")
                select new
                {
                    Name = (string)c.Element("name"),
                    Gender = (string)c.Element("gender"),
                    BeginDate = birthSpan == null ?
                        (DateTime?)null :
                        (DateTime?)birthSpan.Element("start")
                };

有一篇关于here的精彩文章。