Вот входные данные 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>