我有一个字符串为: <result value= "1000 " message= "用户错误 "/>
请问如何分别取到value 和message的值
谢谢!
------解决方案--------------------------------------------------------
IndexOf( "value ")
然后SubString
------解决方案--------------------------------------------------------
use xmlnode or Regex
------解决方案--------------------------------------------------------
try
string yourStr = ..............;
string value = " ";
string message = " ";
Match m = Regex.Match(yourStr, @ " <result\s+value= " "(? <value> [^ " "]*) " "\s+message= " "(? <message> [^ " "]*) " "[^> ]*> ", RegexOptions.IgnoreCase);
if (m.Success)
{
value = m.Groups[ "value "].Value;
message = m.Groups[ "message "].Value;
}
------解决方案--------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmlDom = new XmlDocument();
xmlDom.LoadXml(CreateXmlText( " <result value=\ "1000\ " message=\ "用户错误\ "/> "));
XmlNodeList xmlNodes = xmlDom.GetElementsByTagName( "result ");
if (xmlNodes != null) {
Response.Write(xmlNodes[0].Attributes[0].Name+ " : "+ xmlNodes[0].Attributes[0].Value);
Response.Write( " <br /> ");
Response.Write(xmlNodes[0].Attributes[1].Name + " : " + xmlNodes[0].Attributes[1].Value);
}
}
private string CreateXmlText(string content) {
StringBuilder builder = new StringBuilder();
builder.Append( " <?xml version=\ "1.0\ " encoding=\ "gb2312\ " ?> ");
builder.Append( " <contents> ");
builder.Append(content);
builder.Append( " </contents> ");
return builder.ToString();
}
}
------解决方案--------------------------------------------------------
楼上3种方法都不错!
------解决方案--------------------------------------------------------
搞点新意。
string str= " <result value=\ "1000\ " message=\ "用户错误\ "/> ";
str=str.Replace( " <result value=\ " ", " ").Replace( "\ " message=\ " ", ", ").Replace( "\ "/> ", " ");
------解决方案--------------------------------------------------------
推荐用
qing2005() 的方法