2013-02-22 133 views
1

我想過濾使用Linq到xml的xmldata,問題是我無法獲取使用XElement.Elements(Xname)方法的元素,但是當使用XElement.Desendents(Xname)方法工作正常,但顯示我不想要的所有元素。我想要什麼它應該顯示所有元素和屬性,其元素和屬性名稱在兩個文本框中傳遞。XElement.Elements(XName)方法無法讀取具有屬性的xml節點

XML:

<?xml version="1.0" ?> 
<Summary AvailableRules="71000" SelectedRules="445" OmittedRules="6887"> 
    <BBCE AvailableRules="69" SelectedRules="4" OmittedRules="65"> 
    <SelectedRules> 
     <Rule RuleID="jc_0201" Priority="Strongly Recommended" /> 
     <Rule RuleID="jc_0211" Priority="Strongly Recommended" /> 
     <Rule RuleID="jc_0221" Priority="Strongly Recommended" /> 
     <Rule RuleID="jc_0231" Priority="Strongly Recommended" /> 
    </SelectedRules> 
    <OmittedRules> 
     <Rule RuleID="ar_0001" Priority="Mandatory" /> 
     <Rule RuleID="ar_0002" Priority="Mandatory" /> 
     <Rule RuleID="db_0143_a" Priority="Strongly Recommended" /> 
     <Rule RuleID="db_0143_b" Priority="Strongly Recommended" /> 
     <Rule RuleID="jc_0311" Priority="Mandatory" /> 
     <Rule RuleID="jc_0321" Priority="Mandatory" /> 
     <Rule RuleID="jc_0331" Priority="Mandatory" /> 
     <Rule RuleID="jc_0341" Priority="Mandatory" /> 
     <Rule RuleID="jc_0011" Priority="Strongly Recommended" /> 
     <Rule RuleID="jc_0021" Priority="Strongly Recommended" /> 
     <Rule RuleID="na_0004_a" Priority="Recommended" /> 
     <Rule RuleID="na_0004_b" Priority="Recommended" /> 
     <Rule RuleID="db_0043" Priority="Strongly Recommended" /> 
     <Rule RuleID="db_0042" Priority="Strongly Recommended" /> 
     <Rule RuleID="na_0005" Priority="Strongly Recommended" /> 
     <Rule RuleID="jc_0081" Priority="Recommended" /> 
     <Rule RuleID="jm_0002" Priority="Mandatory" /> 
     <Rule RuleID="db_0142" Priority="Strongly Recommended" /> 
     <Rule RuleID="jc_0061" Priority="Recommended" /> 
     <Rule RuleID="db_0146" Priority="Strongly Recommended" /> 
     <Rule RuleID="db_0140" Priority="Recommended" /> 
     <Rule RuleID="jm_0013" Priority="Strongly Recommended" /> 
     <Rule RuleID="db_0032" Priority="Strongly Recommended" /> 
     <Rule RuleID="db_0141" Priority="Strongly Recommended" /> 
     <Rule RuleID="jc_0171" Priority="Strongly Recommended" /> 
     <Rule RuleID="jm_0010" Priority="Strongly Recommended" /> 
     <Rule RuleID="jc_0281" Priority="Strongly Recommended" /> 
     <Rule RuleID="na_0008" Priority="Recommended" /> 
     <Rule RuleID="na_0009" Priority="Strongly Recommended" /> 
    </OmittedRules> 
    </BBCE> 
</Summary> 

C#代碼:

var button = sender as Button; 
var parent = button.Parent as FrameworkElement; 

//(Textbox to take element`s name) 
var textBox = parent.FindName("textbox1") as TextBox; 

var textbl = parent.FindName("abc") as TextBlock; 
var com = parent.FindName("cbox1") as ComboBox; 

//(Textbox to take ATTRIBUTE`s name) 
var textBox1 = parent.FindName("textbox2") as TextBox; 

XElement ele = XElement.Load(txtFileName.Text); 

//working with Xelement.desendents it works fine 
var fil = from item in ele.Elements(textbl.Text) 
      select item.Element(textBox.Text).Attribute(textBox1.Text); 

foreach (var f in fil) 
{ 
    Label lb = new Label(); 
    lb.Content = f; 
    canvas1.Children.Add(lb); 
} 

我觀察到它工作正常只有BBCE元工作進行時,但在添加彙總元素與屬性(元法)won`工作。

我錯過了什麼嗎?

回答

0

Elements(name)方法返回節點的子元素。 Descendants(name)方法返回節點的後代元素。那是預期的行爲。

所以,當你打電話給ele.Elements(textbl.Text),那麼搜索將只發生在ele元素的直接子女之間。當您通過XElement ele = XElement.Load加載文件時,則ele元素將是xml的根元素。即<Summary>元素在你的情況。它有單人小孩<BBCE>。這就是爲什麼當您使用Elements()方法進行搜索時只能找到<BBCE>

Descendants()方法在您的情況下將搜索文檔中的任何元素,但節點本身除<Summary>之外。

+2

哦......知道了......再次感謝。 – Abhinav 2013-02-22 07:11:53