使用LINQ解析XML数据

3phpmpom  于 2022-12-06  发布在  其他
关注(0)|答案(2)|浏览(137)

我是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>
toiithl6

toiithl61#

以下是一种方法:

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的解析添加检查(可能通过使用 Package 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);
    }
}
pw9qyyiw

pw9qyyiw2#

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是一个字符串,这应该是可行的。
你应该做一些检查,以确保日期是正确的等等.. .,但这是一个快速的例子,应该工作的给定XML例子,如果开始日期被更改为2009-9-03或当前日期的日期。

相关问题