C#操作xml
引用命名空间:using System.Xml
1.检查所要操作的xml文件是否存在:
System.IO.File.Exists(文件路径及名称);
2.得到xml文件:
(1)在asp.net中可以这样得到:
XmlDocument xmlDoc = new XmlDocument();
//导入xml文档
xmlDoc.Load( Server.MapPath("xmlTesting.xml"));
//导入字符串
//xmlDoc.LoadXml("<bookStore> <book id="01" price="3.5元"> 读者</book></bookStore>");
注:Server.MapPath("xmlTesting.xml")此时的xmlTesting.xml文件必须是在当前的解决方案里;同样可以写成完整的物理路径xmlDoc.Load (@"E:"软件学习"测试"myNoteWeb"xmlTesting.xml")
(2)在windForm中 直接用物理路径得到所要操作的xml文件具体实现方法同上
3.创建xml文件:
XmlDocument xmlDoc = new XmlDocument(); //创建xml文档(实例化一个xml)
XmlNode root = xmlDoc.CreateElement("bookStore");//创建根节点
//创建第1个子结点:
XmlNode bookNode = xmlDoc.CreateElement("book");
bookNode.InnerText = "读者";
//为此节点添加属性
法1:
bookPublishNode.SetAttribute("id", "01")
root.AppendChild(bookNode);
法2:
XmlAttribute xmlattribute = tempXmlDoc.CreateAttribute("price");
xmlattribute.Value = "3.5元";
tempRoot .Attributes .Append (xmlattribute )
//创建第2个根节点的子结点:
XmlNode tempBookNode = xmlDoc.CreateElement("tempbook ");
tempBookNode.InnerText ="文摘";
root.AppendChild(tempBookNode);
相关文档:
在 ASP.NET 中可以非常方便地执行 MD5
或 SHA1 加密。
<%@ Import Namespace="System.Web.Security" %>
FormsAuthentication.HashPasswordForStoringInConfigFile
只需要两步,第一步引入名称空间
(该名称空间也可以省略引用),第二步执行加密函数。
FormsAuthentication.HashPasswordForStoringI ......
Rijndael
属对称加密,对称加密在加密和解密时都使用相同的密钥。2000 年 10 月,NIST 选择 Rijndael(发音为 "Rhine dale")作为 AES 算法,用以取代 DES。
Rijndael 的名称空间是:
System.Security.Cryptography
byte[] plaintextBuffer = System.Text.Encoding.UTF8.GetBytes("明文");
// ......
要安全地存储密钥,应将密钥存放在密钥容器中,而不是明文存放在文件中。
如果您不了解密钥容器,可以参照 MSDN 上的 了解计算机级别和用户级别的 RSA 密钥容器
。
CspParameters
的名称空间是:
System.Security.Cryptography
创建和读取密钥容器
CspParameters cp = new CspParameters();
cp.KeyContainerName = ......
在 System.Security.Cryptography 中,我们可以看到有许多类,有些类还很相似,比如:
System.Security.Cryptography.SHA1
System.Security.Cryptography.SHA1Managed
System.Security.Cryptography.SHA1CryptoServiceProvider
这三个类有什么关系呢?
SHA1
是抽象类
,SHA1Managed
和 SHA1CryptoServiceProvider ......
#region 得到所有本地网络中可使用的SQL服务器列表
/// <summary>
/// 得到所有本地网络中可使用的SQL服务器列表
/// </summary>
/// <param name="p_strServerList">服务器列表</param& ......