为什么这段代码不能在一个xml元素中找到任何重复的内容?[英] Why doesn't this code find any duplicates within an xml element?

本文是小编为大家收集整理的关于为什么这段代码不能在一个xml元素中找到任何重复的内容?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

这里是输入的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>

推荐答案

我错过了未在 xml 文件中显示的父节点.我需要添加一个名为 EmployerTPASeparationRequest 的父级,然后在 from 语句中使用它.

本文地址:https://www.itbaoku.cn/post/1557145.html

问题描述

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>

推荐答案

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.