Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6722009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:23:47+00:00 2026-05-26T09:23:47+00:00

I am trying to search XML document for specific information. In the first part

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:23:48+00:00Added an answer on May 26, 2026 at 9:23 am

    How about

     string xmlString =  System.IO.File.ReadAllText(fileName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Word 2003 XML document that I am trying to search for
I'm trying to pull in an src value from an XML document, and in
I am trying to search an xml element from the root element of the
I'm trying to search eBay via C# and XML. I can see that I'm
I have an XML file that I am trying to search using Java. I
I'm trying to create an in-memory xml document such that the root's child nodes
I am trying to read xml nodes values from lastfm web service that look
I am trying to search an XML field within a table, This is not
I'm trying to search multiple attributes in XML : <APIS> <API Key=00001> <field Username=username1
I have some XML which I'm trying to search: <debts> <section id=24 description=YYYYY> <section

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.