将XML重写为排序日期版本

本文关键字:日期 版本 排序 XML 重写 | 更新日期: 2023-09-27 18:15:27

我希望能够用c#编写一个程序,重写XML文件,使日期排序。有办法做到这一点吗?我知道如何读取XML文件,但是如何让程序重写它,使日期排序呢?

下面是XML:

     <?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="transform.xslt"?>
<Root>
  <Customers>
    <Customer CustomerID="GREAL">
      <CompanyName>Great Lakes Food Market</CompanyName>
      <ContactName>Howard Snyder</ContactName>
      <ContactTitle>Marketing Manager</ContactTitle>
      <Phone>(503) 555-7555</Phone>
      <FullAddress>
        <Address>2732 Baker Blvd.</Address>
        <City>Eugene</City>
        <Region>OR</Region>
        <PostalCode>97403</PostalCode>
        <Country>USA</Country>
      </FullAddress>
    </Customer>
    <Customer CustomerID="HUNGC">
      <CompanyName>Hungry Coyote Import Store</CompanyName>
      <ContactName>Yoshi Latimer</ContactName>
      <ContactTitle>Sales Representative</ContactTitle>
      <Phone>(503) 555-6874</Phone>
      <Fax>(503) 555-2376</Fax>
      <FullAddress>
        <Address>City Center Plaza 516 Main St.</Address>
        <City>Elgin</City>
        <Region>OR</Region>
        <PostalCode>97827</PostalCode>
        <Country>USA</Country>
      </FullAddress>
    </Customer>
    <Customer CustomerID="LAZYK">
      <CompanyName>Lazy K Kountry Store</CompanyName>
      <ContactName>John Steel</ContactName>
      <ContactTitle>Marketing Manager</ContactTitle>
      <Phone>(509) 555-7969</Phone>
      <Fax>(509) 555-6221</Fax>
      <FullAddress>
        <Address>12 Orchestra Terrace</Address>
        <City>Walla Walla</City>
        <Region>WA</Region>
        <PostalCode>99362</PostalCode>
        <Country>USA</Country>
      </FullAddress>
    </Customer>
    <Customer CustomerID="LETSS">
      <CompanyName>Let's Stop N Shop</CompanyName>
      <ContactName>Jaime Yorres</ContactName>
      <ContactTitle>Owner</ContactTitle>
      <Phone>(415) 555-5938</Phone>
      <FullAddress>
        <Address>87 Polk St. Suite 5</Address>
        <City>San Francisco</City>
        <Region>CA</Region>
        <PostalCode>94117</PostalCode>
        <Country>USA</Country>
      </FullAddress>
    </Customer>
  </Customers>
  <Orders>
    <Order>
      <CustomerID>GREAL</CustomerID>
      <EmployeeID>6</EmployeeID>
      <OrderDate>1997-05-06T00:00:00</OrderDate>
      <RequiredDate>1997-05-20T00:00:00</RequiredDate>
      <ShipInfo ShippedDate="1997-05-09T00:00:00">
        <ShipVia>2</ShipVia>
        <Freight>3.35</Freight>
        <ShipName>Great Lakes Food Market</ShipName>
        <ShipAddress>2732 Baker Blvd.</ShipAddress>
        <ShipCity>Eugene</ShipCity>
        <ShipRegion>OR</ShipRegion>
        <ShipPostalCode>97403</ShipPostalCode>
        <ShipCountry>USA</ShipCountry>
      </ShipInfo>
    </Order>
    <Order>
      <CustomerID>GREAL</CustomerID>
      <EmployeeID>8</EmployeeID>
      <OrderDate>1997-07-04T00:00:00</OrderDate>
      <RequiredDate>1997-08-01T00:00:00</RequiredDate>
      <ShipInfo ShippedDate="1997-07-14T00:00:00">
        <ShipVia>2</ShipVia>
        <Freight>4.42</Freight>
        <ShipName>Great Lakes Food Market</ShipName>
        <ShipAddress>2732 Baker Blvd.</ShipAddress>
        <ShipCity>Eugene</ShipCity>
        <ShipRegion>OR</ShipRegion>
        <ShipPostalCode>97403</ShipPostalCode>
        <ShipCountry>USA</ShipCountry>
      </ShipInfo>
    </Order>
    <Order>
      <CustomerID>GREAL</CustomerID>
      <EmployeeID>1</EmployeeID>
      <OrderDate>1997-07-31T00:00:00</OrderDate>
      <RequiredDate>1997-08-28T00:00:00</RequiredDate>
      <ShipInfo ShippedDate="1997-08-05T00:00:00">
        <ShipVia>2</ShipVia>
        <Freight>116.53</Freight>
        <ShipName>Great Lakes Food Market</ShipName>
        <ShipAddress>2732 Baker Blvd.</ShipAddress>
        <ShipCity>Eugene</ShipCity>
        <ShipRegion>OR</ShipRegion>
        <ShipPostalCode>97403</ShipPostalCode>
        <ShipCountry>USA</ShipCountry>
      </ShipInfo>
    </Order>
</Orders>
</Root>

我想让它按照OrderDate

排序
 class Program
    {
        static void Main(string[] args)
        {
            XDocument xDoc;
            xDoc = XDocument.Load("CustomerOrders.xml");
        }
    }

将XML重写为排序日期版本

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> myInts = new List<int>() { 1, 2, 3, 4, 5 };
            myInts.ForEach(x => x += 1);

            string input =
                "<Root>" +
                    "<Orders>" +
                        "<Order>" +
                          "<CustomerID>GREAL</CustomerID>" +
                          "<EmployeeID>8</EmployeeID>" +
                          "<OrderDate>1997-07-04T00:00:00</OrderDate>" +
                          "<RequiredDate>1997-08-01T00:00:00</RequiredDate>" +
                          "<ShipInfo ShippedDate='"1997-07-14T00:00:00'">" +
                            "<ShipVia>2</ShipVia>" +
                            "<Freight>4.42</Freight>" +
                            "<ShipName>Great Lakes Food Market</ShipName>" +
                            "<ShipAddress>2732 Baker Blvd.</ShipAddress>" +
                            "<ShipCity>Eugene</ShipCity>" +
                            "<ShipRegion>OR</ShipRegion>" +
                            "<ShipPostalCode>97403</ShipPostalCode>" +
                            "<ShipCountry>USA</ShipCountry>" +
                          "</ShipInfo>" +
                        "</Order>" +
                        "<Order>" +
                          "<CustomerID>GREAL</CustomerID>" +
                          "<EmployeeID>1</EmployeeID>" +
                          "<OrderDate>1997-07-31T00:00:00</OrderDate>" +
                          "<RequiredDate>1997-08-28T00:00:00</RequiredDate>" +
                          "<ShipInfo ShippedDate='"1997-08-05T00:00:00'">" +
                            "<ShipVia>2</ShipVia>" +
                            "<Freight>116.53</Freight>" +
                            "<ShipName>Great Lakes Food Market</ShipName>" +
                            "<ShipAddress>2732 Baker Blvd.</ShipAddress>" +
                            "<ShipCity>Eugene</ShipCity>" +
                            "<ShipRegion>OR</ShipRegion>" +
                            "<ShipPostalCode>97403</ShipPostalCode>" +
                            "<ShipCountry>USA</ShipCountry>" +
                          "</ShipInfo>" +
                        "</Order>" +
                        "<Order>" +
                          "<CustomerID>GREAL</CustomerID>" +
                          "<EmployeeID>3</EmployeeID>" +
                          "<OrderDate>1997-09-25T00:00:00</OrderDate>" +
                          "<RequiredDate>1997-10-23T00:00:00</RequiredDate>" +
                          "<ShipInfo ShippedDate='"1997-09-30T00:00:00'">" +
                            "<ShipVia>3</ShipVia>" +
                            "<Freight>76.13</Freight>" +
                            "<ShipName>Great Lakes Food Market</ShipName>" +
                            "<ShipAddress>2732 Baker Blvd.</ShipAddress>" +
                            "<ShipCity>Eugene</ShipCity>" +
                            "<ShipRegion>OR</ShipRegion>" +
                            "<ShipPostalCode>97403</ShipPostalCode>" +
                            "<ShipCountry>USA</ShipCountry>" +
                          "</ShipInfo>" +
                        "</Order>" +
                        "<Order>" +
                          "<CustomerID>GREAL</CustomerID>" +
                          "<EmployeeID>4</EmployeeID>" +
                          "<OrderDate>1998-01-06T00:00:00</OrderDate>" +
                          "<RequiredDate>1998-02-03T00:00:00</RequiredDate>" +
                          "<ShipInfo ShippedDate='"1998-02-04T00:00:00'">" +
                            "<ShipVia>2</ShipVia>" +
                            "<Freight>719.78</Freight>" +
                            "<ShipName>Great Lakes Food Market</ShipName>" +
                            "<ShipAddress>2732 Baker Blvd.</ShipAddress>" +
                            "<ShipCity>Eugene</ShipCity>" +
                            "<ShipRegion>OR</ShipRegion>" +
                            "<ShipPostalCode>97403</ShipPostalCode>" +
                            "<ShipCountry>USA</ShipCountry>" +
                          "</ShipInfo>" +
                        "</Order>" +
                        "<Order>" +
                          "<CustomerID>GREAL</CustomerID>" +
                          "<EmployeeID>3</EmployeeID>" +
                          "<OrderDate>1998-04-07T00:00:00</OrderDate>" +
                          "<RequiredDate>1998-05-05T00:00:00</RequiredDate>" +
                          "<ShipInfo ShippedDate='"1998-04-15T00:00:00'">" +
                            "<ShipVia>2</ShipVia>" +
                            "<Freight>25.19</Freight>" +
                            "<ShipName>Great Lakes Food Market</ShipName>" +
                            "<ShipAddress>2732 Baker Blvd.</ShipAddress>" +
                            "<ShipCity>Eugene</ShipCity>" +
                            "<ShipRegion>OR</ShipRegion>" +
                            "<ShipPostalCode>97403</ShipPostalCode>" +
                            "<ShipCountry>USA</ShipCountry>" +
                          "</ShipInfo>" +
                        "</Order>" +
                        "<Order>" +
                          "<CustomerID>GREAL</CustomerID>" +
                          "<EmployeeID>4</EmployeeID>" +
                          "<OrderDate>1998-04-22T00:00:00</OrderDate>" +
                          "<RequiredDate>1998-05-20T00:00:00</RequiredDate>" +
                          "<ShipInfo>" +
                            "<ShipVia>3</ShipVia>" +
                            "<Freight>18.84</Freight>" +
                            "<ShipName>Great Lakes Food Market</ShipName>" +
                            "<ShipAddress>2732 Baker Blvd.</ShipAddress>" +
                            "<ShipCity>Eugene</ShipCity>" +
                            "<ShipRegion>OR</ShipRegion>" +
                            "<ShipPostalCode>97403</ShipPostalCode>" +
                            "<ShipCountry>USA</ShipCountry>" +
                          "</ShipInfo>" +
                        "</Order>" +
                    "</Orders>" +
                "</Root>";
            //read file
            XDocument root = XDocument.Parse(input);
            //sort orders by date
            var sortedOrder = root.Descendants("Order").OrderBy(x => (DateTime)x.Element("OrderDate")).ToList();
            //get order element from document
            XElement orders = root.Descendants("Orders").FirstOrDefault();
            //replace original Orders element with new element containing sorts orders
            orders.ReplaceWith(new XElement("Orders",sortedOrder));
            //save file
            root.Save("filename");

        }
    }
}

基本上,尝试序列化和反序列化组合。例如:

少类:

public class XMLExample
{
    public DateTime Date { get; set; }
}
public class Root
{
    public List<XMLExample> XMLExamples { get; set; }
}

和序列化和反序列化在一个方法中:

class Program
{
    static void Main(string[] args)
    {
        // Just store this as a string for thie example
        string xml = "<Root><XMLExamples><XMLExample><Date>1997-10-23T00:00:00</Date></XMLExample><XMLExample><Date>2001-10-23T00:00:00</Date></XMLExample></XMLExamples></Root>";
        Root xmlData;
        XmlSerializer deserializer = new XmlSerializer(typeof(Root));
        // Deserialize to Root object
        using (var reader = new StringReader(xml))
        {
            object obj = deserializer.Deserialize(reader);
            xmlData = (Root)obj;
        }
        xmlData.XMLExamples = xmlData.XMLExamples.OrderByDescending(x => x.Date).ToList();
        // Now Serialize the sorted object
        string sortedXml = string.Empty;
        var serializer = new XmlSerializer(typeof(Root));
        using (var writer = new StringWriter())
        {
            serializer.Serialize(writer, xmlData);
            sortedXml = writer.ToString();
        }
        Console.WriteLine(sortedXml);
        Console.ReadLine();
    }
}

按OrderDate排序:

XElement root = XElement.Load(file);
// Get all the orders
var orders = root.Descendants("Order").ToList();
// Remove the orders from the XML file
orders.Remove();
// Sort the orders
var sortedOrders = orders.OrderBy(o => (DateTime)o.Element("OrderDate"));
// Re-Add them to the XML file
root.Element("Orders").Add(sortedOrders);
root.Save(file);