1

我有一个按钮,我正在尝试执行 a GETthen a POST,我想知道是否有人可以帮助我使用下面的代码,我想要做的是将它设置MessageID为一个字符串,以便我可以将它用于POST uriAddMessagetoGroup. ..

        XDocument xDoc = XDocument.Load(uri1);
        var MessageID = xDoc.Descendants("Message")
            .Select(n => new
            {
                n.Element("MessageID").Value,
            })
            .ToString();

        string uriAddMessagetoGroup = string.Format("http://localhost:8000/Service/AddMessagetoGroup/{0}/{1}/{2}", textBox4.Text, MessageID, textBox21.Text);

完整代码:

 private void button3_Click(object sender, EventArgs e)
        {

            //Get the newly created message while the text is still in the textbox and find its messageID (int)
            string uri1 = "http://localhost:8000/Service/GetMessage/{anything}";
            string tagUri = uri1.Replace("{anything}", textBox21.Text);
            XDocument xDoc = XDocument.Load(tagUri);
            var MessageID = xDoc.Descendants("Message")
                   .Select(n => n.Element("MessageID").Value.ToString());

            //add the string to the uri and add the message to the relevant group while the group still exists in the textbox
            string uriAddMessagetoGroup = string.Format("http://localhost:8000/Service/AddMessagetoGroup/{0}/{1}/{2}", textBox4.Text, MessageID, textBox21.Text);
            byte[] arr2 = Encoding.UTF8.GetBytes(uriAddMessagetoGroup);
            HttpWebRequest req2 = (HttpWebRequest)WebRequest.Create(uriAddMessagetoGroup);
            req2.Method = "POST";
            req2.ContentType = "application/xml";
            req2.ContentLength = arr2.Length;
            Stream reqStrm2 = req2.GetRequestStream();
            reqStrm2.Write(arr2, 0, arr2.Length);
            reqStrm2.Close();
            HttpWebResponse resp2 = (HttpWebResponse)req2.GetResponse();
            MessageBox.Show(resp2.StatusDescription);
            reqStrm2.Close();
            resp2.Close();
4

2 回答 2

0

答案是使用 FirstOrDefault()

于 2012-04-19T21:03:11.807 回答
0

尝试:

 var MessageID = xDoc.Descendants("Message")
        .Select(n =>  n.Element("MessageID").Value.ToString());

 string uriAddMessagetoGroup = string.Format("http://localhost:8000/Service/AddMessagetoGroup/{0}/{1}/{2}", textBox4.Text, MessageID, textBox21.Text);
于 2012-04-19T01:54:55.893 回答