这是输入xml:
<?xml version="1.0"?>
<StateSeparationRequestCollection xsi:schemaLocation="https://uidataexchange.org/schemas SeparationRequest.xsd" xmlns="https://uidataexchange.org/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<StateSeparationRequest>
<StateRequestRecordGUID>30000000000000000000000000004000</StateRequestRecordGUID>
<SSN>999999999</SSN>
</StateSeparationRequest>
<StateSeparationRequest>
<StateRequestRecordGUID>30000000000000000000000000004000</StateRequestRecordGUID>
<SSN>999999999</SSN>
</StateSeparationRequest>
</StateSeparationRequestCollection>
我正在尝试在StateRequestRecordGUID中找到重复的元素值,如果找到重复项,请从文档中删除它们。
这是我的代码:
XDocument doc = XDocument.Load(xmlreader);
XNamespace ns = "https://uidataexchange.org/schemas";
var duplicates = (from req in doc.Descendants(ns + "StateSeparationRequest")
group req by req.Descendants(ns + "StateRequestRecordGUID").First().Value
into g
where g.Count() > 1
select g.Skip(1)).SelectMany(elements => elements);
foreach (var duplicate in duplicates)
{
duplicate.Remove();
}
var node = doc.Descendants(ns + "EmployerTPASeparationRequest");
var node2 = node.ElementAt(i);
string _StateRequestRecordGUID = "";
foreach (var element in node2.Elements())
{ ...
这是输出应该是:
<?xml version="1.0"?>
<StateSeparationRequestCollection xsi:schemaLocation="https://uidataexchange.org/schemas SeparationRequest.xsd" xmlns="https://uidataexchange.org/schemas" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<StateSeparationRequest>
<StateRequestRecordGUID>30000000000000000000000000004000</StateRequestRecordGUID>
<SSN>999999999</SSN>
</StateSeparationRequest>
</StateSeparationRequestCollection>
答案 0 :(得分:0)
你可以试试这个:
var dups = doc.Descendants(ns + "StateSeparationRequest").GroupBy(e => e.Descendants(ns +"StateRequestRecordGUID").First().ToString());
//remove the duplicates
foreach (XElement ele in dups.SelectMany(g => g.Skip(1)))
ele.Remove();
答案 1 :(得分:0)
我在父节点上错过了我没有在xml文件中显示的内容。我需要添加一个名为EmployerTPASeparationRequest的父级,然后在from语句中使用它。