【问题标题】:XElement.Elements(XName) method unable to read xml nodes having attributesXElement.Elements(XName) 方法无法读取具有属性的 xml 节点
【发布时间】:2023-03-08 07:55:01
【问题描述】:

我正在尝试使用 Linq to Xml 过滤 xmldata,问题是我无法使用 XElement.Elements(Xname) 方法获取元素,但是在使用 XElement.Desendents(Xname) 方法时它工作正常但显示我不想要的所有元素。我想要它应该显示所有元素和属性,其元素和属性名称在两个文本框中传递。

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 元素时它工作正常,但添加带有属性的摘要元素(元素方法)将无法正常工作。

我错过了什么吗?

【问题讨论】:

    标签: c# wpf xml linq-to-xml xelement


    【解决方案1】:

    Elements(name) 方法返回节点的子元素。 Descendants(name) 方法返回节点的后代元素。这是预期的行为。

    因此,当您调用 ele.Elements(textbl.Text) 时,搜索将仅发生在 ele 元素的直接子元素之间。当您通过XElement ele = XElement.Load 加载文件时,ele 元素将是 xml 的根元素。 IE。 &lt;Summary&gt; 元素在你的情况下。它有一个孩子&lt;BBCE&gt;。这就是为什么使用Elements()方法搜索时只能找到&lt;BBCE&gt;的原因。

    Descendants() 在您的情况下,方法将搜索文档中的任何元素,&lt;Summary&gt; 节点本身除外。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 2017-09-10
      相关资源
      最近更新 更多