0
我有如下一個XML結構:我如何返回null或匹配的XML節點對象在C#
<Users>
<User Code="1" Roles="1,2,3" />
</Users>
我提供搜索XML文件檢索基於代碼特定用戶像下面
的方法string xpath = "Users/User[@Code="+ Code +"]";
XmlNode user = _xmlDatabase.SelectSingleNode(xpath);
if (user != null)
{
XmlAttributeCollection userMeta = user.Attributes;
if (userMeta != null)
{
int code = int.Parse(Code);
User userInstance = new User(Code, userMeta[1].Value, userMeta[2].Value);
return userInstance;
}
}
我將調用像這樣 User user = GetUserByCode("1");
& _xmlDatabase
該方法是XmlDocument
類的實例。這就有一個問題,
- 我可以返回null沒有任何匹配的用戶發現
- 屬性我尋找不存在
- 這是一個新的文件
因此我修改的方法返回"null"
只能由編譯器投訴"return statement is missing"
我有點想最終用戶做
User user = GetUserByCode("1");
if(user == null)
Display "No User Found"
如果你顯示你的整個方法,而不是隻包含一個不包含方法簽名或'return null'行的代碼片段,那麼可能會有所幫助。 –
@KirkBroadhurst謝謝,但我已經接受了一個答案。在那裏的代碼是我必須爲你提供的。 – Deeptechtons