按根名称对 XML 内容进行分组

Group XML Content By Root Name

由于我的 XML 创建步骤(服务 WSDL 文件逐步处理),我得到了 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>
    

希望此更新对您有所帮助。