c#将XML转换为字符串以便进行搜索

本文关键字:搜索 字符串 XML 转换 | 更新日期: 2023-09-27 18:12:04

我正在尝试搜索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>
这是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);
        }
    }
}
}
如您所见,第一部分从XML文件中获取信息,但我必须在第二部分中使用带有XML信息的字符串。重复一下问题。如何在示例的第二部分使用XML文件而不是字符串?或者如何将XML文件转换为字符串,然后在第二部分中使用它?

c#将XML转换为字符串以便进行搜索

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

加载XML文件:

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

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