1
我想加載一個xml文件。 我這樣做:如何爲XDocument對象中不存在的屬性設置默認值
from e in XDocument.Load(stream).Root.Elements("cust")
select new Customer
{
MemeberID = (int)e.Attribute("custid"),
CustomerID = (int)e.Attribute("custid"),
FirstName = (string)e.Attribute("fname"),
LastName = (string)e.Attribute("lname"),
ShowsNumber = (int)e.Attribute("count_noshow"),
VisitNumber = (int)e.Attribute("count_resos"),
Cancellation = (int)e.Attribute("count_cancel"),
MobileNumber = (string)e.Element("phone").Attribute("phonenumber")
})
即使事情是工作不錯,但現在我有一個情況,即XML文檔時並不需要有mobilenuber
屬性。 所以我可以爲這個手機號碼設置一個默認值,如果它不在xml節點中?
千恩萬謝
不是neceessary作品,becaues的 「phone」 元素,也許不會出現,所以我有例外:'對象引用未設置爲對象的實例。' – 2014-12-27 15:18:39
你說沒有必要擁有該屬性。現在沒有必要擁有這個元素了?編輯:修復了可選的XML元素 – supertopi 2014-12-27 15:20:47
謝謝.......................... – 2014-12-27 15:26:59