【发布时间】:2012-03-13 05:13:44
【问题描述】:
XML 结构:
<Emp>
<Employee username="John"/>
<Employee username="Jason"/>
</Emp>
我不想通过 linq to xml 查询将重复的属性插入到 xml 文件中
var newEmployee= XElement.Parse(defaultEmployee.ToString());
var q = from p in doc.Descendants("Employee")
let attr = p.Attribute("username")
where attr != null && attr.Value != txtusername.Text
select p;
foreach(var dupes in q)
{
newEmployee.Attribute("username").Value = txtusername.Text ;
doc.root.Add(newEmployee);
doc.save(EmployeeFile);
}
我正在尝试只添加一个没有任何重复项的新员工,但我的代码仍然添加了重复项。
有人可以查看我的查询并让我知道我在哪里缺少逻辑吗?
【问题讨论】:
-
显示您实际添加员工的代码 - 我只是看到您覆盖了属性
-
刚刚添加了我实际添加员工的代码
-
真的看不懂你的foreach循环!您将在循环中添加太多具有相同属性的项目!
标签: c# linq-to-xml