5

我已经编写了一个 LINQ to XML 查询,它可以满足我的要求,但它看起来很丑。我想知道,你们如何以一种看起来不那么花哨的方式格式化以下查询?

抱歉,如果我的示例有点冗长。

我正在查询的 XML 文档具有以下结构:

<?xml version="1.0" encoding="iso-8859-1"?>
<newsitem itemid="1" id="root" date="1996-08-20" xml:lang="en">
    <title>A title</title>
    <headline>A headline</headline>
    <dateline>A dateline</dateline>
    <text>
        Some text
    </text>
    <metadata>
        <codes class="">
            <code code="">
                <editdetail attribution=""/>
            </code>
        </codes>
        <dc element="dc.date.created" value=""/>
        <dc element="dc.publisher" value=""/>
        <dc element="dc.date.published" value=""/>
        <dc element="dc.source" value=""/>
        <dc element="dc.creator.location" value=""/>
        <dc element="dc.creator.location.country.name" value=""/>
        <dc element="dc.source" value=""/>
    </metadata>
</newsitem>

以及相应的 LINQ 查询:

XElement dummy = new XElement("dummy");
var query = from article in newsdoc.Elements("newsitem").DefaultIfEmpty(dummy)
            select new
            {
                NewsItemID = (int)article.Attribute("itemid"),
                Date = (DateTime)article.Attribute("date"),
                Title = (string)article.Element("title"),
                Headline = (string)article.Element("headline"),
                ByLine = (string)article.Element("byline"),
                DateLine = (string)article.Element("dateline"),
                NewsText = (string)article.Element("text"),
                Publisher = (string)article.Elements("metadata").Elements("dc").Where(x => (string)x.Attribute("element") == "dc.publisher").Attributes("value").DefaultIfEmpty().ElementAt(0),
                DatePublished = (DateTime)article.Elements("metadata").Elements("dc").Where(x => (string)x.Attribute("element") == "dc.date.published").Attributes("value").DefaultIfEmpty().ElementAt(0),
                Source = (string)article.Elements("metadata").Elements("dc").Where(x => (string)x.Attribute("element") == "dc.source").Attributes("value").DefaultIfEmpty().ElementAt(0),
                CreatorLocation = (string)article.Elements("metadata").Elements("dc").Where(x => (string)x.Attribute("element") == "dc.creator.location").Attributes("value").DefaultIfEmpty().ElementAt(0),
                CreatorLocationCountryName = (string)article.Elements("metadata").Elements("dc").Where(x => (string)x.Attribute("element") == "dc.creator.location.country.name").Attributes("value").DefaultIfEmpty().ElementAt(0),
                Codes = article.Elements("metadata").Elements("codes").Elements("code").Attributes("code").DefaultIfEmpty()
            }; 

谢谢!

4

1 回答 1

4

主要的“丑陋”是底部的东西。我可能会添加一个扩展方法(或只是一个实用方法) - 比如:

    public static XAttribute GetMetadata(this XElement parent, string key)
    {
        return parent.Elements("metadata").Elements("dc")
                 .FirstOrDefault(x => x.Attribute("element").Value == key)
                 .Attribute("value");
    }

那么你应该能够使用类似的东西:

Publisher = (string)article.GetMetadata("dc.publisher");

(未选中)

于 2008-11-21T05:01:26.217 回答