Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
[ServiceContract]
public interface IProduct
{
    [OperationContract]
    string GetID(int rno);

    [OperationContract]
    string GetName(int rno);

    [OperationContract]
    string GetDesc(int rno);

}


Product.cs
C#
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

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.
 
Share this answer
 
v2
Comments
P_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.
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
C#
ds.ReadXml("~/Products.xml");

with
C#
ds.ReadXml(@"C:\Products.xml");


and It worked.

Regards,
Gittu
 
Share this answer
 
Comments
goztepeli 31-Jan-22 8:38am    
Thanks for your solution. I will try it
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!
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900