在这个Web应用程序中,我想将短信发送到手机安。这是我的aspx.cs文件代码:
protected void buttonSendOnClick(object sender, EventArgs e)
{
//are required fields filled in:
if (textboxRecipient.Text == "")
{
textboxError.Text += "Recipient(s) field must not be empty!\n";
textboxError.Visible = true;
return;
}
//we creating the necessary URL string:
string ozSURL = "http://127.0.0.1"; //where Ozeki NG SMS Gateway is running
string ozSPort = "9501"; //port number where Ozeki NG SMS Gateway is listening
string ozUser = HttpUtility.UrlEncode("admin"); //username for successful login
string ozPassw = HttpUtility.UrlEncode("abc123"); //user's password
string ozMessageType = "SMS:TEXT"; //type of message
string ozRecipients = HttpUtility.UrlEncode( textboxRecipient.Text); //who will
//get the message
string ozMessageData = HttpUtility.UrlEncode(textboxMessage.Text); //body of
//message
string createdURL = ozSURL + ":" + ozSPort + "/httpapi" +
"?action=sendMessage" +
"&username=" + ozUser +
"&password=" + ozPassw +
"&messageType=" + ozMessageType +
"&recipient=" + ozRecipients +
"&messageData=" + ozMessageData;
try
{
//Create the request and send data to Ozeki NG SMS Gateway Server by HTTP
connection
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(createdURL);
//Get response from Ozeki NG SMS Gateway Server and read the answer
HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
System.IO.StreamReader respStreamReader = new
System.IO.StreamReader(myResp.GetResponseStream());
string responseString = respStreamReader.ReadToEnd();
respStreamReader.Close();
myResp.Close();
//inform the user
string result = Regex.Replace(responseString, @"<[^>]*>", string.Empty);
textboxError.Text = Server.HtmlEncode( result);
textboxError.Visible = true;
}
catch (Exception)
{
//if sending request or getting response is not successful Ozeki NG SMS
Gateway Server may do not run
textboxError.Text = "Ozeki NG SMS Gateway Server is not running!";
textboxError.Visible = true;
}
}
运行后,我得到的文字为 xml doc ,就像这样
<Responses>
<Response0>
<Action>sendMessage</Action>
<Data>
<AcceptReport>
<StatusCode>0</StatusCode>
<StatusText>Message accepted for delivery</StatusText>
<MessageID>89c8011c-e291-44c3-ac72-cd35c76cb29d</MessageID>
<Recipient>+85568922903</Recipient>
</AcceptReport>
</Data>
</Response0>
</Responses>
但我想把它作为
接受发送消息 消息ID:IEUHSHIL 收件人:+441234567
那我怎么能这样做呢?
答案 0 :(得分:1)
关于评论中建议的方法之一,使用类似的东西;
XmlDocument doc = new XmlDocument();
doc.LoadXml(load your xml document or string here);
XmlNodeList xnList = doc.SelectNodes("Response0/Data/AcceptReport");
foreach (XmlNode xn in xnList)
{
string status = xn["StatusTest"].InnerText;
string messageID = xn["MessageID"].InnerText;
string recipient = xn["Recipient"].InnerText;
}
string finalString = string.Format("{0} Message ID: {1} Recipient {2}", status, messageID, recipient);
这将根据您加载到其中的文档或字符串创建XML文档。 XmlNodeList允许您基本上选择所需的任何XmlElements,在这种情况下,您使用您请求的格式格式化带有节点信息的字符串
答案 1 :(得分:0)
尝试这样的事情
string stext = @"<Responses>
<Response0>
<Action>sendMessage</Action>
<Data>
<AcceptReport>
<StatusCode>0</StatusCode>
<StatusText>Message accepted for delivery</StatusText>
<MessageID>89c8011c-e291-44c3-ac72-cd35c76cb29d</MessageID>
<Recipient>+85568922903</Recipient>
</AcceptReport>
</Data>
</Response0>
</Responses>";
XElement xm = XElement.Parse(stext);
string sout="";
sout = xm.Descendants("StatusText").First().Value + " Message ID:" + xm.Descendants("MessageID").First().Value + " Recipient:" + xm.Descendants("Recipient").First().Value;
答案 2 :(得分:0)
如何在XPath中使用XmlDocument
类?
客户代码:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(...); // Load from file, stream, etc.
string status = GetDeliveryStatus(xmlDocument);
XML文档处理:
private static string GetDeliveryStatus(XmlDocument xmlDocument)
{
XmlNode reportNode = xmlDocument.SelectSingleNode("/Responses/Response0/Data/AcceptReport");
if (reportNode == null)
throw new ArgumentException("AcceptReport node is absent", xmlDocument);
var messageIDNode = reportNode["MessageID"];
if (messageIDNode == null)
throw new ArgumentException("MessageID node is absent", xmlDocument);
var messageID = messageIDNode.InnerText;
var recipientNode = reportNode["Recipient"];
if (recipientNode == null)
throw new ArgumentException("Recipient node is absent", xmlDocument);
var recipient = recipientNode.InnerText;
var result = string.Format("Message accepted for delivery Message ID: {0} Recipient: {1}", messageID, recipient);
return result;
}
答案 3 :(得分:0)
你得到了一个json结果:
您将其转换为字符串,然后用空格替换括号,这就是为什么你有xml。
重新检查这些行:
//inform the user
string result = Regex.Replace(responseString, @"<[^>]*>", string.Empty);
textboxError.Text = Server.HtmlEncode( result);
检查ResponseString并从中提取所需的数据。
有用的链接: reading HttpwebResponse json response, C# ,how to split json format string in order to Deserialize is into .net object?
答案 4 :(得分:0)
使用XSLT。原因是它可以很容易地将变换存储在文件中。这意味着如果消息格式发生变化,则可以轻松更新转换以应对。
添加类似
的功能public void XslTransformer(string source, string stylesheet, string output)
{
XslTransform xslt = new XslTransform();
xslt.Load(stylesheet);
xslt.Transform(source, output);
}
并调用它,传递XML,并进行如下转换:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
Message accepted for delivery
<table border="0">
<tr>
<td>Message ID:</td>
<td><xsl:value-of select="Responses/Response0/Data/AcceptReport/MessageID"/></td>
<td>Recipient:</td>
<td><xsl:value-of select="Responses/Response0/Data/AcceptReport/Recipient"/></td>
<td>StatusCode:</td>
<td><xsl:value-of select="Responses/Response0/Data/AcceptReport/MessageID"/></td>
</tr>
</table>
</html>
</xsl:template>
</xsl:stylesheet>
根据需要更改此格式。