7 Replies - 459 Views - Last Post: 27 April 2014 - 06:01 AM Rate Topic: -----

#1 ayefinch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-April 14

Using .Net Remoting with XML

Posted 25 April 2014 - 06:40 AM

Greetings all,

I am looking for some guidance regarding .Net remoting and XML. I have an exercise that requires the following,

****************************
C# Practice

Using .NET Remoting in C#, write server and client applications that fulfill the following requirements:

Tickets.xml (provided) contains ticket prices for certain destinations provided by an airplane chartering company.

Remotable object
This object is called Tickets. It loads the XML file called Tickets.xml into an XmlDocument object.
It provides a method to retrieve prices for all the destinations.
Another method retrieves a price for a specified destination.

Client
The client connects to the server.
The user has a choice to retrieve either prices for all destinations, or just one price for a specified destination entered by the user.
When a selection is made, the relevant function is called from the server, and the result is displayed in the console.

**********************************************

I have an example that uses a database. I don't however have an example to use for XML. There are plenty XML examples in my study material i.e. XMLTextReader, XMLNode, XMLDoc, XPath/Navigator and so on. Just nothing that I can see that explains how to modify the previously mentioned database example to the required XML format.

If possible would any one here be able to point me to a tutorial or some such that covers this? Google and youtube have not really been as helpful as I had hoped.

Thanks in advance

Finch

Is This A Good Question/Topic? 0
  • +

Replies To: Using .Net Remoting with XML

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,010
  • Joined: 05-May 12

Re: Using .Net Remoting with XML

Posted 25 April 2014 - 06:44 AM

Can you point us at the database remoting example?

And as a quick aside, who is still teaching .NET Remoting? Although still supported, it's been set aside for WCF.
Was This Post Helpful? 0
  • +
  • -

#3 ayefinch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-April 14

Re: Using .Net Remoting with XML

Posted 25 April 2014 - 06:58 AM

Hi Skydiver,

Thanks for the quick reply!

I'll have to type the database example out. I won't be able to do it now as I need to get to work in a bit.

I am doing a course at CTI in cape Town South Africa. They still do remoting and such. Dunno why...

Thanks again

Finch
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,010
  • Joined: 05-May 12

Re: Using .Net Remoting with XML

Posted 25 April 2014 - 10:56 AM

If you have to learn .NET Remoting, get the best book about it: Advanced .NET Remoting . When I was using it, it was an amazingly good guide.

In the meantime, you can follow along in the CodeProject tutorial. I just skimmed over it, but it looks like it encapsulates the basics of what you need.
Was This Post Helpful? 0
  • +
  • -

#5 ayefinch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-April 14

Re: Using .Net Remoting with XML

Posted 26 April 2014 - 03:51 AM

Ok so here's the code...

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using UseDatabase;

namespace DatabaseClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }



        private void btnExecute_Click(object sender, EventArgs e)
        {
            UseDatabase.UseDatabase useDb;

            try
            {
                //Get the object from the server
                useDb = (UseDatabase.UseDatabase)Activator.GetObject(typeof(UseDatabase.UseDatabase), "tcp://localhost:8085/UseDatabase");

                useDb = new UseDatabase.UseDatabase();

                //Call the Method
                DataSet ds = useDb.ExecuteQuery(tbQuery.Text);
                //Set the DG view control's DS
                gridResults.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show("An error has occured." + ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //the channel needs to be registered or the app won't work
            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel, true);
        }
    }
} 


Then the Server

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

namespace DatabaseServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //private void Form1_Load(object sender, EventArgs e)
        //{
        //    RunServer();
        //}

        public void RunServer()
        {
            try
            {
                //Create and register the channel using TCP and port 1902
                TcpChannel channel = new TcpChannel(1902);
                ChannelServices.RegisterChannel(channel, true);

                //Register the UseDatabase class
                RemotingConfiguration.RegisterWellKnownServiceType(typeof(UseDatabase.UseDatabase), "UseDatabase", WellKnownObjectMode.SingleCall);

                lblMessage.Text = "Server Running";
            }
            catch (Exception e)
            {
                lblMessage.Text = "An Error has occured...";
                MessageBox.Show(e.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            RunServer();
        }


    }
}
 


And lastly the Database

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace UseDatabase
{
    public class UseDatabase : MarshalByRefObject
    {
        OleDbConnection conn = new OleDbConnection();

        //default constructor
        public UseDatabase()
            : this("CollegeDatabase.accdb")
        {

        }

        public UseDatabase(string dbPath)
        {
            conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source = " + dbPath);
        }
        public DataSet ExecuteQuery(string query)
        {
            try
            {
                //open conn
                conn.Open();

                //Create and fill DS
                DataSet ds = new DataSet();
                OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn);
                adapter.Fill(ds);

                //Close conn
                conn.Close();

                //return DS
                return ds;
            }
            catch (Exception e)
            {
                //Display an error
                Console.WriteLine("An error has occured:" + e.Message);

                //rethrow e
                throw e;
            }
        }

    }
}
 


Basically what I need to know is what do I change to make this work(read) with an XML document. i.e. Which using statements etc.

Thank you for the time and input.

Regards

Finch
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,010
  • Joined: 05-May 12

Re: Using .Net Remoting with XML

Posted 26 April 2014 - 06:34 PM

Seems pretty straight forward. Instead of opening a database, you load the XML document. Add the appropriate methods, and you are done.
Was This Post Helpful? 0
  • +
  • -

#7 ayefinch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 25-April 14

Re: Using .Net Remoting with XML

Posted 27 April 2014 - 02:16 AM

View PostSkydiver, on 26 April 2014 - 06:34 PM, said:

Seems pretty straight forward. Instead of opening a database, you load the XML document. Add the appropriate methods, and you are done.


Yeah, I get that and I understand that it is simply a bit of modification to make the code work as spec'd. What's tripping me up is what exactly to change and where...

The following, for example, does pretty much the same except that it doesn't need the server and such.

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.XPath;

namespace XPathEx2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            XPathDocument xpDoc = new XPathDocument(@"..\..\data.xml");
            XPathNavigator xpNav = xpDoc.CreateNavigator();
            XPathNodeIterator xpNodeIt = xpNav.Select(txtExpression.Text);
            lstResults.Items.Clear();
            while (xpNodeIt.MoveNext())
            {
                lstResults.Items.Add(xpNodeIt.Current.NodeType.ToString() + ": " + xpNodeIt.Current.Name + " = " + xpNodeIt.Current.Value);
            }
        }
    }
}
 


I'll stab at it and see what I can come up with. It's just StreamReader/Writer and such so it can't be that hard. just a little bit frustrating....

Thanks again

Finch
Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,010
  • Joined: 05-May 12

Re: Using .Net Remoting with XML

Posted 27 April 2014 - 06:01 AM

Go back to the code you had in post #5, and for now, just touch your UseDatabase class. Instead of opening an OLEDB connection, just load the XML document. For now, change the ExecuteQuery() to become GetPriceForDestination() where you try to find the price for a given destination. When you have that working, you can add another method to return all destinations and prices.

Once you have those working, then convert your Client to become a console application.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1