<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MySection111" type="RwConfigDemo.MySection1, RwConfigDemo" />
<section name="MySection222" type="RwConfigDemo.MySection2, RwConfigDemo" />
<section name="MySection333" type="RwConfigDemo.MySection3, RwConfigDemo" />
<section name="MySection444" type="RwConfigDemo.MySection4, RwConfigDemo" />
</configSections>
<MySection111 username="fish-li" url="http://www.1sucai.cn/"></MySection111>
<MySection222>
<users username="fish" password="liqifeng"></users>
</MySection222>
<MySection444>
<add key="aa" value="11111"></add>
<add key="bb" value="22222"></add>
<add key="cc" value="33333"></add>
</MySection444>
<MySection333>
<Command1>
<![CDATA[
create procedure ChangeProductQuantity(
@ProductID int,
@Quantity int
)
as
update Products set Quantity = @Quantity
where ProductID = @ProductID;
]]>
</Command1>
<Command2>
<![CDATA[
create procedure DeleteCategory(
@CategoryID int
)
as
delete from Categories
where CategoryID = @CategoryID;
]]>
</Command2>
</MySection333>
</configuration>
<MySection111 username="fish-li" url="http://www.1sucai.cn/"></MySection111>
public class MySection1 : ConfigurationSection
{
[ConfigurationProperty("username", IsRequired = true)]
public string UserName
{
get { return this["username"].ToString(); }
set { this["username"] = value; }
}
[ConfigurationProperty("url", IsRequired = true)]
public string Url
{
get { return this["url"].ToString(); }
set { this["url"] = value; }
}
}
<MySection222> <users username="fish" password="liqifeng"></users> </MySection222>
public class MySection2 : ConfigurationSection
{
[ConfigurationProperty("users", IsRequired = true)]
public MySectionElement Users
{
get { return (MySectionElement)this["users"]; }
}
}
public class MySectionElement : ConfigurationElement
{
[ConfigurationProperty("username", IsRequired = true)]
public string UserName
{
get { return this["username"].ToString(); }
set { this["username"] = value; }
}
[ConfigurationProperty("password", IsRequired = true)]
public string Password
{
get { return this["password"].ToString(); }
set { this["password"] = value; }
}
}
<MySection333>
<Command1>
<![CDATA[
create procedure ChangeProductQuantity(
@ProductID int,
@Quantity int
)
as
update Products set Quantity = @Quantity
where ProductID = @ProductID;
]]>
</Command1>
<Command2>
<![CDATA[
create procedure DeleteCategory(
@CategoryID int
)
as
delete from Categories
where CategoryID = @CategoryID;
]]>
</Command2>
</MySection333>
public class MySection3 : ConfigurationSection
{
[ConfigurationProperty("Command1", IsRequired = true)]
public MyTextElement Command1
{
get { return (MyTextElement)this["Command1"]; }
}
[ConfigurationProperty("Command2", IsRequired = true)]
public MyTextElement Command2
{
get { return (MyTextElement)this["Command2"]; }
}
}
public class MyTextElement : ConfigurationElement
{
protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
{
CommandText = reader.ReadElementContentAs(typeof(string), null) as string;
}
protected override bool SerializeElement(System.Xml.XmlWriter writer, bool serializeCollectionKey)
{
if( writer != null )
writer.WriteCData(CommandText);
return true;
}
[ConfigurationProperty("data", IsRequired = false)]
public string CommandText
{
get { return this["data"].ToString(); }
set { this["data"] = value; }
}
}
<MySection444> <add key="aa" value="11111"></add> <add key="bb" value="22222"></add> <add key="cc" value="33333"></add> </MySection444>
MySection1 mySectioin1 = (MySection1)ConfigurationManager.GetSection("MySection111");
txtUsername1.Text = mySectioin1.UserName;
txtUrl1.Text = mySectioin1.Url;
MySection2 mySectioin2 = (MySection2)ConfigurationManager.GetSection("MySection222");
txtUsername2.Text = mySectioin2.Users.UserName;
txtUrl2.Text = mySectioin2.Users.Password;
MySection3 mySection3 = (MySection3)ConfigurationManager.GetSection("MySection333");
txtCommand1.Text = mySection3.Command1.CommandText.Trim();
txtCommand2.Text = mySection3.Command2.CommandText.Trim();
MySection4 mySection4 = (MySection4)ConfigurationManager.GetSection("MySection444");
txtKeyValues.Text = string.Join("\r\n",
(from kv in mySection4.KeyValues.Cast<MyKeyValueSetting>()
let s = string.Format("{0}={1}", kv.Key, kv.Value)
select s).ToArray());
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
MySection1 mySectioin1 = config.GetSection("MySection111") as MySection1;
mySectioin1.UserName = txtUsername1.Text.Trim();
mySectioin1.Url = txtUrl1.Text.Trim();
MySection2 mySection2 = config.GetSection("MySection222") as MySection2;
mySection2.Users.UserName = txtUsername2.Text.Trim();
mySection2.Users.Password = txtUrl2.Text.Trim();
MySection3 mySection3 = config.GetSection("MySection333") as MySection3;
mySection3.Command1.CommandText = txtCommand1.Text.Trim();
mySection3.Command2.CommandText = txtCommand2.Text.Trim();
MySection4 mySection4 = config.GetSection("MySection444") as MySection4;
mySection4.KeyValues.Clear();
(from s in txtKeyValues.Lines
let p = s.IndexOf('=')
where p > 0
select new MyKeyValueSetting { Key = s.Substring(0, p), Value = s.Substring(p + 1) }
).ToList()
.ForEach(kv => mySection4.KeyValues.Add(kv));
config.Save();
<system.net>
<mailSettings>
<smtp from="Fish.Q.Li@newegg.com">
<network />
</smtp>
</mailSettings>
</system.net>
SmtpSection section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
labMailFrom.Text = "Mail From: " + section.From;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
SmtpSection section = config.GetSection("system.net/mailSettings/smtp") as SmtpSection;
section.From = "Fish.Q.Li@newegg.com2";
config.Save();
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMyCommand xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyCommand Name="InsretCustomer" Database="MyTestDb">
<Parameters>
<Parameter Name="Name" Type="DbType.String" />
<Parameter Name="Address" Type="DbType.String" />
</Parameters>
<CommandText>insret into .....</CommandText>
</MyCommand>
</ArrayOfMyCommand>
public class MyCommand
{
[XmlAttribute("Name")]
public string CommandName;
[XmlAttribute]
public string Database;
[XmlArrayItem("Parameter")]
public List<MyCommandParameter> Parameters = new List<MyCommandParameter>();
[XmlElement]
public string CommandText;
}
public class MyCommandParameter
{
[XmlAttribute("Name")]
public string ParamName;
[XmlAttribute("Type")]
public string ParamType;
}
private void btnReadXml_Click(object sender, EventArgs e)
{
btnWriteXml_Click(null, null);
List<MyCommand> list = XmlHelper.XmlDeserializeFromFile<List<MyCommand>>(XmlFileName, Encoding.UTF8);
if( list.Count > 0 )
MessageBox.Show(list[0].CommandName + ": " + list[0].CommandText,
this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
private void btnWriteXml_Click(object sender, EventArgs e)
{
MyCommand command = new MyCommand();
command.CommandName = "InsretCustomer";
command.Database = "MyTestDb";
command.CommandText = "insret into .....";
command.Parameters.Add(new MyCommandParameter { ParamName = "Name", ParamType = "DbType.String" });
command.Parameters.Add(new MyCommandParameter { ParamName = "Address", ParamType = "DbType.String" });
List<MyCommand> list = new List<MyCommand>(1);
list.Add(command);
XmlHelper.XmlSerializeToFile(list, XmlFileName, Encoding.UTF8);
}
public static class XmlHelper
{
private static void XmlSerializeInternal(Stream stream, object o, Encoding encoding)
{
if( o == null )
throw new ArgumentNullException("o");
if( encoding == null )
throw new ArgumentNullException("encoding");
XmlSerializer serializer = new XmlSerializer(o.GetType());
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars = "\r\n";
settings.Encoding = encoding;
settings.IndentChars = " ";
using( XmlWriter writer = XmlWriter.Create(stream, settings) ) {
serializer.Serialize(writer, o);
writer.Close();
}
}
/// <summary>
/// 将一个对象序列化为XML字符串
/// </summary>
/// <param name="o">要序列化的对象</param>
/// <param name="encoding">编码方式</param>
/// <returns>序列化产生的XML字符串</returns>
public static string XmlSerialize(object o, Encoding encoding)
{
using( MemoryStream stream = new MemoryStream() ) {
XmlSerializeInternal(stream, o, encoding);
stream.Position = 0;
using( StreamReader reader = new StreamReader(stream, encoding) ) {
return reader.ReadToEnd();
}
}
}
/// <summary>
/// 将一个对象按XML序列化的方式写入到一个文件
/// </summary>
/// <param name="o">要序列化的对象</param>
/// <param name="path">保存文件路径</param>
/// <param name="encoding">编码方式</param>
public static void XmlSerializeToFile(object o, string path, Encoding encoding)
{
if( string.IsNullOrEmpty(path) )
throw new ArgumentNullException("path");
using( FileStream file = new FileStream(path, FileMode.Create, FileAccess.Write) ) {
XmlSerializeInternal(file, o, encoding);
}
}
/// <summary>
/// 从XML字符串中反序列化对象
/// </summary>
/// <typeparam name="T">结果对象类型</typeparam>
/// <param name="s">包含对象的XML字符串</param>
/// <param name="encoding">编码方式</param>
/// <returns>反序列化得到的对象</returns>
public static T XmlDeserialize<T>(string s, Encoding encoding)
{
if( string.IsNullOrEmpty(s) )
throw new ArgumentNullException("s");
if( encoding == null )
throw new ArgumentNullException("encoding");
XmlSerializer mySerializer = new XmlSerializer(typeof(T));
using( MemoryStream ms = new MemoryStream(encoding.GetBytes(s)) ) {
using( StreamReader sr = new StreamReader(ms, encoding) ) {
return (T)mySerializer.Deserialize(sr);
}
}
}
/// <summary>
/// 读入一个文件,并按XML的方式反序列化对象。
/// </summary>
/// <typeparam name="T">结果对象类型</typeparam>
/// <param name="path">文件路径</param>
/// <param name="encoding">编码方式</param>
/// <returns>反序列化得到的对象</returns>
public static T XmlDeserializeFromFile<T>(string path, Encoding encoding)
{
if( string.IsNullOrEmpty(path) )
throw new ArgumentNullException("path");
if( encoding == null )
throw new ArgumentNullException("encoding");
string xml = File.ReadAllText(path, encoding);
return XmlDeserialize<T>(xml, encoding);
}
}
<CommandText>insret into .....</CommandText>
public class MyCDATA : IXmlSerializable
{
private string _value;
public MyCDATA() { }
public MyCDATA(string value)
{
this._value = value;
}
public string Value
{
get { return _value; }
}
XmlSchema IXmlSerializable.GetSchema()
{
return null;
}
void IXmlSerializable.ReadXml(XmlReader reader)
{
this._value = reader.ReadElementContentAsString();
}
void IXmlSerializable.WriteXml(XmlWriter writer)
{
writer.WriteCData(this._value);
}
public override string ToString()
{
return this._value;
}
public static implicit operator MyCDATA(string text)
{
return new MyCDATA(text);
}
}
public MyCDATA CommandText;
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMyCommand xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyCommand Name="InsretCustomer" Database="MyTestDb">
<Parameters>
<Parameter Name="Name" Type="DbType.String" />
<Parameter Name="Address" Type="DbType.String" />
</Parameters>
<CommandText><![CDATA[insret into .....]]></CommandText>
</MyCommand>
</ArrayOfMyCommand>
<?xml version="1.0" encoding="utf-8"?>
using( MemoryStream stream = new MemoryStream() ) {
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineChars = "\r\n";
settings.OmitXmlDeclaration = true;
settings.IndentChars = "\t";
XmlWriter writer = XmlWriter.Create(stream, settings);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有