문제

I may have a simple sesion state tag with self enclosing tag

  1. sessionState mode="StateServer" stateConnectionString="tcpip=CTSINTCOVOCPRD4:42424" />

  2. sessionState mode="StateServer" stateConnectionString="tcpip=CTSINTCOVOCPRD4:42424" /sessionState>

sessionState closing tag may be in the same line or next line. sessionState tag may also have any other setting tags inside it.

I need to find all this scenarios to get full sessionState tag and delete it from config file.

I wrote following lines of code:

                if (Content.Contains("sessionState"))
                {
                    string RegularExpressionPattern = @"<sessionState.*?><\/sessionState>|<sessionState.*?>|<sessionState.*?>\n<\/sessionState>";
                    Regex myRegex = new Regex(RegularExpressionPattern, RegexOptions.None);
                    MatchCollection collection = myRegex.Matches(Content);
                    string stripped = collection[0].Value;
                    Content = Content.Replace(stripped, "");
                    return Content;
                }

Content: config file content

I am getting sessionState tag with self enclosing tag and sessionState with closing tag in the same line, but not getting proper value, if sessionState closing tag is in the next line or if it has any another setting tag inside sessionState tag.

Where am I doing wrong in this code?

도움이 되었습니까?

해결책

Why don't you use :

var sessionStatString = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");

For SessionStateSection : import namespace, using System.Web.Configuration;

UPDATE:

Regex.Replace(Content, "</?(sessionState|SESSIONSTATE).*?>", string.Empty)

Note: I strongly recommend not to update configuration file run time

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top