使用 linq 处理 xml 中的空日期时间元素

发布于 2024-08-25 08:03:08 字数 1586 浏览 4 评论 0原文

您好,

我有一个示例文档,如下所示,

<ItemEntry>
<PurchaseDate>2010-03-18T20:36:32.81108+13:00</PurchaseDate>
<StoreGUID>0a0324ad-5f99-486a-a2d0-870bc6991e9f</StoreGUID>
<ExpiryDate />
<CardID>111111</CardID>
<PurchaseAmount>0</PurchaseAmount>
<RedeemedAmount />
<EntryType>1</EntryType>
<RedeemedDate />
<SalesAssistantID>0</SalesAssistantID>
</ItemEntry>

如您所见,有几个元素 ExpiryDate 和 RedeemedDate 为空。

  var q = from c in xml.Elements("ItemEntry")
                    select new mdDetail {
                        PurchaseDate = (DateTime)c.Element("PurchaseDate"),
                        StoreGUID = (Guid)c.Element("StoreGUID"),
                        ExpiryDate = (DateTime?)c.Element("ExpiryDate")??DateTime.MinValue,
                        CardID = (int)c.Element("CardID"),
                        PurchaseAmount = (double)c.Element("PurchaseAmount"),
                         RedeemedAmount = (double?)c.Element("RedeemedAmount"),
                        EntryType = (int)c.Element("EntryType"),
                        RedeemedDate = (DateTime?)c.Element("RedeemedDate") ??DateTime.MinValue,
                        SalesAssistantID = (int)c.Element("SalesAssistantID"),



                    }                      
                    ;
            foreach (var item in q)
            {

            }

我不知道如何处理空元素值, 我已经尝试过 ??DateTime.MinValue 和 ??null 但都给了我一个“ 字符串未被识别为有效的日期时间。”错误。

有什么建议吗?

谢谢

HI

I have an example document that looks like

<ItemEntry>
<PurchaseDate>2010-03-18T20:36:32.81108+13:00</PurchaseDate>
<StoreGUID>0a0324ad-5f99-486a-a2d0-870bc6991e9f</StoreGUID>
<ExpiryDate />
<CardID>111111</CardID>
<PurchaseAmount>0</PurchaseAmount>
<RedeemedAmount />
<EntryType>1</EntryType>
<RedeemedDate />
<SalesAssistantID>0</SalesAssistantID>
</ItemEntry>

As you can see there are couple of elements ExpiryDate and RedeemedDate are are empty.

  var q = from c in xml.Elements("ItemEntry")
                    select new mdDetail {
                        PurchaseDate = (DateTime)c.Element("PurchaseDate"),
                        StoreGUID = (Guid)c.Element("StoreGUID"),
                        ExpiryDate = (DateTime?)c.Element("ExpiryDate")??DateTime.MinValue,
                        CardID = (int)c.Element("CardID"),
                        PurchaseAmount = (double)c.Element("PurchaseAmount"),
                         RedeemedAmount = (double?)c.Element("RedeemedAmount"),
                        EntryType = (int)c.Element("EntryType"),
                        RedeemedDate = (DateTime?)c.Element("RedeemedDate") ??DateTime.MinValue,
                        SalesAssistantID = (int)c.Element("SalesAssistantID"),



                    }                      
                    ;
            foreach (var item in q)
            {

            }

I am not sure how to deal with the null element value,
I have tried ??DateTime.MinValue and ??null however both give me a "
String was not recognized as a valid DateTime." error.

Any suggestions?

Thank you

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

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

发布评论

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

评论(2

人间不值得 2024-09-01 08:03:08
ExpiryDate = String.IsNullOrEmpty((string)c.Element("ExpiryDate"))? 
    DateTime.MinValue : DateTime.Parse((string)c.Element("ExpiryDate"))
ExpiryDate = String.IsNullOrEmpty((string)c.Element("ExpiryDate"))? 
    DateTime.MinValue : DateTime.Parse((string)c.Element("ExpiryDate"))
万人眼中万个我 2024-09-01 08:03:08

如果 ExpireyDate 是,您也可以使用 null 代替 DateTime.MinValue
声明为可为空

@Gabe,你不能只使用 null - 你需要使用 (DateTime?)null 因为编译器不会知道如何将 null 转换为 DateTime 对象

因此,如果您希望该值只是空白(null),这将是最终代码:

ExpiryDate = String.IsNullOrEmpty(c.Element("ExpiryDate").Value)? 
    (DateTime?)null : DateTime.Parse(c.Element("ExpiryDate").Value)

假设 DateTime 被声明为可为 null (DateTime?)

"You could also use null instead of DateTime.MinValue if ExpireyDate is
declared to be nullable
"

@Gabe, you can't just use null - you need to use (DateTime?)null because the compiler won't know how to convert null into a DateTime object

So if you want the value to just be a blank (null) this would be the final code:

ExpiryDate = String.IsNullOrEmpty(c.Element("ExpiryDate").Value)? 
    (DateTime?)null : DateTime.Parse(c.Element("ExpiryDate").Value)

Assuming DateTime is a declared a nullable (DateTime?)

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