你如何在datagridview或reportview的windows窗体应用程序中显示来自wcf服务的xml数据? [英] how do you display xml data from wcf service in windows form app in datagridview or reportview?

查看:23
本文介绍了你如何在datagridview或reportview的windows窗体应用程序中显示来自wcf服务的xml数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 wcf 和 rest 操作的新手,因此非常感谢您的帮助.

I'm new to wcf and rest operations so any help would be great thank you.

如何在 rdlc 或 datagridview 中显示信息?

How do i display information in a rdlc or a datagridview?

我的界面使用名为 GetAllCustomer 的方法设置操作合同

My Interface set up operation contract with a method called GetAllCustomer

namespace ConnectionToDatabase
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IGetCustomer" in both code and config file together.
    [ServiceContract]
    public interface IGetCustomer
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/customers")]        
        List<Customer> GetAllCustomer();
    }
}

我的客户类实现了接口类并连接到 sql 数据库并返回客户信息列表

My customer class which implements interface class and connects to a sql database and returns a list of customer info

public class GetCustomer : IGetCustomer
    {
        public List<Customer> GetAllCustomer()
        {
            List<Customer> mylist = new List<Customer>();

            using (SqlConnection conn = new SqlConnection("Data Source=MATRIX\;Initial Catalog=******;Integrated Security=True"))
            {
                conn.Open();

                string cmdStr = String.Format("select * from CInformation c inner join EmploymentInformation e on c.CID = e.CID");
                SqlCommand cmd = new SqlCommand(cmdStr, conn);
                SqlDataReader rd = cmd.ExecuteReader();

                if (rd.HasRows)
                {
                    while (rd.Read())
                    {
                        mylist.Add(new Customer(rd["LastName"].ToString(), rd["FirstName"].ToString(), rd["MiddleName"].ToString(), rd["EmailAddress"].ToString(), rd["PhoneNumber"]));
                    }
                }
                conn.Close();
            }

            return mylist;
        }
    }

    [DataContract]
    public class Customer
    {
        [DataMember]
        public string lastname { get; set; }
        [DataMember]
        public string firstname { get; set; }
        [DataMember]
        public string middlename { get; set; }
        [DataMember]
        public string emailaddress { get; set; }
        [DataMember]
        public string phonenumber { get; set; }            
        public Customer(string last, string first, string middle, string email, string phone)
        {
            this.lastname = last;
            this.firstname = first;
            this.middlename = middle;
            this.emailaddress = email;
            this.phonenumber = phone;                
        }


    }//end of class

网络配置

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

    <services>
      <service name="ConnectionToDatabase.GetCustomer" behaviorConfiguration="EmpServiceBehaviour">
        <endpoint address ="" binding="webHttpBinding" contract="ConnectionToDatabase.IGetCustomer" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="EmpServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

windows 窗体应用

windows form app

public Form1()
{
    InitializeComponent();
}

private void btnGetAllCustomers_Click(object sender, EventArgs e)
{
 WebRequest request = WebRequest.Create("http://localhost:52420/GetCustomer.svc/xml/customers");
        WebResponse ws = request.GetResponse();

        StreamReader responseStream = new StreamReader(ws.GetResponseStream());
        string response = responseStream.ReadLine();
        responseStream.Close();   
        textBox1.AppendText(response);
}  

现在看起来像这样

在文本框中

<GetAllCustomerResponse xmlns="http://tempuri.org/"><GetAllCustomerResult xmlns:a="http://schemas.datacontract.org/2004/07/ConnectionToDatabase" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:Customer><a:firstname>Dog</a:firstname><a:lastname>Cat</a:lastname><a:middlename>up</a:middlename></a:Customer></GetAllCustomerResult></GetAllCustomerResponse>

推荐答案

你需要喜欢这个

    using (nServiceReferenceCustomers.GetCustomerClient svc = new ServiceReferenceCustomers.GetCustomerClient())
    {
        XmlNode node = svc.GetAllCustomer();
        DataSet ds = new DataSet();
        using (XmlNodeReader reader = new XmlNodeReader(node))
        {
            ds.ReadXml(reader);
        }


        DataTable table = ds.Tables["Table"];
        DataRow row = table.Rows[0];
        string Lastname= (string) row["lastname"];
        string Firstname= (string) row["firstname"];
        string Middlename= (string) row["middlename "];
string email= (string) row["emailaddress"];
string Phone= (string) row["phonenumber"];
    }

这篇关于你如何在datagridview或reportview的windows窗体应用程序中显示来自wcf服务的xml数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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