DHL: linqToxml 解析xml值
Code
StringBuilder st = new StringBuilder();
st.Append("<req cmd=\"push_ticket\">");
st.Append("<ticket tid=\"30001001\">");
st.Append("<userid>001</userid> ");
st.Append("<caller>13241856009</caller>");
st.Append("<callerid>01</callerid>");
st.Append("<called>13141543311</called>");
st.Append("<calledid>11</calledid>");
st.Append("<priceid>1</priceid>");
st.Append("<feemode>扣用户</feemode>");
st.Append("<calllen>30</calllen>");
st.Append("<starttime>2009-09-11 09:01:01<starttime>");
st.Append("<pid>PingCo</pid>");
st.Append("<cpid>PingCoI</cpid>");
st.Append("</ticket></req>");
string newVariable = url.VirtualPathUrl+ "/Default.aspx";
HttpHelper.Post(newVariable, st.ToString(), Encoding.UTF8);
StringBuilder st = new StringBuilder();
st.Append("<req cmd=\"push_ticket\">");
st.Append("<ticket tid=\"30001001\">");
st.Append("<userid>001</userid> ");
st.Append("<caller>13241856009</caller>");
st.Append("<callerid>01</callerid>");
st.Append("<called>13141543311</called>");
st.Append("<calledid>11</calledid>");
st.Append("<priceid>1</priceid>");
st.Append("<feemode>扣用户</feemode>");
st.Append("<calllen>30</calllen>");
st.Append("<starttime>2009-09-11 09:01:01<starttime>");
st.Append("<pid>PingCo</pid>");
st.Append("<cpid>PingCoI</cpid>");
st.Append("</ticket></req>");
string newVariable = url.VirtualPathUrl+ "/Default.aspx";
HttpHelper.Post(newVariable, st.ToString(), Encoding.UTF8);
Code
/// <summary>
/// post指定内容到指定URL
/// </summary>
/// <param name="url">post的地址</param>
/// <param name="content">post的内容</param>
/// <param name="encode">编码</param>
/// <returns>返回响应的数据</returns>
public static string Post(string url, string content, Encoding encode)
{
try
{
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
var byteArray = encode.GetBytes(content);
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = byteArray.Length;
var dataStream = httpRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//接收返回的字符串
var response = (HttpWebResponse)httpRequest.GetResponse();
var receiveStream = response.GetResponseStream();
var readStream = new StreamReader(receiveStream, encode);
var ret = readStream.ReadToEnd();
readStream.Close();
return ret;
}
catch (Exception exception)
{
ILog log = LogManager.GetLogger("HttpHelper");
//记录错误信息日志
log.Error(string.Format("Post数据出错:{0},PostUrl:{1},PostContent:{2}", exception, url, content));
return "<resp ret=\"0\"></resp>";
}
}
/// <summary>
/// post指定内容到指定URL
/// </summary>
/// <param name="url">post的地址</param>
/// <param name="content">post的内容</param>
/// <param name="encode">编码</param>
/// <returns>返回响应的数据</returns>
public static string Post(string url, string content, Encoding encode)
{
try
{
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "POST";
var byteArray = encode.GetBytes(content);
httpRequest.ContentType = "application/x-www-form-urlencoded";
httpRequest.ContentLength = byteArray.Length;
var dataStream = httpRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//接收返回的字符串
var response = (HttpWebResponse)httpRequest.GetResponse();
var receiveStream = response.GetResponseStream();
var readStream = new StreamReader(receiveStream, encode);
var ret = readStream.ReadToEnd();
readStream.Close();
return ret;
}
catch (Exception exception)
{
ILog log = LogManager.GetLogger("HttpHelper");
//记录错误信息日志
log.Error(string.Format("Post数据出错:{0},PostUrl:{1},PostContent:{2}", exception, url, content));
return "<resp ret=\"0\"></resp>";
}
}
Code
var sr = new StreamReader(Request.InputStream);
string xmlStr = sr.ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
//log.Info(xmlStr);
XElement elemet = XElement.Parse(xmlStr);
log.Info(String.Format("Linq:{0}", elemet.Attribute("tid").Value)); //获取属性
log.Info("Linq2:" + elemet.Element("ticket").Element("caller"));//获取节点值
var sr = new StreamReader(Request.InputStream);
string xmlStr = sr.ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);
//log.Info(xmlStr);
XElement elemet = XElement.Parse(xmlStr);
log.Info(String.Format("Linq:{0}", elemet.Attribute("tid").Value)); //获取属性
log.Info("Linq2:" + elemet.Element("ticket").Element("caller"));//获取节点值