如何在WCF服务中读取XML文件? [英] How to read XML File in a WCF Service ?

查看:87
本文介绍了如何在WCF服务中读取XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Friends,

我有这个WCF服务,我需要读取XML文件中的一些数据&然后我需要将该数据作为字符串返回给客户端。

但我在WCF服务项目中读取XML文件时遇到问题。



这是我用来阅读和阅读的代码。返回值:



IProduct.cs

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



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";
        }
    }
}





现在好友plz建议我如何阅读此XML文件&安培;将数据返回给客户?



先谢谢。

问候,

Dash



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

Thanks in Advance.
Regards,
Dash

推荐答案

感谢分享!



而不是c:\硬编码路径,在WCF服务中,您可以保存产品。 App_Data文件夹中的xml。



您可以通过System.Web.Hosting和System.IO命名空间参考尝试以下代码。



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

ds.ReadXml(filePath);



谢谢!
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!


除非你指明问题是什么,或者它返回了什么错误,否则很难说。我建议你在每个'ds.ReadXml(Products.xml)'行上放一个断点,找出它返回到DataSet中的数据(如果有的话)。这将使你(和我们)更好地了解问题所在。
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.


Well Guys我自己找到了解决方案。

之前我已经映射了xml文件路径,就像我以前在aspx页面中做的那样。

但是WCF服务没有走绝对路径。

所以我刚刚更换了

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


with

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





和它工作。



问候,

Gittu



and It worked.

Regards,
Gittu


这篇关于如何在WCF服务中读取XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆