我有這樣的XML:LINQ查詢來獲取XML元素時的屬性不存在
<Config>
<EmpFieldsMap>
<Employee>
<Field>
<Name update = "false">EmpNumber</Name>
</Field>
<Field>
<Name insert = "true">EmpName</Name>
</Field>
<Field>
<Name insert = "true">EmpDesignation</Name>
</Field>
</Employee>
</EmpFieldsMap>
</Config>
我的應用程序會做一個INSERT或更新其字段將來自這個XML。 每個標籤將具有插入或更新屬性,如上面的代碼片段所示。
對於插入所有具有屬性
insert = "true"
標籤和不具有這個屬性的標籤,在這種情況下,「EmpNumber」,必須加以考慮。
同樣適用於更新。
此代碼給了我所有插件屬性設置爲true的標籤:
insertTags = from p in xml.Element("Config").Element("EmpFieldsMap").Elements("Field")
where p.Element("Name").Attribute("insert") != null
&& p.Element("Name").Attribute("insert").Value == "true"
select p.Element("Name").Value;
拆卸檢查空
insertTags = from p in xml.Element("Config").Element("EmpFieldsMap").Elements("Field")
where p.Element("Name").Attribute("insert").Value == "true"
select p.Element("Name").Value;
給未設置
對象參考實例
錯誤。
我在編寫查詢時遇到了問題,該查詢還將包含不存在該屬性的標籤。
有人可以幫助我嗎?
問候。
什麼是 - ?? - 在做什麼? – Gero 2013-02-20 12:19:06
@Gero - 這是[null coalescing operator](http://msdn.microsoft.com/zh-cn/library/ms173224.aspx)。如果'insert'屬性返回null,則將其替換爲'true'。 – 2013-02-21 00:47:36