xml 和条件(按日期时间)

发布于 2024-10-23 22:44:21 字数 1404 浏览 1 评论 0原文

XML

<CalendarFairs> 
    <CalendarFair>
        <DateStart>2011-04-05T00:00:00</DateStart>
       <DateEnd>2011-04-09T00:00:00</DateEnd>
        <Title>aaaa</Title>   
       <IdExecutive>1</IdExecutive>
   </CalendarFair>
  <CalendarFair>
      <DateStart>2011-04-16T00:00:00</DateStart>
      <DateEnd>2011-04-19T00:00:00</DateEnd>
      <Title>bbb</Title>
      <IdExecutive>2</IdExecutive>
  </CalendarFair>
<CalendarFairs>

代码

  var elements = from element in doc.Descendants("CalendarFair")
                           where DateTime.Parse (element.Elements ("DateStart").ToString())==DateTime.Now 
                           select new
                           {
                               dateStart = element.Element("DateStart").Value,
                               dateEnd=element.Element("DateEnd").Value,
                               title=element.Element("Title").Value,
                               idExcutive = element.Element("IdExecutive").Value ,
                           };

foreach (var item in elements)//send this error

            {}
System.FormatException: The string was not recognized as a valid DateTime. There is a 
unknown word starting at index 0.

为什么会出错?

XML

<CalendarFairs> 
    <CalendarFair>
        <DateStart>2011-04-05T00:00:00</DateStart>
       <DateEnd>2011-04-09T00:00:00</DateEnd>
        <Title>aaaa</Title>   
       <IdExecutive>1</IdExecutive>
   </CalendarFair>
  <CalendarFair>
      <DateStart>2011-04-16T00:00:00</DateStart>
      <DateEnd>2011-04-19T00:00:00</DateEnd>
      <Title>bbb</Title>
      <IdExecutive>2</IdExecutive>
  </CalendarFair>
<CalendarFairs>

Code

  var elements = from element in doc.Descendants("CalendarFair")
                           where DateTime.Parse (element.Elements ("DateStart").ToString())==DateTime.Now 
                           select new
                           {
                               dateStart = element.Element("DateStart").Value,
                               dateEnd=element.Element("DateEnd").Value,
                               title=element.Element("Title").Value,
                               idExcutive = element.Element("IdExecutive").Value ,
                           };

foreach (var item in elements)//send this error

            {}
System.FormatException: The string was not recognized as a valid DateTime. There is a 
unknown word starting at index 0.

why error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ繥欢鉨o。 2024-10-30 22:44:21

尝试按如下方式更改它:

var elements = from element in doc.Descendants("CalendarFair")
               let start = element.Element("DateStart").Value
               where DateTime.Parse (start)==DateTime.Now.Date
               select new
               {
                   dateStart = start,
                   dateEnd=element.Element("DateEnd").Value,
                   title=element.Element("Title").Value,
                   idExcutive = element.Element("IdExecutive").Value ,
               };

编辑:根据您发布的 XML,上面的查询效果很好。尝试使用以下输入进行测试:

<CalendarFairs> 
    <CalendarFair>
        <DateStart>2011-04-05T00:00:00</DateStart>
       <DateEnd>2011-04-09T00:00:00</DateEnd>
        <Title>aaaa</Title>   
       <IdExecutive>1</IdExecutive>
   </CalendarFair>
  <CalendarFair>
      <DateStart>2011-03-20T00:00:00</DateStart>
      <DateEnd>2011-04-19T00:00:00</DateEnd>
      <Title>bbb</Title>
      <IdExecutive>2</IdExecutive>
  </CalendarFair>
</CalendarFairs>

请注意,我已插入今天的开始日期。实际上我认为结果是空的只是因为没有带有实际日期的条目。

Try to change it as follows:

var elements = from element in doc.Descendants("CalendarFair")
               let start = element.Element("DateStart").Value
               where DateTime.Parse (start)==DateTime.Now.Date
               select new
               {
                   dateStart = start,
                   dateEnd=element.Element("DateEnd").Value,
                   title=element.Element("Title").Value,
                   idExcutive = element.Element("IdExecutive").Value ,
               };

EDIT: based on the XML you have posted the query above works pretty well. Try to test it with this input:

<CalendarFairs> 
    <CalendarFair>
        <DateStart>2011-04-05T00:00:00</DateStart>
       <DateEnd>2011-04-09T00:00:00</DateEnd>
        <Title>aaaa</Title>   
       <IdExecutive>1</IdExecutive>
   </CalendarFair>
  <CalendarFair>
      <DateStart>2011-03-20T00:00:00</DateStart>
      <DateEnd>2011-04-19T00:00:00</DateEnd>
      <Title>bbb</Title>
      <IdExecutive>2</IdExecutive>
  </CalendarFair>
</CalendarFairs>

Note that I have inserted today's start date. Actually I think the result was empty just because there weren't entries with actual date.

对风讲故事 2024-10-30 22:44:21

听起来您输入的一个或多个 字符串不是有效的 DateTime 格式。

您可以发布一些示例输入 XML 吗?

您可能需要使用 ParseExact 提供日期格式 - http://msdn .microsoft.com/en-us/library/w2sa9yss.aspx

It sounds like one or more of your input <DateStart> strings is not in a valid DateTime format.

Can you post some sample input XML?

It may be that you need to provide the date format using ParseExact - http://msdn.microsoft.com/en-us/library/w2sa9yss.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文