使用LINQ解析XML数据

SDC*_*SDC 4 c# xml linq parsing

我是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>
Run Code Online (Sandbox Code Playgroud)

And*_*are 7

这是一种方法:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

0079
131.25
Run Code Online (Sandbox Code Playgroud)

请注意,需要比此示例提供的错误检查更多.我特意添加了解析日期时间的检查(可能是通过使用包装的函数DateTime.TryParseExact).

编辑:如果要使用XDocument而不是a 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 };
Run Code Online (Sandbox Code Playgroud)

请记住,XDocument除非您将XML作为真实文档使用,否则不需要使用.在大多数情况下,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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您已经将XML加载到`XDocument`中,那么您不需要调用`XDocument.Parse`.我将再次编辑以演示如何使用磁盘上的XML文件. (2认同)