为什么此代码在 xml 元素中找不到任何重复项?

发布于 2024-11-08 20:40:42 字数 2050 浏览 0 评论 0原文

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从此见与不见 2024-11-15 20:40:42

你可以试试这个:

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

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();
大海や 2024-11-15 20:40:42

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文