比如:
<membership defaultProvider="AspNetSqlProvider">
<providers>
<add connectionStringName="AppConnectionString" applicationName="/" description="" minRequiredPasswordLength="3" minRequiredNonalphanumericCharacters="0" requiresUniqueEmail="false" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" passwordFormat="Hashed" name="AspNetSqlProvider" type="System.Web.Security.SqlMembershipProvider" />
</providers>如何读取applicationName,description,minRequiredPasswordLength等值的集合?
<membership defaultProvider="AspNetSqlProvider">
<providers>
<add connectionStringName="AppConnectionString" applicationName="/" description="" minRequiredPasswordLength="3" minRequiredNonalphanumericCharacters="0" requiresUniqueEmail="false" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" passwordFormat="Hashed" name="AspNetSqlProvider" type="System.Web.Security.SqlMembershipProvider" />
</providers>如何读取applicationName,description,minRequiredPasswordLength等值的集合?
{
XmlTextReader xmlReader;
XmlValidatingReader xmlDoc = null;
int nElements, gnAttributes, nComments, nPIs, nCDATAs;
String sWorkingText;
nElements = 0;
gnAttributes = 0;
nComments = 0;
nPIs = 0;
nCDATAs = 0;
sWorkingText = "";
resultsArea.InnerText = "";
try
{
xmlReader = new XmlTextReader(fileSelector.PostedFile.InputStream);
xmlReader.WhitespaceHandling = WhitespaceHandling.None;
xmlDoc = new XmlValidatingReader(xmlReader);
xmlDoc.ValidationType = ValidationType.Auto;
XmlUrlResolver resolver = new XmlUrlResolver();
xmlDoc.XmlResolver = resolver;
xmlDoc.ValidationEventHandler += new ValidationEventHandler(this.ValidationEvtCallback);
xmlDoc.Schemas.Add((XmlSchemaCollection)Session["cache"]);
while(xmlDoc.Read())
{
switch (xmlDoc.NodeType)
{
case XmlNodeType.Element:
nElements++;
resultsArea.InnerText += "Element: " + xmlDoc.Name + " has " + xmlDoc.AttributeCount + " attributes\r\n";
gnAttributes += xmlDoc.AttributeCount;
// process the attributes
while (xmlDoc.MoveToNextAttribute())
{
resultsArea.InnerText += "\t" + xmlDoc.Name + "= " + xmlDoc.Value + "\r\n";
}
break;
case XmlNodeType.Text:
sWorkingText = "Text: " + xmlDoc.Value + "\r\n";
resultsArea.InnerText += sWorkingText;
break;
case XmlNodeType.DocumentType:
resultsArea.InnerText += "Document has a DOCTYPE\r\n";
break;
case XmlNodeType.Comment:
nComments++;
resultsArea.InnerText += "Comment: " + xmlDoc.Value + "\r\n";
break;
case XmlNodeType.ProcessingInstruction:
nPIs++;
break;
case XmlNodeType.CDATA:
nCDATAs++;
break;
}
}
sWorkingText = "\r\nTotal number of elements: " + nElements + "\r\n";
sWorkingText += "Total number of attributes in document: " + gnAttributes + "\r\n";
sWorkingText += "Total comments: " + nComments + "\r\n";
sWorkingText += "Total PIs: " + nPIs + "\r\n";
sWorkingText += "Total CDATA sections: " + nCDATAs;
resultsArea.InnerText += sWorkingText;
}
catch (XmlException exc)
{
sWorkingText = "Exception while parsing:\r\n";
sWorkingText += "Line number: " + exc.LineNumber + "\r\n";
sWorkingText += "Line position: " + exc.LinePosition + "\r\n";
sWorkingText += "Message: " + exc.Message + "\r\n\r\n";
sWorkingText += "Stack Trace:\r\n" + exc.StackTrace;
resultsArea.InnerText = sWorkingText;
} finally
{
if (xmlDoc != null)
xmlDoc.Close();
}
}
///节点遍历
while(xmlDoc.Read())
{
///节点的类型。xmlnode 类型就只有六类,元素,文本,文档类型,注释,处理指示,CDATA。
switch (xmlDoc.NodeType)
{
///是元素节点
case XmlNodeType.Element:nElements++;resultsArea.InnerText += "Element: " + xmlDoc.Name + " has " + xmlDoc.AttributeCount + " attributes\r\n";///获取节点属性数目
gnAttributes += xmlDoc.AttributeCount;
// 读取属性while (xmlDoc.MoveToNextAttribute())
{
resultsArea.InnerText += "\t" + xmlDoc.Name + "= " + xmlDoc.Value + "\r\n";
}
///终止case
break;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Configuration;#endregionnamespace Samples.Aspnet.SystemWebConfiguration
{
class UsingMembershipSection
{
static void Main(string[] args)
{
try
{
// Set the path of the config file.
string configPath = ""; // Get the Web application configuration object.
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath); // Get the section related object.
MembershipSection configSection =
(MembershipSection)config.GetSection("system.web/membership"); // Display title and info.
Console.WriteLine("ASP.NET Configuration Info");
Console.WriteLine(); // Display Config details.
Console.WriteLine("File Path: {0}",
config.FilePath);
Console.WriteLine("Section Path: {0}",
configSection.SectionInformation.Name); // Display Default Provider.
Console.WriteLine("DefaultProvider: {0}",
configSection.DefaultProvider); // Set Default Provider.
configSection.DefaultProvider =
"AspNetSqlRoleProvider"; // Display HashAlgorithmType value.
Console.WriteLine("HashAlgorithmType: {0}",
configSection.HashAlgorithmType); // Set HashAlgorithmType value.
configSection.HashAlgorithmType =
MachineKeyValidation.SHA1.ToString(); // Display UserIsOnlineTimeWindow value.
Console.WriteLine("UserIsOnlineTimeWindow: {0}",
configSection.UserIsOnlineTimeWindow); // Set UserIsOnlineTimeWindow value.
configSection.UserIsOnlineTimeWindow =
TimeSpan.FromMinutes(15) ; // Display the number of Providers.
Console.WriteLine("Providers Collection Count: {0}",
configSection.Providers.Count); // Display elements of the Providers collection property.
foreach (ProviderSettings providerItem in configSection.Providers)
{
Console.WriteLine();
Console.WriteLine("Provider Details:");
Console.WriteLine("Name: {0}", providerItem.Name);
Console.WriteLine("Type: {0}", providerItem.Type);
} // Update if not locked.
if (!configSection.SectionInformation.IsLocked)
{
config.Save();
Console.WriteLine("** Configuration updated.");
}
else
{
Console.WriteLine("** Could not update, section is locked.");
}
} catch (Exception e)
{
// Unknown error.
Console.WriteLine(e.ToString());
} // Display and wait.
Console.ReadLine();
}
}
}
Membership会自己处理这些变配置属性。
Membership中都有相应的属性可以读出这些值