2012-08-09 185 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> 

改变这种格式,只要你喜欢。