2013-04-12 107 views
0

在,我解析XML,我有以下几点:解析字符串到C#DateTime对象

<event> 
    <book>Felicity Fly</book> 
    <author>Christina Gabbitas</author> 
    <bookImgUrl>http://www.whsmith.co.uk/Images/Products\957\255\9780957255203_t_f.jpg</bookImgUrl> 
    <info>Christina Gabbitas will be signing copies of her new book, Felicity Fly. Books should be bought from WHSmith. Proof of purchase may be necessary</info> 
    <date>20 Apr 2013</date> 
    <startTime>10:30</startTime> 
    <location> 
     <name>WHSmith Chester</name> 
     <address>5-7 Foregate Street</address> 
     <city>Chester</city> 
     <county>Cheshire</county> 
     <postcode>CH1 1HH</postcode> 
     <tel>01244 321106</tel> 
    </location> 
</event> 

我想创建一个从两个节点< date><startTime> DateTime对象。所以我这样做:

var EventEntity = new Event() 
{ 
    Book = e.Book, 
    BookImgUrl = e.BookImgUrl, 
    Info = e.Info, 
    Start = new DateTime().**?** 
}; 

但是当我按点DateTime对象后,我没有从智能感知得到的解析方法,这是为什么[]?我究竟做错了什么?

我打算使用this post中列出的解决方案。

回答

1

解析是一种静态方法,但您将其称为实例方法。 你应该这样称呼它:

var EventEntity = new Event() 
{ 
    Book = e.Book, 
    BookImgUrl = e.BookImgUrl, 
    Info = e.Info, 
    Start = DateTime.ParseExact(...) // or DateTime.TryParseExact(...) 
}; 
+0

谢谢你Daniele。如果我将日期和开始时间加上它们之间的空格,这种格式是否正确?“dd MMM YYYY HH:mm” – Ciwan

+0

我的意思是如果我连接我的'日期'和'startTime'之间的空白空间。 – Ciwan

+0

我还没有尝试格式,但对我来说听起来不错。你必须注意文化,否则你的月份将不会被正确解析(InvariantCulture并不意味着为一切工作;有一些关于它的文章......) –