c# - 按根名称对 XML 内容进行分组
问题描述
由于我的 XML 创建步骤(Service WSDL File Step by Step Processing),我得到了这种 XML 语法:
<auth>
<appKey>ABCD567</appKey>
</auth>
<auth>
<appSecret>456TYUU</appSecret>
</auth>
主机服务不接受它,响应 <一个'auth' 元素丢失>,它接受这个:
<auth>
<appKey>ABCD567</appKey>
<appSecret>456TYUU</appSecret>
</auth>
我如何通过 XML 处理代码实现(在创建 XML 后将相同的根与不同的元素分组 - 无法更改创建过程)这种语法。
实际代码的某些部分如下所示:
<env:Envelope
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header/>
<soapenv:Body>
<sch:GetCategoryAttributesRequest>
<auth>
<appKey>***</appKey>
<appSecret>***</appSecret>
</auth>
<categoryId>1002306</categoryId>
<pagingData>
<currentPage>0</currentPage>
<pageSize>100</pageSize>
</pagingData>
<attributeList>
<attribute>
<id>354189900</id>
</attribute>
<attribute>
<mandatory>true</mandatory>
</attribute>
</attributeList>
</sch:GetCategoryAttributesRequest>
</soapenv:Body>
</soapenv:Envelope>
解决方案
基于这个问题和最后一个 Xml 添加,我假设 xml 就像:
注意:sch
不在所提出的 XML 中退出,所以我创建了它xmlns:sch=""http://www.w3.org/2001/XMLSchema-instance""
<env:Envelope
xmlns:env=""http://schemas.xmlsoap.org/soap/envelope/"">
<env:Header/>
<env:Body>
<sch:GetCategoryAttributesRequest xmlns:sch=""http://www.w3.org/2001/XMLSchema-instance"">
<auth>
<appKey>ABCD567</appKey>
</auth>
<auth>
<appSecret>456TYUU</appSecret>
</auth>
<categoryId>1002306</categoryId>
<pagingData>
<currentPage>0</currentPage>
</pagingData>
<pagingData>
<pageSize>100</pageSize>
</pagingData>
</sch:GetCategoryAttributesRequest>
</env:Body>
</env:Envelope>;
要使用Linq to XML特别是XDocument获得所需的输出:
1 - 如果 xml 有多个节点,则构建分组节点:
var groupedItems = xDocument
.Descendants(xn + "GetCategoryAttributesRequest")
.Elements()
.GroupBy(x => x.Name)
.Select(x =>
{
return x.Skip(1).Any() ? new XElement(x.Key, x.Descendants()) : x.First();
}).ToList()
2 - 替换旧的 xml:
xDocument.Descendants(xn + "GetCategoryAttributesRequest").First().ReplaceNodes(groupedItems);
整个代码:
string xml = @"
<env:Envelope
xmlns:env=""http://schemas.xmlsoap.org/soap/envelope/"">
<env:Header/>
<env:Body>
<sch:GetCategoryAttributesRequest xmlns:sch=""http://www.w3.org/2001/XMLSchema-instance"">
<auth>
<appKey>ABCD567</appKey>
</auth>
<auth>
<appSecret>456TYUU</appSecret>
</auth>
<categoryId>1002306</categoryId>
<pagingData>
<currentPage>0</currentPage>
</pagingData>
<pagingData>
<pageSize>100</pageSize>
</pagingData>
</sch:GetCategoryAttributesRequest>
</env:Body>
</env:Envelope>";
XDocument xDocument = XDocument.Parse(xml);
XNamespace xn = "http://www.w3.org/2001/XMLSchema-instance";
var groupedItems = xDocument
.Descendants(xn + "GetCategoryAttributesRequest")
.Elements()
.GroupBy(x => x.Name)
.Select(x =>
{
return x.Skip(1).Any() ? new XElement(x.Key, x.Descendants()) : x.First();
}).ToList();
xDocument.Descendants(xn + "GetCategoryAttributesRequest").First().ReplaceNodes(groupedItems);
Console.WriteLine(xDocument);
结果
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header />
<env:Body>
<sch:GetCategoryAttributesRequest xmlns:sch="http://www.w3.org/2001/XMLSchem
a-instance">
<auth>
<appKey>ABCD567</appKey>
<appSecret>456TYUU</appSecret>
</auth>
<categoryId>1002306</categoryId>
<pagingData>
<currentPage>0</currentPage>
<pageSize>100</pageSize>
</pagingData>
</sch:GetCategoryAttributesRequest>
</env:Body>
</env:Envelope>
我希望您发现此更新对您有所帮助。
推荐阅读
- ios - 如何从 App Delegate 为 GoogleSignIn 框架切换 @State 变量?
- python - 有没有办法对 Pandas DataFrame 或 Numpy ndarray 中的多列进行并行前向填充?
- angular - 使用 JavaScript Angular 7 创建 HTML 时无法识别 CSS 类
- wso2 - 使用 WSO2 API 管理控制台访问和管理 Mule API?
- python - 如何创建一个返回军事时间的类
- ios - iOS 13 导航栏提示始终为黑色
- google-apps-script - 当源是函数时(例如 NOW()),GOOGLE 脚本“仅复制到值”不起作用
- elasticsearch - Kibana 图表中的“分组依据”
- android - 如何减少滚动视图中图像之间的空间?
- python - 尝试 Pip 安装面部识别库的问题