我使用Linq to Xml來解析來自遺留系統的一些xml消息。其中一條消息以名稱/值對的形式出現。因此,我正在按名稱進行查找,然後試圖獲得相同的值。但是,當值爲空(<Value/>
)我的代碼是拋出錯誤Input string was not in a correct format.
Linq to Xml - 輸入字符串
我想弄清楚解決這個問題的最佳方法。任何建議將不勝感激(嘗試填充可爲空int類型int的屬性?)。
代碼示例:
myRecord.myField= xdoc.Descendants("Information")
.Where(x => (string)x.Element("Name") == "myField")
.Select(x => (int?)x.Element("Value")).FirstOrDefault();
XML摘錄:
<Information>
<Name>myField</Name>
<Value />
</Information>
始終欣賞反饋/輸入。
謝謝,
小號
會拋出NullReferenceException異常時,有沒有這樣的元素(恐怕是沒有的情況下在這裏,但值得知道) – MarcinJuraszek 2013-03-12 15:06:47
@MarcinJuraszek不,它不會這種情況將處理失蹤元素'(v == null || v.IsEmpty)' – 2013-03-12 15:06:58
嗯,你必須更新你的答案,而我正在打字:) – MarcinJuraszek 2013-03-12 15:08:13