读取XML响应从页 [英] Read XML Response From Page
问题描述
我使用C#和放大器; ASP.net执行张贴到网页。我怎样才能按顺序阅读XML响应知道如果我提交了错误或者是成功的?
这是我已经试过了,但它只会返回一个成功/失败的消息,也不会告诉我实际的XML从返回的页面。
私人无效执行()
{
this.lblResult.Text =的String.Empty;
字典<字符串,字符串> dictFormValues =新词典<字符串,字符串>();
字符串的connectionString = NULL;
SqlConnection的CNN;
CMD的SqlCommand;
StringBuilder的SQL =新的StringBuilder();
SqlDataReader的阅读器;
字符串email =的String.Empty;
的connectionString =数据源=服务器;初始目录=分贝;用户ID =;密码=;
sql.Append(选择TOP 1 maexst);
sql.Append(从redbone.redlight.dbo.maxima); CNN =新的SqlConnection(的connectionString);
尝试
{
cnn.Open();
CMD =新的SqlCommand(sql.ToString(),美国有线电视新闻网)
读者= cmd.ExecuteReader();
而(reader.Read()){dictFormValues.Add(maexst,reader.GetValue(0)的ToString()); }
reader.Close();
cmd.Dispose();
cnn.Close();
}
赶上(例外前){回复于(ex.Message.ToString()); }
字符串strIpAddress = System.Web.HttpContext.Current.Request.UserHostAddress;
字符串strPageTitle = this.Title;
字符串strPageURL = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
字符串STRERROR =;
布尔blnRet = FALSE;
blnRet =邮报(dictFormValues,strPageTitle,strPageURL,楼盘STRERROR);
如果(blnRet ==真)
{
this.lblResult.Text =这是很好的!
}
其他{this.lblResult.Text = STRERROR +:出现错误 }
}公共BOOL blnRet(词典<字符串,字符串> dictFormValues,串strPageTitle,串strPageURL,裁判字符串strMessage)
{
字符串strEndpointURL =的String.Format(HTTP://testtest12test123.aspx); System.Web.Script.Serialization.JavaScriptSerializer JSON =新System.Web.Script.Serialization.JavaScriptSerializer();
字符串strPostData =;
的foreach(在dictFormValues变种D){strPostData + = d.Key +=+ Server.UrlEn code(d.Value)+&放大器;; }
strPostData + =hs_context =;
System.Net.HttpWebRequest R =(System.Net.HttpWebRequest)System.Net.WebRequest.Create(strEndpointURL);
r.Method =POST;
r.Accept =应用/ JSON;
r.ContentType =应用/的X WWW的形式urlen codeD;
r.ContentLength = strPostData.Length;
r.KeepAlive = FALSE;
使用(System.IO.StreamWriter SW =新System.IO.StreamWriter(r.GetRequestStream()))
{
尝试{sw.Write(strPostData); }
赶上(异常前)
{
strMessage = ex.Message;
返回false;
}
}
返回true;
}
修改搜索结果
成功响应搜索
<?XML版本=1.0编码=UTF-8&GT?;
<&结果GT;
<成功> 1 LT; /成功>
<&帖子ID GT; 12345< /&帖子ID GT;
<错误/>
< /&结果GT;
故障响应搜索
<?XML版本=1.0编码=UTF-8&GT?;
<&结果GT;
<成功与GT; 0℃; /成功>
<帖子ID />
<错误>
<错误>错误中列出此处< /错误>
<错误>上市此处&lt错误2; /错误>
<错误>错误3上市此处< /错误>
< /错误>
< /&结果GT;
尝试:
VAR请求= WebRequest.Create(http://some.website/)作为HttpWebRequest的;
变种响应= request.GetResponse();流receiveStream = response.GetResponseStream();
StreamReader的readStream =新的StreamReader(receiveStream,Encoding.UTF8);VAR的结果= readStream.ReadToEnd();
这将会把网页的内容结果
。下一步要看什么反应居然是......从那里,你可以使用:
- XmlDocument.LoadXml
- XDocument.Parse
- XElement.Parse
或许有点像 HTML敏捷性包将让您解析响应。
使用的XElement 示例
使用System.Xml.Linq的;
使用System.Linq的;
使用的System.Xml;VAR XML = System.Xml.Linq.XElement.Parse(结果);
如果(xml.Elements(成功)。FirstOrDefault()。值==1)
{
//处理成功
Console.WriteLine(所有的工作!);
}
其他
{
VAR误差= xml.Elements(错误);
的foreach(在errors.Elements VAR错误(错误))
{
//读取错误信息
Console.WriteLine(error.Value);
}
}
( Runnable的小提琴)
有可能是解析更简单的方式的XElement
,但应该给你的想法。
I am using C# & ASP.net to perform a POST to a webpage. How can I read the XML response in order to know if my submission had errors or was a success?
This is what I have tried, but it will only return a success/fail message, it will not show me the actual xml returned from the page.
private void Perform()
{
this.lblResult.Text = string.Empty;
Dictionary<string, string> dictFormValues = new Dictionary<string, string>();
string connectionString = null;
SqlConnection cnn;
SqlCommand cmd;
StringBuilder sql = new StringBuilder();
SqlDataReader reader;
string email = string.Empty;
connectionString = "Data Source=server;Initial Catalog=db;User ID=;Password=";
sql.Append("select TOP 1 maexst ");
sql.Append("from redbone.redlight.dbo.maxima ");
cnn = new SqlConnection(connectionString);
try
{
cnn.Open();
cmd = new SqlCommand(sql.ToString(), cnn);
reader = cmd.ExecuteReader();
while (reader.Read()) { dictFormValues.Add("maexst", reader.GetValue(0).ToString()); }
reader.Close();
cmd.Dispose();
cnn.Close();
}
catch (Exception ex) { Response.Write(ex.Message.ToString()); }
string strIpAddress = System.Web.HttpContext.Current.Request.UserHostAddress;
string strPageTitle = this.Title;
string strPageURL = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
string strError = "";
bool blnRet = false;
blnRet = Post(dictFormValues, strPageTitle, strPageURL, ref strError);
if (blnRet == true)
{
this.lblResult.Text = "It was good!";
}
else { this.lblResult.Text = strError + ": Error Occured"; }
}
public bool blnRet(Dictionary<string, string> dictFormValues, string strPageTitle, string strPageURL, ref string strMessage)
{
string strEndpointURL = string.Format("http://testtest12test123.aspx");
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
string strPostData = "";
foreach (var d in dictFormValues) { strPostData += d.Key + "=" + Server.UrlEncode(d.Value) + "&"; }
strPostData += "hs_context=";
System.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strEndpointURL);
r.Method = "POST";
r.Accept = "application/json";
r.ContentType = "application/x-www-form-urlencoded";
r.ContentLength = strPostData.Length;
r.KeepAlive = false;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(r.GetRequestStream()))
{
try { sw.Write(strPostData); }
catch (Exception ex)
{
strMessage = ex.Message;
return false;
}
}
return true;
}
EDIT
SUCCESS RESPONSE
<?xml version="1.0" encoding="utf-8" ?>
<result>
<success>1</success>
<postid>12345</postid>
<errors/>
</result>
FAIL RESPONSE
<?xml version="1.0" encoding="utf-8" ?>
<result>
<success>0</success>
<postid/>
<errors>
<error>Error Listed Here</error>
<error>Error 2 Listed Here</error>
<error>Error 3 Listed Here</error>
</errors>
</result>
Try:
var request = WebRequest.Create("http://some.website/") as HttpWebRequest;
var response = request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
var result = readStream.ReadToEnd();
That will put the content of the page in result
What to do next depends on what the response actually is... From there you could use:
- XmlDocument.LoadXml
- XDocument.Parse
- XElement.Parse
Or perhaps something like the HTML Agility Pack will let you parse the response.
Example using XElement
using System.Xml.Linq;
using System.Linq;
using System.Xml;
var xml = System.Xml.Linq.XElement.Parse(result);
if (xml.Elements("success").FirstOrDefault().Value == "1")
{
// Process Success
Console.WriteLine("All Worked!");
}
else
{
var errors = xml.Elements("errors");
foreach (var error in errors.Elements("error"))
{
// read error messages
Console.WriteLine(error.Value);
}
}
There's probably a simpler way of parsing the XElement
, but that should give you the idea.
这篇关于读取XML响应从页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!