Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用LINQ解析XML数据_C#_Xml_Linq_Parsing - Fatal编程技术网

C# 用LINQ解析XML数据

C# 用LINQ解析XML数据,c#,xml,linq,parsing,C#,Xml,Linq,Parsing,我是LINQ的新手,迫切需要尽快完成这个项目。我需要返回的id与正确的价格信息为今天的日期为每米。任何建议都将不胜感激!以下是XML的一个示例: <Pricing> <MPrice> <Id>0079</Id> <Price> <Price>31.25</Price> <StartDt>2009-8-01</StartDt>

我是LINQ的新手,迫切需要尽快完成这个项目。我需要返回的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>

0079
31.25
2009-8-01
2009-08-26
131.25
2009-08-26
9999-12-31
0081
131.25
2009-8-01
2009-08-26
231.25
2009-08-26
9999-12-31
字符串id=yourDocument
.子公司(“定价”)
.后代(“MPrice”)
.式中(i=>i.价格)
.后代(“StartDt”)
.Select(s=>DateTime.Parse(s.Value))
.FirstOrDefault().Date==DateTime.Now.Date)
.Select(i=>i.Id.FirstOrDefault().Value)
.FirstOrDefault();
假设id是一个字符串,这应该可以工作。你可以把它设为int


你应该做一些检查以确保日期正确等等,但这是一个快速示例,如果开始日期更改为2009-9-03或当前日期,该示例应适用于给定的Xml示例。

以下是一种方法:

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);
    }
}
请注意,需要比本例提供的更多的错误检查。我将特别添加对datetime解析的检查(可能通过使用包装
datetime.TryParseExact
的函数)

编辑:如果要使用
XDocument
而不是
XElement
,则需要对查询进行细微的更改(注意使用
子体
方法而不是
元素
方法):


我可以使用XDocument而不是XElement进行同样的解析吗?似乎仍然不起作用。我使用的是:xdocumentxmldoc=XDocument.Load(“my.xml”);现在,当我调用:XDocument.Parse(xml).substanders(“MPrice”).Elements(“Price”)时,我收到一个错误,其中未将XDocument转换为字符串。有什么想法吗?如果您已经将XML加载到
XDocument
中,那么就不需要调用
XDocument.Parse
。我将再次编辑以演示如何使用磁盘上的XML文件进行编辑。感谢您的时间和帮助。项目几乎完成了,我学到了一些新东西。谢谢
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
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 };
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);
    }
}