为什么此代码在 xml 元素中找不到任何重复项?
这是输入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以试试这个:
You can try this:
我错过了未在 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.