加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 00:18:04 所属栏目:百科 来源:网络整理
导读:我正在尝试搜索 XML文档以获取特定信息.在程序的第一部分,我将所有信息从XML显示到控制台(这很简单,我已经完成了),在第二部分,我试图在节点之间搜索特定信息,以便在控制台上显示它.我也这样做了,但我不知道如何从XML文件(order.xml)读取XML并将其转换为字符
我正在尝试搜索 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:DocsKristianstadHomework_5XMLXMLorder.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文件转换为字符串,然后在第二部分中使用它?

解决方法

怎么样

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读