Click here to Skip to main content
12,450,981 members (35,414 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET WCF
Hello Friends,
I've this WCF Service where I need to Read Some data from an XML file & then I need to return that data to Client as string.
But I'm facing problem to Read the XML File in WCF Service Project.

Here is the code I used to Read & return the values:

IProduct.cs
[ServiceContract]
public interface IProduct
{
    [OperationContract]
    string GetID(int rno);
 
    [OperationContract]
    string GetName(int rno);
 
    [OperationContract]
    string GetDesc(int rno);
 
}

Product.cs
public class Product : IProduct
{
 
    public string GetID(int rno)
    {
        DataSet ds = new DataSet();
        try
        {
            ds.ReadXml("~/Products.xml");
            string res = ds.Tables[0].Rows[rno][0].ToString();
            return res;
        }
        catch
        {
            return "No More Data";
        }
    }
 
    public string GetName(int rno)
    {
        DataSet ds = new DataSet();
        try
        {
            ds.ReadXml("Products.xml");
            string res = ds.Tables[0].Rows[rno][0].ToString();
            return res;
        }
        catch
        {
            return "No More Data";
        }
    }
 
    public string GetDesc(int rno)
    {
        DataSet ds = new DataSet();
        try
        {
            ds.ReadXml("Products.xml");
            string res = ds.Tables[0].Rows[rno][0].ToString();
            return res;
        }
        catch
        {
            return "No More Data";
        }
    }
}

Now Friends plz Suggest me how can I read this XML File & return the data to Client ?

Thanks in Advance.
Regards,
Dash
Posted 11-Feb-13 4:31am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Unless you specify what the problem is, or what errors it's returning, it's hard to tell. I suggest you put a breakpoint on each of your 'ds.ReadXml("Products.xml")' lines to find out what data it returns into the DataSet (if any). This will give both you (and us) a much better idea of where the problem lies.
  Permalink  
v2
Comments
Gittu Dash 11-Feb-13 12:10pm
   
Bro, the Service always returns the string present inside catch block i.e. "No More Data"
I checked it properly & the Error lies in reading the xml file.

I used to use
ds.ReadXml(Server.MapPath("File.xml"));
to read XML files in aspx pages, but this code isn't working here.

So I'm asking you guys, if there is any way I can read the Xml file.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Well Guys I found the Solution myself.
Previously I mapped the xml file path as I used to do in aspx pages.
But WCF Service doesn't take absolute path.
So I just replaced
ds.ReadXml("~/Products.xml");
with
ds.ReadXml(@"C:\Products.xml");

and It worked.

Regards,
Gittu
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Thanks for sharing!

Instead of c:\ hardcoded path, in WCF service, you can save product.xml in App_Data folder.

You can try below code by System.Web.Hosting and System.IO namespace references.

string filePath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "App_Data", "Products.xml");
ds.ReadXml(filePath);

Thank you!
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160826.1 | Last Updated 8 Sep 2015
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100