急!!c#中,datalist如何显示从webservice接口中调用的xml格式数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了急!!c#中,datalist如何显示从webservice接口中调用的xml格式数据相关的知识,希望对你有一定的参考价值。

我今用c#调用webservice接口,调出来的数据是xml格式数据:

<Data schema="PUBTRANSPACTREC">
<PUBTRANSPACTREC>
<ID>1772038</ID>
<CODE>200908120009</CODE>
<CONTRACTTYPE>转包</CONTRACTTYPE>
<SERCODE>201002000185830</SERCODE>
<INNAME>宋永可</INNAME>
<INDEPUTY xml:space="preserve"> </INDEPUTY>
<INADDR xml:space="preserve"> </INADDR>
<OUTNAME>宋永波</OUTNAME>
<OUTDEPUTY xml:space="preserve"> </OUTDEPUTY>
<OUTADDR xml:space="preserve"> </OUTADDR>
<SIGDATE xml:space="preserve"> </SIGDATE>
<SIGPURPOSE xml:space="preserve"> </SIGPURPOSE>
<PERIOD>12</PERIOD>
<STARTDATE>2009-03-15</STARTDATE>
<ENDDATE>2010-03-14</ENDDATE>
<DEALTYPE xml:space="preserve"> </DEALTYPE>
<NOTE xml:space="preserve"> </NOTE>
<TOTALAREA>2.8</TOTALAREA>
<DEALTERM xml:space="preserve"> </DEALTERM>
<EYEEITENESS xml:space="preserve"> </EYEEITENESS>
<VISEDATE xml:space="preserve"> </VISEDATE>
<SIGORG xml:space="preserve"> </SIGORG>
<DUTY xml:space="preserve"> </DUTY>
<PAYTERM xml:space="preserve"> </PAYTERM>
<PERYINC>0.0</PERYINC>
<GOODS>0.0</GOODS>
<CASH>500.0</CASH>
<OTHERRASS xml:space="preserve"> </OTHERRASS>
<USEDPURPOSE>种植业</USEDPURPOSE>
<PAYTYPE>一次性支付</PAYTYPE>
<RENTYPE>以现金方式支付</RENTYPE>
<LOCATION xml:space="preserve"> </LOCATION>
<SOILNAME>排井161312040702055014</SOILNAME>
<SOILAREA>1.5</SOILAREA>
<EASTTO>宋计伍</EASTTO>
<WESTTO>宋永峰</WESTTO>
<SOUTHTO>沟</SOUTHTO>
<NORTHTO>路</NORTHTO>
<SOILLEVEL>一级</SOILLEVEL>
</PUBTRANSPACTREC>
</Data>

请问我应该怎么写,可以使调出来的数据用datalist显示出来

/// 读取xml直接返回DataSet
///
/// xml文件相对路径
///
public static DataSet GetDataSetByXml(string strXmlPath)

try

DataSet ds = new DataSet();
ds.ReadXml(GetXmlFullPath(strXmlPath));
if(ds.Tables.Count > 0)

return ds;

return null;

catch(Exception ex)

System.Windows.Forms.MessageBox.Show(ex.ToString());
return null;

追问

我的读取程序是:
WebReference.PubSoilDataWebService pubSoilWS = new WebReference.PubSoilDataWebService();
private void bindGY()
string strXml= pubSoilWS.GetTransPactData();
DataSet ds = new DataSet();
ds.ReadXmlSchema();
ds.ReadXml();
this.dlLZ2.DataSource = ds;
this.dlLZ2.DataBind();

您在给我看看

追答

把ps改成ds试试

追问

private void bindGY()
string strXml= pubSoilWS.GetTransPactData();
DataSet ds = new DataSet();
---------------------------------
ds.ReadXml( 里面写啥 );
---------------------------------
this.dlLZ2.DataSource = ds;
this.dlLZ2.DataBind();

这样显示:System.ArgumentException: URL 不能为空。

追答

写xml文档的实际路径
--------------------------------------------------------------------------
给你个方法
ds.ReadXml( GetXmlFullPath(xml文档的相对路径));
public static string GetXmlFullPath(string strPath)

if(strPath.IndexOf(":") > 0)

return strPath;

else

return Application.StartupPath + strPath;

参考技术A 去 csdn 论坛看看 里面有人回答的!!

如何从datalist更新数据库表?

我正在建立一个电子商务网站,我使用数据列表显示我的产品,当我按下“添加到购物车”按钮时,它们应该被添加到数据库表中但由于某种原因,产品不是在数据库表中更新。此外,单击第一个产品会出现错误,说“已经有一个打开的阅读器”,显示错误的行是“int result = comm.ExecuteNonQuery();”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Globalization;

namespace MWM1812
{
    public partial class productCatalog : System.Web.UI.Page
    {
        public enum MessageType { Success, Error, Info, Warning };
        protected void ShowMessage(string Message, MessageType type)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), "ShowMessage('" + Message + "','" + type + "');", true);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }




        protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "AddToCart")
            {
                Session["uid"] = 1;
                Label lbl = (Label)e.Item.FindControl("PIDLabel");
                lblAtC.Text = lbl.Text;
                ShowMessage("Product successfully added to Cart", MessageType.Success);

                string connStr = ConfigurationManager.ConnectionStrings["MWM1812ConnString"].ConnectionString;
                SqlConnection conn = new SqlConnection(connStr);

                conn.Open();

                string sqlQuery = "SELECT * FROM tblShoppingCart WHERE uid=@uid AND pid=@pid";
                SqlCommand comm = new SqlCommand(sqlQuery, conn);
                comm.Parameters.AddWithValue("@uid", Session["uid"]);
                comm.Parameters.AddWithValue("@pid", lblAtC.Text.Trim());
                using (SqlDataReader reader = comm.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        string sqlQuery2 = "UPDATE tblShoppingCart SET qty=qty+1 WHERE uid=@uid AND pid=@pid";
                        SqlCommand comm1 = new SqlCommand(sqlQuery2, conn);
                        comm1.Parameters.AddWithValue("@qty", 1);
                        comm1.Parameters.AddWithValue("@dtShopped", DateTime.Now.ToString());
                        //// Add code for adding parameters and executing sqlQuery2 
                        int result = comm1.ExecuteNonQuery();
                    }
                    else
                    {



                        string sqlQuery1 = "INSERT INTO tblShoppingCart (uid, pid, qty, dtShopped) VALUES (@uid, @pid, @qty, @dtShopped)";
                        SqlCommand comma = new SqlCommand(sqlQuery1, conn);
                        comma.Parameters.AddWithValue("@uid", Session["uid"]);
                        comma.Parameters.AddWithValue("@pid", lblAtC.Text);
                        comma.Parameters.AddWithValue("@qty", 1);
                        //comm.Parameters.AddWithValue("@dtShopped", DateTime.Now.ToLongDateString());
                        comma.Parameters.AddWithValue("@dtShopped", DateTime.Now.ToString());

                        int result = comma.ExecuteNonQuery();


                    }
                }
            }
        }
    }
}

答案

尝试像这样更新代码:

if (e.CommandName == "AddToCart") {
     bool productExists = false;

     Session["uid"] = 1;
     Label lbl = (Label) e.Item.FindControl("PIDLabel");
     lblAtC.Text = lbl.Text;
     ShowMessage("Product successfully added to Cart", MessageType.Success);

     string connStr = ConfigurationManager.ConnectionStrings["MWM1812ConnString"].ConnectionString;
     SqlConnection conn = new SqlConnection(connStr);

     conn.Open();

     string sqlQuery = "SELECT * FROM tblShoppingCart WHERE uid=@uid AND pid=@pid";
     SqlCommand comm = new SqlCommand(sqlQuery, conn);
     comm.Parameters.AddWithValue("@uid", Session["uid"]);
     comm.Parameters.AddWithValue("@pid", lblAtC.Text.Trim());

     using(SqlDataReader reader = comm.ExecuteReader()) {    
        productExists = reader.HasRows;
     }

      if (productExists) {
       string sqlQuery2 = "UPDATE tblShoppingCart SET qty=qty+1 WHERE uid=@uid AND pid=@pid";

       // Add code for adding parameters and executing sqlQuery2 
      } else {



        string sqlQuery1 = "INSERT INTO tblShoppingCart (uid, pid, qty, dtShopped) VALUES (@uid, @pid, @qty, @dtShopped)";
        SqlCommand comma = new SqlCommand(sqlQuery, conn);
        comma.Parameters.AddWithValue("@uid", Session["uid"]);
        comma.Parameters.AddWithValue("@pid", lblAtC.Text);
        comma.Parameters.AddWithValue("@qty", 1);
        //comm.Parameters.AddWithValue("@dtShopped", DateTime.Now.ToLongDateString());
        comma.Parameters.AddWithValue("@dtShopped", DateTime.Now.ToString());

        int result = comma.ExecuteNonQuery();


      }     
    }

请注意使用reader.HasRows属性来确定产品是否已存在。此外,SqlDataReader包含在using块中以便将其处理掉。

以上是关于急!!c#中,datalist如何显示从webservice接口中调用的xml格式数据的主要内容,如果未能解决你的问题,请参考以下文章

用datalist如何实现分页的代码(C#)

.net(c#)Repeater控件问题

如何从datalist更新数据库表?

Jquery Ajax 如何实现动态加载Repeater 或者 DataList

c# winform listbox 如何 获取 当前 选中的值 急!!!

急!C# 从文件名里去处特殊字符