0

我创建了自己的 Web 服务来使用。我添加了服务参考。但是在 store_getProducts 处,XDocument xReturn = XDocument.Load(e.Result); 发生错误 它说该字符串未被识别为有效的uri。参数名称 inputuri。有人可以告诉我有什么问题吗

SvcRefstore.ServiceSoapClient storeclient = new SvcRefstore.ServiceSoapClient();
       storeclient.getProductsAllCompleted += new EventHandler<SvcRefstore.getProductsAllCompletedEventArgs>(store_getProducts);
       storeclient.getProductsAllAsync();
       storeclient.setItemsarrayAsync(itemarray);

store_getProducts

 void store_getProducts(object sender, SvcRefstore.getProductsAllCompletedEventArgs e)
    {

        XDocument xReturn = XDocument.Load(e.Result);
         IEnumerable<Products> myQuote = from item in xReturn.Descendants("Products")
                                         select new Products
                                         {

                                             Name = Convert.ToString(item.Element("Name").Value),
                                             unitPrice = Convert.ToString(item.Element("unitPrice").Value),
                                         };
         Products thisQuote = myQuote.ElementAt(0);
         textBlock1.Text = thisQuote.Name.ToString();
    }

下面是我创建消费的 getProducts 方法

 public string getProductsAll()
{
    storeDBCon dbConn;
    dbConn = new storeDBCon();

    DataSet idataset = new DataSet();
    string products = "";

    SqlConnection db = dbConn.GetConnection();

    SqlCommand cmd;
    string sqlRetrieve = "SELECT * FROM Product WHERE Name = @Name";

    db.Open();
    try
    {
        for (int i = 0; i < itemsarray.Length; i ++ )

         //   foreach (String item in itemsarray)
            {
                cmd = new SqlCommand(sqlRetrieve, db);

                cmd.Parameters.AddWithValue("@Name", itemsarray[i]);

                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows == true)
                {
                    reader.Read();
                    //_userId = reader.GetInt32(0);
                    productID = reader.GetInt32(0);
                    pname = reader.GetString(1);
                    pDesc = reader.GetString(2);
                    unitPrice = reader.GetString(3);
                    imagefilename = reader.GetString(4);

                    products = productID + "," +pname + "," + pDesc + "," + unitPrice + "," + imagefilename + "";
                }
                else

                    products = "Null";

            }


    }
    catch (Exception ex)
    {
        errMsg = ex.Message;
    }
    finally
    {
        db.Close();

    }


    return products;

}

下面是 setItemsarray 方法

public void setItemsarray(string[] _str)
    {
        itemsarray = _str;
    }
4

2 回答 2

1

这里有几个问题,首先,如果你想将一个字符串加载到 XDocument 中,你需要调用XDocument doc = XDocument.Parse(string)not XDocument.Load()

其次,您的getProductsAll()Web 服务方法只是构建一个逗号分隔的字符串。是什么让您认为可以将其加载到 XML 文档中?您需要从getProductsAll()

于 2013-01-10T16:05:32.307 回答
0

正如我在之前的回答中所解释的,XDocument.Load当您指定加载内容的 url 并XDocument.Parse在字符串中加载 xml 内容时使用。

在您的情况下,您必须像这样重写代码:

void store_getProducts(object sender, SvcRefstore.getProductsAllCompletedEventArgs e)
{

    XDocument xReturn = XDocument.Parse(e.Result); // Change is here
     IEnumerable<Products> myQuote = from item in xReturn.Descendants("Products")
                                     select new Products
                                     {

                                         Name = Convert.ToString(item.Element("Name").Value),
                                         unitPrice = Convert.ToString(item.Element("unitPrice").Value),
                                     };
     Products thisQuote = myQuote.ElementAt(0);
     textBlock1.Text = thisQuote.Name.ToString();
}

[编辑]:经过一番挖掘,我可以看到您的服务器代码不返回 XML,而是一个简单的字符串连接值......稍后您还会遇到其他错误。

于 2013-01-10T16:05:07.807 回答