none
How to read XML File in a WCF Service ? RRS feed

  • Question

  • 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 whenever I try to Consume the Service it returns me value present inside Catch block for every method.

    Using break point I debug the code I found out the problem is reading the Xml file.

    So I'd be greatly helped if anybody provide me any working solution for it

    Thanks & Regards.

    Monday, February 11, 2013 5:37 PM

Answers

  • Well I found answer myself.

    Actually the problem was in the path of XML File.

    I used to map the XML file as in aspx pages.But here in WCF it takes complete physical path.

    So I just changed the "ds.ReadXml("Products.xml");" with "s.ReadXml(@"C:\Products.xml");" & the problem got fixed.

    Regards

    Monday, February 11, 2013 6:14 PM

All replies

  • Hi,

    Set a breakpoint inside catch block and see what exception is being raised. Most probably it is from ReadXml method that you are using or probably calling .ToString() on null value of cell.

    Thanks,

    BR,

    JollyWagoner.

    Monday, February 11, 2013 5:55 PM
  • Well I found answer myself.

    Actually the problem was in the path of XML File.

    I used to map the XML file as in aspx pages.But here in WCF it takes complete physical path.

    So I just changed the "ds.ReadXml("Products.xml");" with "s.ReadXml(@"C:\Products.xml");" & the problem got fixed.

    Regards

    Monday, February 11, 2013 6:14 PM