我有這樣的XML:C#的LINQ to XML - 返回多個孩子
<?xml version="1.0" encoding="utf-8" ?>
<Interfaces>
<Interface>
<Name>Account Lookup</Name>
<PossibleResponses>
<Response>Account OK to process</Response>
<Response>Overridable restriction</Response>
</PossibleResponses>
</Interface>
<Interface>
<Name>Balance Inquiry</Name>
<PossibleResponses>
<Response>Funds available</Response>
<Response>No funds</Response>
</PossibleResponses>
</Interface>
</Interfaces>
我需要檢索的接口可能響應:
// Object was loaded with XML beforehand
public class Interfaces : XElement {
public List<string> GetActionsForInterface(string interfaceName) {
List<string> actionList = new List<string>();
var actions = from i in this.Elements("Interface")
where i.Element("Name").Value == interfaceName
select i.Element("PossibleResponses").Element("Response").Value;
foreach (var action in actions)
actionList.Add(action);
return actionList;
}
}
結果應該是一個清單,如本(對接口 '賬戶查詢'):
賬戶OK處理
可重寫限制
但它只返回第一個值 - 「帳戶可以處理」。這裏有什麼問題?
編輯:
我改變了我的方法:
public List<string> GetActionsForInterface(string interfaceName) {
List<string> actionList = new List<string>();
var actions = from i in this.Elements("interface")
where i.Element("name").Value == interfaceName
select i.Element("possibleresponses").Elements("response").Select(X => X.Value);
foreach (var action in actions)
actionList.Add(action);
return actionList;
}
但現在我得到第2個錯誤 'actionList.Add(動作);':
The best overloaded method match for System.Collections.Generic.List<string>.Add(string)' has some invalid arguments
Argument 1: cannot convert from 'System.Collections.Generic.IEnumerable<char>' to 'string'
我想選擇多將結果轉換爲其他字符串?
編輯:
要解決的最後一個錯誤:
foreach (var actions in query)
foreach(string action in actions)
actionList.Add(action);
顯然,這裏存在一個數組中的數組。
能否請您對 '選擇多個' 解釋一下嗎? – Yoav