凯锐

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  96 随笔 :: 39 文章 :: 208 评论 :: 22 Trackbacks
        這兩天﹐對xml作為數據庫產生了興趣﹐找了一些資料﹐也搞出了一點眉目﹐在這里記錄一下。算是對自己學習xml的一個小結吧。技朮內容不是很強﹐高手大俠們就不需看了。呵呵....
       不多說廢話﹐咱們程序員最注重的是實用性﹐以下就將本人自己產生xml數據庫﹐然后再登錄驗証的全過程共享出來。
       首先﹐請建立一個windows專案,然后從工具箱中拖兩個TextBox﹐ID分別為UserName 和UserPwd,然后再拖兩個Button出來﹐ID分別為btnOK和btnGen.Text屬性分別設為"驗証"和"建立"。
       然后在btnGen的click事件中加入如下代碼﹐產生一個xml文件﹐作為數據庫﹕
            XmlDocument xd = new XmlDocument();
            XmlNode xnDec 
= xd.CreateNode(XmlNodeType.XmlDeclaration, """");
            XmlElement xeRoot 
= xd.CreateElement("Users");
            xd.AppendChild(xnDec);
            xd.AppendChild(xeRoot);

            XmlElement xe1 
= xd.CreateElement("Users");
            XmlElement xe1Name 
= xd.CreateElement("UserName");
            XmlElement xe1Pass 
= xd.CreateElement("UserPassword");
            xe1Name.InnerText 
= "Jack";
            xe1Pass.InnerText 
= "123";
            xeRoot.AppendChild(xe1);
            xe1.AppendChild(xe1Name);
            xe1.AppendChild(xe1Pass);

            XmlElement xe2 
= xd.CreateElement("Users");
            XmlElement xe2Name 
= xd.CreateElement("UserName");
            XmlElement xe2Pass 
= xd.CreateElement("UserPassword");
            xe2Name.InnerText 
= "King";
            xe2Pass.InnerText 
= "123";
            xeRoot.AppendChild(xe2);
            xe2.AppendChild(xe2Name);
            xe2.AppendChild(xe2Pass);

            xd.Save(Application.StartupPath 
+ "\\Users.xml");
       接著在btnOK的click事件中輸入如下代碼﹐作為驗証段﹐當然﹐我并沒有對xml文件中的相關敏感信息加密﹐畢竟只算是一個小的學習總結吧。
            DataSet ds = new DataSet();
            ds.ReadXml(Application.StartupPath 
+ "\\Users.xml");
            
//DataView dv = new DataView();
            
//dv = ds.Tables[0].DefaultView;
            
//dv.Sort = "UserName";
            
//dv.RowFilter = "UserName ='" + UserName.Text.Trim() + "'";
            DataTable dt = ds.Tables[0];
            DataRow[] dta 
= dt.Select("UserName='" + UserName.Text.Trim() + "'");

            
//this.dataGridView1.DataSource = dv;
            if (dta != null && dta.Length > 0 )
            {
                DataRow dr 
= dta[0];
                
string strPwd = (string)dr["UserPassword"];
                
if (strPwd == this.UserPwd.Text.Trim())
                {
                    MessageBox.Show(
"OK");
                }
                
else
                {
                    MessageBox.Show(
"No OK");
                }
            }
            
else
            {
                MessageBox.Show(
"No this account");
            }


posted on 2007-01-08 13:35 凯锐 阅读(917) 评论(15)  编辑 收藏 所属分类: C# Programing

评论

#1楼 [楼主] 2007-01-08 15:11 精浪      
再轉貼一段C#xml操作類出來。
public class XmlControl
{
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();

public XmlControl(string XmlFile)
{
//
// TODO: 在這裡加入建構函式的程式碼
//
try
{
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}

public DataView GetData(string XmlPathNode)
{
//查找數據。返回一個DataView
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds.Tables[0].DefaultView;
}

public void Replace(string XmlPathNode,string Content)
{
//更新節點內容。
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
}

public void Delete(string Node)
{
//刪除一個節點。
string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}

public void InsertNode(string MainNode,string ChildNode,string Element,string Content)
{
//插入一節點和此節點的一子節點。
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
}

public void InsertElement(string MainNode,string Element,string Attrib,string AttribContent,string Content)
{
//插入一個節點,帶一屬性。
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib,AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}

public void InsertElement(string MainNode,string Element,string Content)
{
//插入一個節點,不帶屬性。
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}

public void Save()
{
//保存文檔。
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
}

=========================================================

实例应用:

string strXmlFile = Server.MapPath("TestXml.xml");
XmlControl xmlTool = new XmlControl(strXmlFile);

// 數據顯視
// dgList.DataSource = xmlTool.GetData("Book/Authors[ISBN=\"0002\"]");
// dgList.DataBind();

// 更新元素內容
// xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp");
// xmlTool.Save();

// 添加一個新節點
// xmlTool.InsertNode("Book","Author","ISBN","0004");
// xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa");
// xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii");
// xmlTool.Save();

// 刪除一個指定節點的所有內容和屬性
// xmlTool.Delete("Book/Author[ISBN=\"0004\"]");
// xmlTool.Save();

// 刪除一個指定節點的子節點
// xmlTool.Delete("Book/Authors[ISBN=\"0003\"]");
// xmlTool.Save();

  回复  引用  查看    


标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
 
另存  打印