这里是做这件事的一种方法:
using System;
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main()
{
String xml = @"<Pricing>
<MPrice>
<Id>0079</Id>
<Price>
<Price>31.25</Price>
<StartDt>2009-8-01</StartDt>
<EndDt>2009-08-26</EndDt>
</Price>
<Price>
<ListPrice>131.25</ListPrice>
<StartDt>2009-08-26</StartDt>
<EndDt>9999-12-31</EndDt>
</Price>
</MPrice>
</Pricing>";
var priceInfo = from e in XElement.Parse(xml).Elements("MPrice").Elements("Price")
let start = DateTime.Parse(e.Descendants("StartDt").FirstOrDefault().Value)
let end = DateTime.Parse(e.Descendants("EndDt").FirstOrDefault().Value)
where start < DateTime.Now && end > DateTime.Now
select new { Id = e.Parent.Element("Id").Value, ListPrice = e.Element("ListPrice").Value };
Console.WriteLine(priceInfo.FirstOrDefault().Id);
Console.WriteLine(priceInfo.FirstOrDefault().ListPrice);
}
}
输出:
0079
131.25
请注意,需要有更多的错误检查比这个例子提供。我会特别添加检查日期时间的解析(可能通过使用包装DateTime.TryParseExact
的函数)。
编辑:如果你想使用XDocument
代替XElement
,你将需要对查询了微妙的变化(注意Descendants
方法,而不是Elements
方法的用法):
var priceInfo = from e in XDocument.Parse(xml).Descendants("MPrice").Elements("Price")
let start = DateTime.Parse(e.Descendants("StartDt").FirstOrDefault().Value)
let end = DateTime.Parse(e.Descendants("EndDt").FirstOrDefault().Value)
where start < DateTime.Now && end > DateTime.Now
select new { Id = e.Parent.Element("Id").Value, ListPrice = e.Element("ListPrice").Value };
请记住,除非您将XML用作真实文档,否则不需要使用XDocument
。在大多数情况下,XElement
类型就足够了。
编辑#2:如果你想从磁盘加载XDocument
然后用这个办法:
using System;
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main()
{
XDocument document = XDocument.Load(@"d:\test.xml");
var priceInfo = from e in document.Descendants("MPrice").Elements("Price")
let start = DateTime.Parse(e.Descendants("StartDt").FirstOrDefault().Value)
let end = DateTime.Parse(e.Descendants("EndDt").FirstOrDefault().Value)
where start < DateTime.Now && end > DateTime.Now
select new { Id = e.Parent.Element("Id").Value, ListPrice = e.Element("ListPrice").Value };
Console.WriteLine(priceInfo.FirstOrDefault().Id);
Console.WriteLine(priceInfo.FirstOrDefault().ListPrice);
}
}
我能做使用的XDocument代替的XElement相同的解析? – SDC 2009-09-03 17:37:49
是的,你可以!请参阅我的编辑。 – 2009-09-03 17:58:29
仍似乎没有工作。我正在使用: XDocument xmlDoc = XDocument.Load(“my.xml”); 现在,当我调用: XDocument.Parse(xml).Descendants(“MPrice”)。Elements(“Price”) 我收到一个错误,未将XDocument转换为字符串。有任何想法吗? – SDC 2009-09-03 19:57:56