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