2012-08-09 163 views
1

在此Web應用程序中,我想向移動廣告發送短信。這是我的aspx.cs文件的代碼:將xml文件轉換爲文本

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文檔這樣

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

接受留言送貨 信息ID:IEUHSHIL 收信人:+441234567

那麼我該怎麼做?

+2

使用'XDocument'或'XMLDocument' – EaterOfCode 2012-08-09 08:10:09

+0

我該怎麼做?我是新的 – 2012-08-09 08:13:29

+1

89c8011c-e291​​-44c3-ac72-cd35c76cb29d是否轉換爲IEUHSHIL?怎麼樣? – 2012-08-09 08:14:28

回答

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?

+0

哦,我認爲你的答案是正確的我的問題,但如何檢查ResponseString並從中提取所需的數據?我剛從那開始。你能讓我知道嗎?謝謝 – 2012-08-09 08:36:38

+0

你能幫我嗎?我真的很需要你的幫助 – 2012-08-09 08:50:12

+0

你在responseString中得到了什麼?在調試時將其獲取並粘貼到此處。或通過註釋行結果得到它的結果= Regex.Replace(responseString,@「<[^>」*>「,string.Empty);並設置textboxError.Text = responseString; – 2012-08-09 09:08:15

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,在這種情況下,你格式化爲節點信息的字符串,在你要求

+0

這段代碼真的搞砸了tbh請修復錯誤 – EaterOfCode 2012-08-09 08:18:46

+0

秒,我這樣寫意。默認格式 – 2012-08-09 08:20:57

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; 
+0

感謝您的回答。我會同胞。 – 2012-08-09 08:37:37

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; 
} 
+0

謝謝我會嘗試 – 2012-08-09 08:37:05

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> 

改變這種格式,只要你喜歡。