1

在这个网络应用程序中,我想向移动网络发送一条短信。这是我的 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

那么我该怎么做呢?

4

5 回答 5

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,在这种情况下,您可以按照您请求的格式使用节点信息格式化一个字符串

于 2012-08-09T08:16:43.973 回答
0

XmlDocument将类与 XPath 一起使用怎么样?

客户端代码:

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;
}
于 2012-08-09T08:29:05.470 回答
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;
于 2012-08-09T08:21:48.307 回答
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>

根据需要更改此格式。

于 2012-08-09T10:10:57.863 回答
0

你得到了一个 json 结果:

您将其转换为字符串,然后用空格替换大括号,这就是您得到 xml 的原因。

重新检查这些行:

 //inform the user
            string result = Regex.Replace(responseString, @"<[^>]*>", string.Empty);
            textboxError.Text = Server.HtmlEncode( result);

检查 ResponseString 并从中提取所需的数据。

有用的链接: 阅读 HttpwebResponse json 响应,C#如何拆分 json 格式字符串以便反序列化为 .net 对象?

于 2012-08-09T08:29:26.423 回答