I have some XML
我有一些XML
<Users>
<User Name="Z"/>
<User Name="D"/>
<User Name="A"/>
</User>
I want to sort that by Name. I load that xml using XDocument
. How can I view that xml sorted by Name?
我想按名字排序。我使用XDocument加载该xml。如何查看按名称排序的xml?
2 个解决方案
#1
13
You can sort using LINQ to Xml, if XmlDocument is not the case
如果不是XmlDocument,您可以使用LINQ to Xml进行排序
XDocument input = XDocument.Load(@"input.xml");
XDocument output = new XDocument(
new XElement("Users",
from node in input.Root.Elements()
orderby node.Attribute("Name").Value descending
select node));
#2
0
XDocument xdoc = new XDocument(
new XElement("Users",
new XElement("Name", "Z"),
new XElement("Name", "D"),
new XElement("Name", "A")));
var doc = xdoc.Element("Users").Elements("Name").OrderBy(n => n.Value);
XDocument doc2 = new XDocument(new XElement("Users", doc));
#1
13
You can sort using LINQ to Xml, if XmlDocument is not the case
如果不是XmlDocument,您可以使用LINQ to Xml进行排序
XDocument input = XDocument.Load(@"input.xml");
XDocument output = new XDocument(
new XElement("Users",
from node in input.Root.Elements()
orderby node.Attribute("Name").Value descending
select node));
#2
0
XDocument xdoc = new XDocument(
new XElement("Users",
new XElement("Name", "Z"),
new XElement("Name", "D"),
new XElement("Name", "A")));
var doc = xdoc.Element("Users").Elements("Name").OrderBy(n => n.Value);
XDocument doc2 = new XDocument(new XElement("Users", doc));