这是我的 XML 文件:
<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>
我正在将数据加载到 XDocument 中,然后从 XDocument 中检索数据并将其添加到 Book 类中,但我无法获取日期。我想检索友好日期。
这是我所拥有的:
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 =
}
);
我怎样才能得到友好的约会对象?
最佳答案
PubDate = DateTime.ParseExact(xml.Root.Elements("pubDates")
.Elements("date")
.Where(n => n.Attribute("format").Value == "standard")
.FirstOrDefault()
.Value
, "yyyy-mm-dd", CultureInfo.InvariantCulture);
关于c# - 从 XDocument 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2916815/