2011-11-22 192 views
0

考慮下面的XML:遞歸地使用XPath遞歸地選擇子節點?

<Checks> 
    <IsSomethingCheck Param="1"/> 
    <IsSomeoneCheck Param2="2"/> 
    <Checks Operator="Or"> 
     <IsAnythingCheck Param3="3"/> 
    </Checks> 
</Checks> 

我怎麼會遍歷遞歸Checks元素的孩子嗎?

下面的代碼適用於最外面的孩子,但停止處理內大多數兒童(內Checks元件的部分)

protected override void InitializePlugin(XmlElement xmlElement) 
{ 
    Enum.TryParse(xmlElement.GetAttribute("Operator"), out _operator); 

    XPathNodeIterator nav = xmlElement.CreateNavigator().Select("Checks/*"); 

    foreach (XPathNavigator checkNode in nav) 
    { 
     if (_checksLookup.ContainsKey(checkNode.Name)) 
     { 
      Type checkType = _checksLookup[checkNode.Name]; 
      Check check = Activator.CreateInstance(checkType) as Check; 
      check.Initialize(checkNode.OuterXml); 
      Checks.Add(check); 
     } 
    }  
} 

PS:此方法的的XmlElement期望它將接收整個Checks部的XML。

回答

2

找到了! XPath表達式應該是/CheckManager/*而不是CheckManager/*