[英]Parsing XML Data with LINQ
我是LINQ的新手,迫切需要快速完成這個項目。 我需要使用每個MPrice的今天日期的正確價格信息返回id。 任何建議都非常感謝! 以下是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>
<MPrice>
<Id>0081</Id>
<Price>
<Price>131.25</Price>
<StartDt>2009-8-01</StartDt>
<EndDt>2009-08-26</EndDt>
</Price>
<Price>
<ListPrice>231.25</ListPrice>
<StartDt>2009-08-26</StartDt>
<EndDt>9999-12-31</EndDt>
</Price>
</MPrice>
</Pricing>
這是一種方法:
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);
}
}
string id = yourDocument
.Descendants("Pricing")
.Descendants<XElement>("MPrice")
.Where<XElement>(i => i.Descendants("Price")
.Descendants<XElement>("StartDt")
.Select<XElement, DateTime>(s => DateTime.Parse(s.Value))
.FirstOrDefault<DateTime>().Date == DateTime.Now.Date)
.Select<XElement, string>(i => i.Descendants("Id").FirstOrDefault<XElement>().Value)
.FirstOrDefault<string>();
這應該假設id是一個字符串。 你可以把它變成一個int。
你應該做一些檢查,以確保日期是正確的等。 。 ,但這是一個快速示例,如果“開始日期”更改為2009-9-03或當前日期日期,則應對給定的Xml示例起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.