转换xml文件为文本 [英] translate xml file to text

查看:197
本文介绍了转换xml文件为文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此Web应用程序我想发送短信到移动安。这是aspx.cs文件我的code:

In this web application I want to send an sms to a mobile ann. This is my code of aspx.cs file:

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文档像这样

After I run I got text as xml doc like this

<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>

但我想它diplay为

but I want it diplay as

消息被接受交付
消息ID:IEUHSHIL
收件人:441234567

Message accepted for delivery Message ID: IEUHSHIL Recipient: +441234567

所以,我怎么能这样做呢?

So how can I do this?

推荐答案

你有一个JSON结果:

you got a json result :

您将它改成字符串,然后换成括号与空间,这就是为什么你得到的XML。

You converted it into string and then replaced braces with spaces , Thats why you got xml.

重新检查这些行:

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

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

Check ResponseString and extract the required data from it.

有用的链接:
<一href=\"http://stackoverflow.com/questions/5493949/reading-httpwebresponse-json-response-c-sharp\">reading HttpwebResponse JSON响应,C#
,<一个href=\"http://stackoverflow.com/questions/9197963/how-to-split-json-format-string-in-order-to-deserialize-is-into-net-object\">how为了反序列化分割JSON格式字符串到.NET对象?

这篇关于转换xml文件为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆