Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# convert XML to string in order to search it

Tags:

string

c#

xml

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.

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:

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?

like image 937
Vojtech Avatar asked Dec 03 '25 19:12

Vojtech


1 Answers

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.

So to load an XML file:

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

// XDocument version
XDocument doc = XDocument.Load(filename);
like image 50
Jon Skeet Avatar answered Dec 06 '25 07:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!