Click here to Skip to main content
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 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 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 OriginalGriff 380
1 _Amy 255
2 Richard Deeming 250
3 Nirav Prabtani 223
4 CHill60 175
0 _Amy 110
1 Maciej Los 90
2 OriginalGriff 85
3 Mehdi Gholam 80
4 Sergey Alexandrovich Kryukov 75


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2014
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