我已經寫了一個Linq到Xml查詢,從xml創建實體,我在xml中有一個DateTime字段,它可能是空白的,我需要將相同的空白分配給字段在實體中,我收到錯誤「String not recognized as DateTime」將空值賦給DateTime字段
XDocument xDocument = XDocument.Load(@「c:\ Sample.xml」);
var _pndList =
from plist in
xDocument.Descendants("HEADER")
select new PND()
{
DeliveryDate =
DateTime.ParseExact(Convert(plist, "DELIVERYDATE"), "yyyyMMdd",
CultureInfo.InvariantCulture, DateTimeStyles.None),
LoadClosed =
DateTime.ParseExact(Convert(plist, "LOADCLOSEDDATETIME"), "yyyyMMddhhmmss",
CultureInfo.InvariantCulture, DateTimeStyles.None),
TrailerId = Convert(plist, "TRAILERID"),
TripAndRouteId = Convert(plist, "TRIPROUTEID"),
StoreCode = plist.Ancestors("STORE").Attributes().First().Value,
Product =
(from mlist in
plist.Ancestors("STORE").Descendants("RECORD")
select new PreNotifiedProduct()
{
DepotCode = Convert(plist, "DEPOTCODE"),
MU = Convert(mlist, "MU"),
CaseOrUnitQuantity =
System.Convert.ToInt32("0" + Convert(mlist, "NOOFCASES")),
OuterCaseHeight =
System.Convert.ToInt32("0" +
Convert(mlist, "OUTERCASEHEIGHT")),
OuterCaseLength =
System.Convert.ToInt32("0" +
Convert(mlist, "OUTERCASELENGTH")),
OuterCaseWidth =
System.Convert.ToInt32("0" + Convert(mlist, "OUTERCASEWIDTH")),
ProductCode = Convert(mlist, "TPNB"),
UnitsPerCase =
System.Convert.ToInt32("0" + Convert(mlist, "UNITSPERCASE")),
UseByDate =
DateTime.ParseExact(Convert(plist, "LOADCLOSEDDATETIME"),
"yyyyMMddhhmmss",
CultureInfo.InvariantCulture,
DateTimeStyles.None)
}).ToList()
};
}
在實體PreNotifiedProduct()UseByDate是日期時間
的
我想你將不得不檢查元素是否爲空,如果爲空,則可能將其設置爲'DateTime.MinValue'(表兄弟姐妹'DateTime'沒有什麼空白,最接近的是使其成爲可空類型並依賴null作爲空值) – V4Vendetta 2013-05-02 12:18:24