110,536
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml = "<row><loginname>chentianya</loginname><loginpwd>25d55ad283aa400af464c76d713c07ad</loginpwd><username>%E7%9C%81%E5%8F%91%E6%94%B9%E5%A7%94%E7%94%B5%E5%AD%90%E7%9B%91%E5%AF%9F%E7%94%A8%E6%88%B7</username><email>0</email><mobile> 0 </mobile><parentid>ff80808116e773020116eb07e91a0044</parentid><othertype>0</othertype><usersort>1</usersort></row>";
RowRecord info = RowRecord.CreateFromXMLText(xml);
Console.WriteLine(info);
Console.Read();
}
}
public class RowRecord {
public string LoginName;
public string LoginPassword;
public string UserName;
public string Email;
public string Mobile;
public string ParentId;
public string OtherType;
public string UserSort;
RowRecord() { }
static Regex reg;
public static RowRecord CreateFromXMLText(string xml) {
if (reg == null) {
string pLoginName = GetTagPattern("loginname");
string pLoginPassword = GetTagPattern("loginpwd");
string pUserName = GetTagPattern("username");
string pEmail = GetTagPattern("email");
string pMobile = GetTagPattern("mobile");
string pParentId = GetTagPattern("parentid");
string pOtherType = GetTagPattern("othertype");
string pUserSort = GetTagPattern("usersort");
string pattern = string.Format("(?:{0}|{1}|{2}|{3}|{4}|{5}|{6}|{7})+",
pLoginName,
pLoginPassword,
pUserName,
pEmail,
pMobile,
pParentId,
pOtherType,
pUserSort);
reg = new Regex(pattern,RegexOptions.Compiled);
}
Match m = reg.Match(xml);
if (m.Success) {
RowRecord info = new RowRecord();
info.LoginName = m.Groups["loginname"].Value;
info.LoginPassword = m.Groups["loginpwd"].Value;
info.UserName = m.Groups["username"].Value;
info.Email = m.Groups["email"].Value;
info.Mobile = m.Groups["mobile"].Value;
info.ParentId = m.Groups["parentid"].Value;
info.OtherType = m.Groups["othertype"].Value;
info.UserSort = m.Groups["usersort"].Value;
return info;
}
return null;
}
static string GetTagPattern(string tagName) {
return string.Format("<{0}>(?<{0}>[^<>]*)</{0}>", tagName);
//如果有空格或者换行的话
//return string.Format("<{0}>\s*(?<{0}>[^<>]*)\s*</{0}>", tagName);
}
public override string ToString()
{
return string.Format("LoginName:{0}\nLoginPassword:{1}\nUserName:{2}\nEmail:{3}\nMobile:{4}\nParentId:{5}\nOtherType:{6}\nUserSort:{7}",
LoginName,LoginPassword,UserName,Email,Mobile,ParentId,OtherType,UserSort);
}
}
}