Why doesn't this code find any duplicates within an xml element?

不问归期 提交于 2020-01-07 05:19:44

问题


Here is the input 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>

I am trying to find duplicate element Values at StateRequestRecordGUID, and if duplicates are found, remove them from the document.

Here is my code:

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())
{ ...

This is what the output should be:

<?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>

回答1:


You can try this:

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();



回答2:


I missed on parent node that I didn't display in the xml files. I needed to add a parent called EmployerTPASeparationRequest, then use that in the from statement.



来源:https://stackoverflow.com/questions/6064330/why-doesnt-this-code-find-any-duplicates-within-an-xml-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!