chyel-love'net

本人今年大四,即将毕业,准备从事dotnet程序开发业,
擅长b/s开发,熟悉三层结构,希望有愿意收留本人者,请与我联系。QQ:109267323
posts - 21, comments - 7, trackbacks - 0, articles - 0
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理

公告

XML基本使用

Posted on 2005-09-03 02:39 chyel-love 阅读(...) 评论(...) 编辑 收藏

一、用DataSet操作XML文件
      XML读DataSet:

                DataSet ds=new DataSet();
                
string strXmlPath=Server.MapPath("test.xml");
                ds.ReadXml(strXmlPath);
                
//此时XML文件已经读入到DataSet中了

                DataGrid1.DataSource
=ds.Tables[0].DefaultView;
                DataGrid1.DataBind();


      DataSet写XML:

                string strXmlPath=Server.MapPath("test.xml");
                ds.WriteXml(strXmlPath);
                 
//这时把DataSet里存储的内容写到xml中


二、读取XML结点
      1、使用XmlNodeReader
           

  XmlDocument xDoc=new XmlDocument();
             xDoc.Load(Server.MapPath(
"test.xml"));
             XmlNodeReader xReader
=new XmlNodeReader(xDoc);
              
//XmlNodeReader相当于DataReader
             string str="";
             
while(xReader.Read())
             
{
                     
if(xReader.NodeType==XmlNodeType.Element)
                      
{
                         
switch(xReader.Name)
                            
{
                                       
case "title":
                                                     xReader.Read();
                                                     str
=XReader.Value;
                                                     
break;
                                       
case "title":
                                                     xReader.Read();
                                                     str
=XReader.Value;
                                                     
break;
                                       
default :
                                                     
break;
                               }

                          }

                   }


       2、使用XmlNodeList

                XmlDocument xDoc=new XmlDocument();
                 xDoc.Load(Server.MapPath(
"test.xml"));
                 XmlNodeList xNodeList
=xDoc.GetElementsByTagName("title");
                  Response.Write(xNodeList[
0].InnerXml);



三 生成XML

void CreateXml()
        
{
            xmldoc
=new XmlDocument();
            
//加入XML的声明段落
            xmlnode=xmldoc.CreateNode(XmlNodeType.XmlDeclaration,"","");
            xmldoc.AppendChild(xmlnode);
            
//加入一个根元素
            xmlelem=xmldoc.CreateElement("","Employees","");
            xmldoc.AppendChild(xmlelem);
            
//加入另外一个元素
            for(int i=1;i<3;i++)
            
{
                XmlNode root
=xmldoc.SelectSingleNode("Employees");//查找<Employees>
                XmlElement xe1=xmldoc.CreateElement("Node");//创建一个<node>节点
                xe1.SetAttribute("genre","李红");//设置该节点genre属性
                xe1.SetAttribute("ISBN","2-3432-4");//设置节点isbn属性

                XmlElement xesub1
=xmldoc.CreateElement("title");
                xesub1.InnerText
="软件工程";//设置文本节点
                xe1.AppendChild(xesub1);//添加到<Node>节点
                XmlElement xesub2=xmldoc.CreateElement("author");
                xesub2.InnerText
="张三";
                xe1.AppendChild(xesub2);
                XmlElement xesub3
=xmldoc.CreateElement("price");
                xesub3.InnerText
="56.3";
                xe1.AppendChild(xesub3);

                root.AppendChild(xe1);
//添加到<Employees>节点中
            }

            
//保存创建好的XML文档
            xmldoc.Save(Server.MapPath(".")+"\\files\\data.xml");

        }