XML文件而不是XML字符串

时间:2015-10-01 19:05:55

标签: c# xml

我希望我的程序输入XML文件而不是字符串,这可能吗?

现在我喜欢这个:

string input =
            "<Root>" +
                    "<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>1991-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);

但我希望XDocument.Parse接受XML文件

XDocument root = XDocument.Parse(theFile);

这可能吗?有没有解决的办法?我需要我的程序来接收XML文件

1 个答案:

答案 0 :(得分:3)

您可以使用具有大量重载的Click here,其中一个接受string URI:

var root = XDocument.Load(@"C:\pathtoxml.xml");