将XML文件转换为已排序的文件

时间:2015-10-01 14:11:37

标签: c# xml

我尝试编写一个程序,将XML文件转换为按日期排序 XML文件如下所示:

    <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>

我希望按 OrderDate 进行排序但我不知道该怎么做。任何人都可以给我任何建议吗?

2 个答案:

答案 0 :(得分:0)

XDocument xDoc = XDocument.Parse(xmlDoc);//xmlDoc = your xml file or input stream
IList<XElement> records = xDoc.Root.Elements("Order").OrderBy(x => x.Element("OrderDate").Value).ToList();

这是OrderDate订购的所有订单记录的列表

如果您希望将其存储在文件中,请使用“记录”作为数据源重新创建XML文件

答案 1 :(得分:0)

我使用了Linq 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)
        {
            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>";

            XDocument root = XDocument.Parse(input);

            var sortedOrder = root.Descendants("Order").Select(x => new
            {
                order = x,
                orderDate = (DateTime)x.Element("OrderDate")
            }).OrderBy(y => y.orderDate).ToList();

            XElement sortedXml = new XElement("Root", new XElement("Orders"));
            XElement orders = sortedXml.Descendants("Orders").FirstOrDefault();
            foreach (var ordered in sortedOrder)
            {
                orders.Add(ordered.order);
            }
        }
    }
}
​