开发者

Get data from XDocument

开发者 https://www.devze.com 2022-12-31 23:33 出处:网络
So here\'s my XML file: <book> <title>Book Title</title> <author>Book Author</author>

So here's my XML file:

<book>
    <title>Book Title</title>
    <author>Book Author</author>
    <pubDates>
        <date format="standard">1991-01-15</date> 
        <date format="friendly">January 1991</date> 
    </pubDates>
</book>

I'm loading the dat开发者_开发问答a into an XDocument, then retrieving it from the XDocument and adding it into a Book class, but I'm having trouble getting the date. I'd like to retrieve the friendly date.

Here's what I have:

XDocument xml = XDocument.Load("http://www.mysite.com/file.xml");

List<Book> books = new List<Book>();
books.Add(new Book
                {
                    Title = xml.Root.Element("title").Value,
                    Author = xml.Root.Element("author").Value,
                    //PubDate = 
                }
            );

How can I get the friendly date?


PubDate = DateTime.ParseExact(xml.Root.Elements("pubDates")
.Elements("date")
.Where(n => n.Attribute("format").Value == "standard")
.FirstOrDefault()
.Value
, "yyyy-mm-dd", CultureInfo.InvariantCulture);


I haven't tested this, but it should look something like this:

from node in xml.DescendantNodes("pubDates").DescendantNodes("date")
where node.Attribute("format").Value == "friendly"
select node.Value.FirstOrDefault()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号