XML学习笔记(三)

             前言

                  学习了XML的基础知识,也查看了XMLDocument类的属性和方法,现在结合网上的一些例子进行强化

           主体

              第一部分   创建XML

              例子1  根据已有的XML文档加载XML             

          XmlDocument xmlDoc  =   new  XmlDocument();
            
// load的起始路径是从根节点开始的,既解决方案所在的目录
          xmlDoc.Load( " XML\\bookstore.xml " );

 

               例子2  根据XML格式的字符串加载XML  

                        

代码
            XmlDocument xmlDoc  =   new  XmlDocument();
            xmlDoc.LoadXml(
@" <?xml version=""1.0"" encoding=""gb2312""?>
                                <bookstore>
                                  <book>
                                    <title>sqlserver2005</title>
                                    <price>25</price>
                                    <bookconcern>123</bookconcern>
                                  </book>  
                                </bookstore>
" );

 

                   第一部分   读写XML

             例子1  添加一个节点              

代码
            XmlDocument xmlDoc  =   new  XmlDocument();
            xmlDoc.Load(
" xml\\bookstore.xml " );
            XmlElement newXmlElement
= xmlDoc.CreateElement( " book " );
            newXmlElement.SetAttribute(
" isNew " " " );
            XmlNode titleNode 
=  xmlDoc.CreateNode(XmlNodeType.Element,  " title " "" );
            titleNode.InnerText 
=   " VS2010 " ;
            newXmlElement.AppendChild(titleNode);

            
// 添加一段纯文本
            
// XmlNode textnode = xmlDoc.CreateNode(XmlNodeType.Text, "文本", "");
            
// textnode.InnerText = "文本测试";
            
// newXmlElement.AppendChild(textnode);

            


            xmlDoc.DocumentElement.AppendChild(newXmlElement);

          例子2  将XML修改后以新文件输出

 

代码
             string  filename  =   " 新的.xml " ;
            Response.ContentEncoding 
=  System.Text.Encoding.Default;
            Response.AppendHeader(
" Content-Disposition " " attachment;filename= "   +  filename);

            Response.Clear();
            
// xmlDoc的内容
            Response.Write(xmlDoc.OuterXml);
            Response.End();

 

             因此接下来对Response进行学习

             

你可能感兴趣的:(学习笔记)