C#将XML转换为字符串以便搜索它

Voj*_*ech 3 c# xml string

我正在尝试搜索XML文档以获取特定信息.在程序的第一部分,我将所有信息从XML显示到控制台(这很简单,我已经完成了),在第二部分,我试图在节点之间搜索特定信息,以便在控制台上显示它.我也这样做了,但我不知道如何从XML文件(order.xml)读取XML并将其转换为字符串以便使用它.

这是我的代码:

order.xml

<?xml version="1.0" encoding="utf-8" ?>
<ordercat>
  <order order_ID="1" employee_ID="125">
    <CustomerId>1</CustomerId>
    <OrderDate>19.12.2009</OrderDate>
    <ShippedDate>21.12.2011</ShippedDate>
    <ShipName>Sven Skanske</ShipName>
    <ShipAddress>Stockholm 542, Stockolm</ShipAddress>
    <ShipCountry>Sweden</ShipCountry>
  </order>
  <order order_ID="2" employee_ID="145">
    <CustomerId>5</CustomerId>
    <OrderDate>25.10.2010</OrderDate>
<ShippedDate>31.10.2010</ShippedDate>
<ShipName>Jan Hoznovski</ShipName>
<ShipAddress>Warsawska 212, Warsaw</ShipAddress>
<ShipCountry>Poland</ShipCountry>
  </order>
  <order order_ID="3" customerID="4" employee_ID="112">
    <CustomerId>4</CustomerId>
    <OrderDate>15.10.2011</OrderDate>
    <ShippedDate>16.10.2011</ShippedDate>
    <ShipName>Martin Petrzilka</ShipName>
    <ShipAddress>U Hrocha 2145, Sedlcany</ShipAddress>
    <ShipCountry>Czech Republic</ShipCountry>
  </order>
</ordercat>
Run Code Online (Sandbox Code Playgroud)

这是C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.XPath;

namespace XML
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSet ds = new DataSet();
            string pathe = @"D:\Docs\Kristianstad\Homework_5\XML\XML\order.xml";
            ds.ReadXml(pathe);

        foreach (DataTable dt in ds.Tables)
        {
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn column in dt.Columns)
                {
                    Console.WriteLine(row[column]);
                }
                Console.WriteLine();
            }
        }
        Console.WriteLine("Press any key to continue ...");
        Console.ReadKey();
        Console.WriteLine("");

        string xmlText = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
        xmlText += "<ordercat>";
        xmlText += "<order order_ID=\"1\" employee_ID=\"125\">";
        xmlText += "<CustomerId>1</CustomerId>";
        xmlText += "<OrderDate>19.12.2009</OrderDate>";
        xmlText += "<ShippedDate>21.12.2011</ShippedDate>";
        xmlText += "<ShipName>Sven Skanske</ShipName>";
        xmlText += "<ShipAddress>Stockholm 542, Stockolm</ShipAddress>";
        xmlText += "<ShipCountry>Sweden</ShipCountry>";
        xmlText += "</order>";
        xmlText += "<order order_ID=\"2\" employee_ID=\"145\">";
        xmlText += "<CustomerId>5</CustomerId>";
        xmlText += "<OrderDate>25.10.2010</OrderDate>";
        xmlText += "<ShippedDate>31.10.2010</ShippedDate>";
        xmlText += "<ShipName>Jan Hoznovski</ShipName>";
        xmlText += "<ShipAddress>Warsawska 212, Warsaw</ShipAddress>";
        xmlText += "<ShipCountry>Poland</ShipCountry>";
        xmlText += "</order>";
        xmlText += "<order order_ID=\"3\" customerID=\"4\" employee_ID=\"112\">";
        xmlText += "<CustomerId>4</CustomerId>";
        xmlText += "<OrderDate>15.10.2011</OrderDate>";
        xmlText += "<ShippedDate>16.10.2011</ShippedDate>";
        xmlText += "<ShipName>Martin Petrzilka</ShipName>";
        xmlText += "<ShipAddress>U Hrocha 2145, Sedlcany</ShipAddress>";
        xmlText += "<ShipCountry>Czech Republic</ShipCountry>";
        xmlText += "</order>";
        xmlText += "</ordercat>";

        XmlDocument xml = new XmlDocument();
        xml.LoadXml(xmlText);

        XmlNodeList xnList = xml.SelectNodes("/ordercat/order[CustomerId='5']");
        foreach (XmlNode xn in xnList)
        {
            string shippedDate = xn["ShippedDate"].InnerText;
            string shipName = xn["ShipName"].InnerText;
            Console.WriteLine(shippedDate + " " + shipName);
        }
    }

}
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,第一部分从XML文件中获取信息,但我必须在第二部分中使用带有XML信息的字符串.所以重复这个问题.如何在示例的第二部分使用XML文件而不是字符串?或者我如何将XML文件转换为字符串,然后在第二部分中使用它?

Jon*_*eet 11

目前还不清楚为什么要将XML文档加载到一个DataSet开头.在整个代码中使用XmlDocument(或者最好XDocument是使用.NET 3.5或更高版本).我还强烈建议不要在代码中将XML构建为字符串.基本上,当您想要处理XML时,请使用XML API.

所以要加载XML文件:

// XmlDocument version
XmlDocument doc = new XmlDocument();
doc.Load(filename);

// XDocument version
XDocument doc = XDocument.Load(filename);
Run Code Online (Sandbox Code Playgroud)