从WCF应用程序返回XML

时间:2013-05-24 12:53:55

标签: .net wcf

我正在使用WCF从我的Web应用程序使用.net框架从数据库中获取数据。在我的WCF应用程序代码中,要求是以XML格式获取数据。在我的Web应用程序代码中,要求是使用object调用WCF服务以获取XML格式的数据库数据。

WCF service.cs代码如下

public class AWS_WCF_Service : IAWS_WCF_Service
{
    public SqlConnection Con = new SqlConnection();

    public SqlConnectionStringBuilder Sbu = new SqlConnectionStringBuilder();

    public static SqlConnection GlobalConnection_Web = new SqlConnection();

    public SqlConnectionStringBuilder Sbu_Web = new SqlConnectionStringBuilder();
    public static DataTable dt = new DataTable();

    public static DataSet myDataSet = new DataSet();


    public void DoWork()
    {

    }


    public XmlDocument RetrieveData(String Tblname, String StationID, DateTime date)
    {
        bool flgPrcs = OpenDB();

        if (flgPrcs == false)
        {
            return null;
        }

        String Query = "select * from " + Tblname + " where ID = ' 1 ' "

        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(Query, Con);
        adapter.Fill(dt);

        myDataSet.Tables.Add(dt);
        string xmlStr = "";
        XmlDocument xmlDoc = new XmlDocument();

        xmlStr = myDataSet.GetXml();

        xmlDoc.LoadXml(xmlStr);

        return xmlDoc;
    }

    public bool OpenDB()
    {
        if (Con.State.ToString().ToUpper() == "OPEN")
        {
            Con.Close();
        }

        Sbu.DataSource = @"XXXXXXX";
        Sbu.InitialCatalog = "XXXXX";
        Sbu.IntegratedSecurity = false;
        Sbu.UserID = "XXXX";
        Sbu.Password = "XXXXX";

        Con.ConnectionString = Sbu.ConnectionString;
        Con.Open();

        return true;
    }

    public bool CloseDB()
    {
        if (Con.State.ToString().ToUpper() == "OPEN")
        {
            Con.Close();
        }

        return true;
    }
}

WCF接口代码如下

[ServiceContract]
public interface IAWS_WCF_Service
{
    [OperationContract]
    void DoWork();

    [OperationContract,XmlSerializerFormat]
    System.Xml.XmlDocument RetrieveData(String Tblname, String StationID, DateTime  date);

    [OperationContract]
    bool OpenDB();

    [OperationContract]
    bool CloseDB();

}

Web应用程序,我使用WCF将数据转换为XML,代码如下

 AWS_Service1.AWS_WCF_ServiceClient ObjAwsSer = new AWS_WCF_ServiceClient();

 XmlDocument OBJXml = ObjAwsSer.RetrieveData("XXX", "XX", DateTime.Now);

我收到以下错误

  

无法将类型'System.Xml.Linq.XElement'隐式转换为'System.Xml.XmlDocument'

0 个答案:

没有答案