Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于子节点对整个xdocument进行排序_C#_Xml_Xslt_Xpath_Linq To Xml - Fatal编程技术网

C# 基于子节点对整个xdocument进行排序

C# 基于子节点对整个xdocument进行排序,c#,xml,xslt,xpath,linq-to-xml,C#,Xml,Xslt,Xpath,Linq To Xml,我有以下格式的xml: <?xml version="1.0" encoding="utf-8"?> <contactGrp name="People"> <contactGrp name="Developers"> <customer name="Mike" ></customer> <customer name="Brad" ></customer> <customer na

我有以下格式的xml:

<?xml version="1.0" encoding="utf-8"?>
<contactGrp name="People">
  <contactGrp name="Developers">
    <customer name="Mike" ></customer>
    <customer name="Brad" ></customer>
    <customer name="Smith" ></customer>
  </contactGrp>
  <contactGrp name="QA">
    <customer name="John" ></customer>
    <customer name="abi" ></customer>
  </contactGrp>
</contactGrp>
<?xml version="1.0" encoding="utf-8"?>
<contactGrp name="People">
  <contactGrp name="Developers">
    <customer name="Brad" ></customer>
    <customer name="Mike" ></customer>
    <customer name="Smith" ></customer>
  </contactGrp>
  <contactGrp name="QA">
    <customer name="abi" ></customer>
    <customer name="John" ></customer>
  </contactGrp>
</contactGrp>

我想根据客户的姓名对客户列表进行排序,并以以下格式返回文档:

<?xml version="1.0" encoding="utf-8"?>
<contactGrp name="People">
  <contactGrp name="Developers">
    <customer name="Mike" ></customer>
    <customer name="Brad" ></customer>
    <customer name="Smith" ></customer>
  </contactGrp>
  <contactGrp name="QA">
    <customer name="John" ></customer>
    <customer name="abi" ></customer>
  </contactGrp>
</contactGrp>
<?xml version="1.0" encoding="utf-8"?>
<contactGrp name="People">
  <contactGrp name="Developers">
    <customer name="Brad" ></customer>
    <customer name="Mike" ></customer>
    <customer name="Smith" ></customer>
  </contactGrp>
  <contactGrp name="QA">
    <customer name="abi" ></customer>
    <customer name="John" ></customer>
  </contactGrp>
</contactGrp>

我正在使用c#,目前使用xmldocument


谢谢你

你可以这样做

var doc = XDocument.Load(/* ... */);

foreach (var g in doc.Descendants("contactGrp"))
{
    var customers = g.Elements("customer").ToList();
    customers.Remove();
    g.Add(customers.OrderBy(c => c.Attribute("name").Value));
}

如果希望拥有样式表并使用它转换文档,则:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
    <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/contactGrp">
    <contactGrp name="Developers">
      <xsl:apply-templates select="contactGrp"/>
    </contactGrp>
  </xsl:template>

  <xsl:template match="contactGrp/contactGrp">
    <contactGrp>
      <xsl:attribute name="name">
        <xsl:value-of select="@name"/>
      </xsl:attribute>

      <xsl:for-each select="customer">
        <xsl:sort select="@name"/>
        <xsl:copy-of select="."/>
      </xsl:for-each>

    </contactGrp>
  </xsl:template>

</xsl:stylesheet>

此转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="contactGrp">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*">
     <xsl:sort select="@name"/>
   </xsl:apply-templates>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

应用于提供的XML文档时:

<contactGrp name="People">
    <contactGrp name="Developers">
        <customer name="Mike" ></customer>
        <customer name="Brad" ></customer>
        <customer name="Smith" ></customer>
    </contactGrp>
    <contactGrp name="QA">
        <customer name="John" ></customer>
        <customer name="abi" ></customer>
    </contactGrp>
</contactGrp>
<contactGrp name="People">
  <contactGrp name="Developers">
    <customer name="Brad" />
    <customer name="Mike" />
    <customer name="Smith" />
  </contactGrp>
  <contactGrp name="QA">
    <customer name="abi" />
    <customer name="John" />
  </contactGrp>
</contactGrp>

生成所需的正确结果:

<contactGrp name="People">
    <contactGrp name="Developers">
        <customer name="Mike" ></customer>
        <customer name="Brad" ></customer>
        <customer name="Smith" ></customer>
    </contactGrp>
    <contactGrp name="QA">
        <customer name="John" ></customer>
        <customer name="abi" ></customer>
    </contactGrp>
</contactGrp>
<contactGrp name="People">
  <contactGrp name="Developers">
    <customer name="Brad" />
    <customer name="Mike" />
    <customer name="Smith" />
  </contactGrp>
  <contactGrp name="QA">
    <customer name="abi" />
    <customer name="John" />
  </contactGrp>
</contactGrp>


注意事项:无论
contactGrp
元素的嵌套级别如何,始终会生成正确的结果

我感到困惑,因为
xpath
标记。。。如果您想选择一个节点集,那么XPath可以选择正确的技术。如果要对节点集进行正确排序,则需要XPath引擎主机语言。但是如果你想转换一个XML树,标准资源是XSLT。我知道,我似乎无法让它解决我的问题。好问题,+1。请参阅我的答案,以获得一个完整、简短且简单的XSLT解决方案,该解决方案适用于
contactGrp
元素的任何嵌套级别非常感谢。我知道xslt是正确的方法,但无法让xml排序到正确的级别。非常感谢。