简体   繁体   English

XML文件而不是XML字符串

[英]XML-file instead of XML string

I want my program to input a XML-file instead of string, is it possible? 我希望程序输入XML文件而不是字符串,这可能吗?

Right now I have like this: 现在我有这样的:

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

But I want the XDocument.Parse to take in a XML-file instead 但是我希望XDocument.Parse代替XML文件

XDocument root = XDocument.Parse(theFile);

Is this possible? 这可能吗? Is there a way around this? 有没有解决的办法? I need my program to take in a XML-file 我需要我的程序来接收XML文件

您可以使用XDocument.Load ,它具有大量的重载,其中一个接受string URI:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM