C# 将xml文件转换为文本
在这个web应用程序中,我想向手机发送短信。这是我的aspx.cs文件代码:C# 将xml文件转换为文本,c#,asp.net,C#,Asp.net,在这个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!
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;
}
}
protectedvoid按钮取消单击(对象发送方,事件参数e)
{
//是否填写了必填字段:
如果(textboxRecipient.Text==“”)
{
textboxError.Text+=“收件人字段不能为空!\n”;
textboxError.Visible=true;
返回;
}
//我们正在创建必要的URL字符串:
字符串ozSURL=”http://127.0.0.1“;//Ozeki NG SMS网关运行的位置
字符串ozSPort=“9501”//Ozeki NG SMS网关正在侦听的端口号
字符串ozUser=HttpUtility.UrlEncode(“admin”);//成功登录的用户名
字符串ozPassw=HttpUtility.UrlEncode(“abc123”);//用户密码
string ozMessageType=“SMS:TEXT”;//消息类型
string ozRecipients=HttpUtility.UrlEncode(textboxRecipient.Text);//谁将
//明白了吗
字符串ozMessageData=HttpUtility.UrlEncode(textboxMessage.Text);//的正文
//信息
字符串createdURL=ozSURL+“:“+ozSPort+”/httpapi”+
“?操作=发送消息”+
“&username=“+ozUser”+
“&password=“+ozPassw+
“&messageType=“+ozMessageType+
“&recipient=“+ozRecipients+
“&messageData=“+ozMessageData;
尝试
{
//创建请求并通过HTTP将数据发送到Ozeki NG SMS网关服务器
连接
HttpWebRequest myReq=(HttpWebRequest)WebRequest.Create(createdURL);
//从Ozeki NG短信网关服务器获取响应并读取答案
HttpWebResponse myResp=(HttpWebResponse)myReq.GetResponse();
System.IO.StreamReader respStreamReader=新建
System.IO.StreamReader(myResp.GetResponseStream());
string responseString=respStreamReader.ReadToEnd();
respStreamReader.Close();
myResp.Close();
//通知用户
string result=Regex.Replace(responseString,@“]*>”,string.Empty);
textboxError.Text=Server.HtmlEncode(结果);
textboxError.Visible=true;
}
捕获(例外)
{
//如果发送请求或获取响应不成功,Ozeki NG SMS
网关服务器可能无法运行
textboxError.Text=“Ozeki NG短信网关服务器未运行!”;
textboxError.Visible=true;
}
}
运行之后,我得到的文本为xmldoc,如下所示
<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>
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
邮件已接受发送
89c8011c-e291-44c3-ac72-cd35c76cb29d
+85568922903
但我想让它像你一样
邮件已接受发送
消息ID:IEUHSHIL
收件人:+441234567
那么我该怎么做呢?关于评论中建议的方法之一,请使用类似的方法
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,在本例中,您可以使用节点信息格式化字符串,格式为您请求的格式。对于注释中建议的方法之一,请使用以下内容:
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,在本例中,您使用节点信息格式化字符串,格式为您请求的格式
<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>
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;
string-stext=@”
发送消息
0
邮件已接受发送
89c8011c-e291-44c3-ac72-cd35c76cb29d
+85568922903
";
XElement xm=XElement.Parse(stext);
字符串sout=“”;
sout=xm.substands(“StatusText”).First().Value+“消息ID:+xm.substands(“MessageID”).First().Value+”收件人:+xm.substands(“收件人”).First().Value;
试试这样的方法
<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>
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;
string-stext=@”
发送消息
0
邮件已接受发送
89c8011c-e291-44c3-ac72-cd35c76cb29d
+85568922903
";
XElement xm=XElement.Parse(stext);
字符串sout=“”;
sout=xm.substands(“StatusText”).First().Value+“消息ID:+xm.substands(“MessageID”).First().Value+”收件人:+xm.substands(“收件人”).First().Value;
如何将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;
}
将
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;
}
您得到了一个json结果: 您将其转换为字符串,然后用空格替换大括号,这就是为什么要使用xml 重新检查这些行:
//inform the user
string result = Regex.Replace(responseString, @"<[^>]*>", string.Empty);
textboxError.Text = Server.HtmlEncode( result);
//通知用户
string result=Regex.Replace(responseString,@“]*>”,string.Empty);
textboxError.Text=Server.HtmlEncode(结果);
检查ResponseString并从中提取所需数据
有用链接:
,您得到了一个json结果: 您将其转换为字符串,然后用空格替换大括号,这就是为什么要使用xml 重新检查这些行:
//inform the user
string result = Regex.Replace(responseString, @"<[^>]*>", string.Empty);
textboxError.Text = Server.HtmlEncode( result);
//通知用户
string result=Regex.Replace(responseString,@“]*>”,string.Empty);
textboxError.Text=Server.HtmlEncode(结果);
检查ResponseString并从中提取所需数据
有用链接:
,使用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>
邮件已接受发送
消息ID: