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

时间:2022-09-13 08:05:21

I am trying to search XML document for specific information. In the first part of the program I display all the info from the XML to a console (that is easy and I have done that) and in the second I am trying to search among the nodes for specific info to display it on the console. I have done this too but I don't know how to read the XML from a XML file (order.xml) and convert it to string in order to use it.

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

This is my code:

这是我的代码:

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>

And this is the C# code:

这是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);
        }
    }

}
}

As you can see the first part gets the info from the XML file, but I have to use string with XML info in the second part. So to repeat the question. How to use the XML file in the second part of the example and not the string? Or how can I convert XML file to string and then use it in the second part?

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

2 个解决方案

#1


2  

How about

 string xmlString =  System.IO.File.ReadAllText(fileName);

#2


11  

It's unclear why you're loading the XML document into a DataSet to start with. Just use XmlDocument (or preferrably XDocument if you're using .NET 3.5 or later) throughout your code. I'd also strongly recommend against building up XML as a string in your code at all. Basically, when you want to deal with XML, use an XML API.

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

So to load an XML file:

所以要加载XML文件:

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

// XDocument version
XDocument doc = XDocument.Load(filename);

#1


2  

How about

 string xmlString =  System.IO.File.ReadAllText(fileName);

#2


11  

It's unclear why you're loading the XML document into a DataSet to start with. Just use XmlDocument (or preferrably XDocument if you're using .NET 3.5 or later) throughout your code. I'd also strongly recommend against building up XML as a string in your code at all. Basically, when you want to deal with XML, use an XML API.

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

So to load an XML file:

所以要加载XML文件:

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

// XDocument version
XDocument doc = XDocument.Load(filename);