【发布时间】:2018-06-02 11:17:53
【问题描述】:
我试图在我的 XML 中找到最大的 DateTime 值。
这是一个 XML 示例:
<?xml version="1.0" encoding="utf-16"?>
<?xml-stylesheet type='text/xsl' href='http://127.0.0.123/sitemaps/xmltemplate/main-sitemap.xsl'?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://127.0.0.123/?????</loc>
<lastmod>2018-05-13</lastmod>
<changefreq>daily</changefreq>
<priority>0.1</priority>
</url>
<url>
<loc>http://127.0.0.123/?????-????</loc>
<lastmod>2018-05-26</lastmod>
<changefreq>daily</changefreq>
<priority>0.1</priority>
</url>
</urlset>
这是我尝试使用的代码:
XDocument xdoc = XDocument.Load(FullAddressXML);
var maxId = xdoc.Elements("url").Select(x => new {
MaxDateTime = x.Descendants("lastmod").Max(y=>(DateTime)y)
});
当我运行这个时,maxId 是空的。
【问题讨论】:
-
请您提供比“它有效”更多的细节吗?编译失败了吗?它会编译但抛出异常吗? (如果是这样,什么异常?)它不会抛出异常,但没有给您期望的答案吗?
-
编译完成但maxId为空
-
所以请将该详细信息编辑到问题中。只说“它不起作用”的问题绝不是一个好问题。
标签: c# xml linq-to-xml xelement