现在的位置: 首页 > 综合 > 正文

xmlDocument是什么?(转)

2013年02月25日 ⁄ 综合 ⁄ 共 8695字 ⁄ 字号 评论关闭

 它的作用是解析xml文档,将其转换为对象,使你可以在程序中对其操作
例如如下的xml文档:
<?xml version="1.0" encoding="utf-8"?>
<userdata createuser="false">
   <dataconnection>
     <server>localhost</server>
     <uid>sa</uid>
     <pwd></pwd>
   </dataconnection>
   <net>
     <name>jiayuan</name>
   </net>
</userdata>

使用xmlDocument对其一些简单操作如下:

读取节点中的一个属性
    XmlDocument doc=new XmlDocument();
    doc.Load("config.xml");
    XmlNode xnuser=doc.SelectSingleNode("userdata");
    string flag=xnuser.Attributes["createuser"].InnerText;

读取节点中的值
    XmlDocument doc=new XmlDocument();
    doc.Load("config.xml");
    XmlNode xnserver = doc.SelectSingleNode("userdata/dataconnection/server");

修改节点的属性
    XmlDocument doc=new XmlDocument();
    doc.Load("config.xml");
    XmlNode xnuser=doc.SelectSingleNode("userdata");
    xnuser.Attributes["createuser"].InnerText="false";
    doc.Save("config.xml");

追加节点
    XmlDocument doc = new XmlDocument();
    XmlTextReader reader = new XmlTextReader("config.xml");
    doc.Load("config.xml");
    XmlElement root = doc.DocumentElement; // 获取根节点
    XmlElement tagMessage = doc.CreateElement("net");
    XmlElement tagText = doc.CreateElement("name");
    tagText.InnerText   = netname;
    tagMessage.AppendChild(tagText);    // 追加到 xml 文本的最后面
    root.AppendChild(tagMessage);
    reader.Close();      // 关闭 XmlTextReader
    doc.Save("config.xml");      // 保存 xml 文件

 

 

XmlElement类拥有许多方法来访问它的“属性”(GetAttribute, SetAttribute, RemoveAttribute, GetAttributeNode, 等等)。

你也可以使用“Attributes”属性来返回一个支持“名字”或者“序号”访问的“XML属性集”。

那么,从XmlElement类的解释来看,我们很容易就知道XmlNode和XmlElement类的区别了:

XmlElement类是只拥有“属性”的一个节点,而XmlNode则是不仅拥有“属性”,还拥有“子节点”的一个节点。

所以,我们在使用它们的时候,如果你需要获取或者设置节点中的innerText或者innerXml,那么你就需要用XmlNode;而如果你需要获取或者设置节点本身的属性(参数)的时候,你就需要用XmlElement,当然,你也可以用(XmlElement)对XmlNode进行转换得到。

 

private XmlDocument xmlDoc;

        //load xml file
        private void LoadXml()
        {
            xmlDoc=new XmlDocument();
            xmlDoc.Load(<strong class="kgb" onmouseover="isShowAds = false;isShowAds2 = false;isShowGg = true;InTextAds_GgLayer="Server";KeyGate_ads.ShowGgAds(this,"Server",event)" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; cursor: hand; color: #0000ff; padding-top: 0px; border-right-width: 0px; text-decoration: underline" onclick="javascript:window.open("http://pagead2.googlesyndication.com/pagead/iclk?sa=l&ai=BVcplKKhsR8CoM5mE7AO5iLjRAZ33gjaBvvjYBMCNtwGgjQYQAhgCIK2F0A0oFDgAUNKVx_j8_____wFgncHWgbgFoAGZgof_A6oBCjIwMDAwMzc1NDGyAQ93d3cua25vd3NreS5jb23IAQHaASJodHRwOi8vd3d3Lmtub3dza3kuY29tLzM0MTMyNi5odG1sqAMB6AMf6APXAg&num=2&adurl=http://www.devexpress.com/fastgrid&client=ca-pub-5186439242807756");GgKwClickStat("Server","www.devexpress.com/fastgrid","afc","2000037541");" onmouseout="isShowGg = false;InTextAds_GgLayer="Server"">Server.MapPath("User.xml"));
        }

        //添加节点
        private void AddElement()
        {
           
            LoadXml();

            XmlNode xmldocSelect=xmlDoc.SelectSingleNode("user");
           
            XmlElement el=xmlDoc.CreateElement("person"); //添加person节点
            el.SetAttribute("name","<strong class="kgb" onmouseover="isShowAds = false;isShowAds2 = false;isShowGg = true;InTextAds_GgLayer="_u98CE_u4E91";KeyGate_ads.ShowGgAds(this,"_u98CE_u4E91",event)" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; cursor: hand; color: #0000ff; padding-top: 0px; border-right-width: 0px; text-decoration: underline" onclick="javascript:window.open("http://pagead2.googlesyndication.com/pagead/iclk?sa=l&ai=BicPaKKhsR8CoM5mE7AO5iLjRAf6jjS_-0brtA8CNtwGg_goQBhgGIK2F0A0oFDgAULWTzt_6_____wFgncHWgbgFqgEKMjAwMDAzNzU0MbIBD3d3dy5rbm93c2t5LmNvbcgBAdoBImh0dHA6Ly93d3cua25vd3NreS5jb20vMzQxMzI2Lmh0bWyAAgGpAhR1x7HkOII-yALe9MwDqAMB6AMf6APXAg&num=6&adurl=http://www.jagpack.com/cn/pro_01.asp%3Fid%3D6&client=ca-pub-5186439242807756");GgKwClickStat("风云","www.jagpack.com","afc","2000037541");" onmouseout="isShowGg = false;InTextAds_GgLayer="_u98CE_u4E91"">风云"); //添加person节点的属性"name"
            el.SetAttribute("sex","女");   //添加person节点的属性 "sex"
            el.SetAttribute("age","25");   //添加person节点的属性 "age"
           
             XmlElement xesub1=xmlDoc.CreateElement("pass"); //添加person节点的里的节点
             xesub1.InnerText="123";//设置文本节点
             el.AppendChild(xesub1);
             XmlElement xesub2=xmlDoc.CreateElement("Address");
             xesub2.InnerText="昆明";//设置文本节点
             el.AppendChild(xesub2);

            xmldocSelect.AppendChild(el);
            xmlDoc.Save(Server.MapPath("user.xml"));

        }

       

        //修改节点
        private void UpdateElement()
        {
            LoadXml();
            XmlNodeList nodeList=xmlDoc.SelectSingleNode("user").ChildNodes;//获取bookstore节点的所有子节点
            foreach(XmlNode xn in nodeList)//遍历所有子节点
            {
                XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
                if(xe.GetAttribute("name")=="风云")//如果name属性值为“风云”
                {
                    xe.SetAttribute("name","发明");

                   
                    //如果下面有子节点在下走
                     XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
                    foreach(XmlNode xn1 in nls)//遍历
                    {
                       XmlElement xe2=(XmlElement)xn1;//转换类型
                        if(xe2.Name=="pass")//如果找到
                        {
                            xe2.InnerText="66666";//则修改
                            break;

                        }
                    }

                    break;
                }
            }
           xmlDoc.Save(Server.MapPath("user.xml"));//<strong class="kgb" onmouseover="isShowAds = false;isShowAds2 = false;isShowGg = true;InTextAds_GgLayer="_u4FDD_u5B58";KeyGate_ads.ShowGgAds(this,"_u4FDD_u5B58",event)" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; cursor: hand; color: #0000ff; padding-top: 0px; border-right-width: 0px; text-decoration: underline" onclick="javascript:window.open("http://pagead2.googlesyndication.com/pagead/iclk?sa=l&ai=Bkn4kKKhsR8CoM5mE7AO5iLjRAd_XnRf3nJWsA8CNtwHwkwkQAxgDIK2F0A0oFDgAUNPW4vT-_____wFgncHWgbgFqgEKMjAwMDAzNzU0MbIBD3d3dy5rbm93c2t5LmNvbcgBAdoBImh0dHA6Ly93d3cua25vd3NreS5jb20vMzQxMzI2Lmh0bWyoAwHoAx_oA9cC&num=3&adurl=http://www.exactmagic.com/products/testmatrix/index.html&client=ca-pub-5186439242807756");GgKwClickStat("保存","www.exactmagic.com","afc","2000037541");" onmouseout="isShowGg = false;InTextAds_GgLayer="_u4FDD_u5B58"">保存
        }

        //删出节点
        private void deleteNode()
        {
           
            LoadXml();
            XmlNodeList xnl=xmlDoc.SelectSingleNode("user").ChildNodes;

            foreach(XmlNode xn in xnl)
            {
                XmlElement xe=(XmlElement)xn;
               
                if(xe.GetAttribute("name")=="发明")
                {
                    //xe.RemoveAttribute("name");//删除name属性
                    xe.RemoveAll();//删除该节点的<strong class="kgb" onmouseover="isShowAds = false;isShowAds2 = false;isShowGg = true;InTextAds_GgLayer="_u5168_u90E8";KeyGate_ads.ShowGgAds(this,"_u5168_u90E8",event)" style="border-top-width: 0px; padding-right: 0px; padding-left: 0px; font-weight: normal; border-left-width: 0px; border-bottom-width: 0px; padding-bottom: 0px; margin: 0px; cursor: hand; color: #0000ff; padding-top: 0px; border-right-width: 0px; text-decoration: underline" onclick="javascript:window.open("http://pagead2.googlesyndication.com/pagead/iclk?sa=l&ai=Bg4jLKKhsR8CoM5mE7AO5iLjRAYmRkzP1ndjeBMCNtwGwvRAQCxgLIK2F0A0oFDgAUKOR2_T4_____wFgncHWgbgFqgEKMjAwMDAzNzU0MbIBD3d3dy5rbm93c2t5LmNvbcgBAdoBImh0dHA6Ly93d3cua25vd3NreS5jb20vMzQxMzI2Lmh0bWyAAgGpAhR1x7HkOII-yAKVnvACqAMB6AMf6APXAg&num=11&adurl=http://www.syruns.com/product/&client=ca-pub-5186439242807756");GgKwClickStat("全部","www.syruns.com","afc","2000037541");" onmouseout="isShowGg = false;InTextAds_GgLayer="_u5168_u90E8"">全部内容

                   
                    break;
                }
               
            }
            xmlDoc.Save(Server.MapPath("user.xml"));//保存
        }

        private void showIt()
        {
            LoadXml();
            XmlNode xn=xmlDoc.SelectSingleNode("user");

            XmlNodeList xnl=xn.ChildNodes;
  
            foreach(XmlNode xnf in xnl)
            {
                XmlElement xe=(XmlElement)xnf;
//                Console.WriteLine(xe.GetAttribute("name"));//显示属性值
//                Console.WriteLine(xe.GetAttribute("sex"));
//
//                XmlNodeList xnf1=xe.ChildNodes;
//                foreach(XmlNode xn2 in xnf1)
//                {
//                    Console.WriteLine(xn2.InnerText);//显示子节点点文本
//                }

            }

        }
   
Xml的样式:
<?xml version="1.0" encoding="gb2312"?>
<user>
<person>
</person>
<person name="风拉" sex="男" age="25">
    <pass>123</pass>
    <Address>大明</Address>
</person>
<person name="风云" sex="女" age="25">
    <pass>123</pass>
    <Address>昆明</Address>
</person>
</user>

 

抱歉!评论已关闭.