根据子节点对整个 xdocument 进行排序
我有一个以下格式的 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>
我正在使用 c#,当前使用 xmldocument。
谢谢
I have an xml of the following format:
<?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>
I'd like to sort the list of customers based on their names, and return the document in the following format:
<?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>
I am using c# and currently xmldocument.
thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以做这样的事情
You could do something like this
如果您想要一个样式表并使用它来转换文档,那么:
If you want to have a stylesheet and use it to transform the document then:
此转换:
当应用于提供的 XML 文档时:
产生所需的正确结果:
请注意:无论
contactGrp
元素的嵌套级别如何,始终会生成正确的结果This transformation:
when applied on the provided XML document:
produces the wanted, correct result:
Do note: The correct results will always be produced -- regardless of the level of nesting of the
contactGrp
elements