Click here to Skip to main content
11,410,750 members (63,249 online)
Rate this: bad
good
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 5: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 at 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  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 8,885
1 OriginalGriff 6,915
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web01 | 2.8.150414.5 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2015
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