感觉是xml啊,如果用regex处理就比较乱,容易出错,代码不好维护啊。用xml处理吧。
code:
stirng old=....;
XmlDocument doc =new XmlDocument();
doc.LoadXml(old);
//find all parent of node RoleId
//xpath: //RoleID/parent::*
XmlNode nodes()=doc.SelectNodes(@"//RoleID/parent::*");
XmlNode parent;
XmlNode self;
foreach(parent in nodes)
{
//find node what will be replaced
//xpath: ./RoleID
self=parent.SelectSingleNode(@"./RoleID);
if(self != null) parent.Remove(self);
}string newString=doc.OuterXml;
code:
stirng old=....;
XmlDocument doc =new XmlDocument();
doc.LoadXml(old);
//find all parent of node RoleId
//xpath: //RoleID/parent::*
XmlNode nodes()=doc.SelectNodes(@"//RoleID/parent::*");
XmlNode parent;
XmlNode self;
foreach(parent in nodes)
{
//find node what will be replaced
//xpath: ./RoleID
self=parent.SelectSingleNode(@"./RoleID);
if(self != null) parent.Remove(self);
}string newString=doc.OuterXml;
正则表达式为:
pattern=@"<RoleID.+RoleID>"
replaceStatement=""
="<UserInfo><Error></Error><UserID><Value></Value><Error></Error></UserID><UserName><Value></Value><Error></Error></UserName><UserPwd><Value></Value><Error></Error></UserPwd><RoleID><Value></Value><Error></Error></RoleID></UserInfo>";NewString = Regex.Replace(OldString ,@"<RoleID>.*?</RoleID>", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);
有了saucer(思归) 的回复,我已经知道该如何处理了,谢谢saucer(思归) 。