XML的读写

 

  /// <summary>
    
/// 从已有的XML文件读取
    
/// </summary>

     private   void  ReadXml()
    
{
        XmlDocument xmlDoc 
= new XmlDocument();
        xmlDoc.Load(Server.MapPath(
"EnterpriseIntroduce.xml"));
        
//XmlNode xmlDocSelect = xmlDoc.SelectSingleNode("Introduce");
        XmlNodeReader nodeRd = new XmlNodeReader(xmlDoc);

        
while (nodeRd.Read())
        
{
            
if (nodeRd.NodeType == XmlNodeType.Element)
            
{
                
switch (nodeRd.Name)
                
{
                    
case "Name":
                        nodeRd.Read();
                        
this.txtName.Text = nodeRd.Value;
                        
break;
                    
case "Webset":
                        nodeRd.Read();
                        
this.txtWebset.Text = nodeRd.Value;
                        
break;
                    
case "Phone":
                        nodeRd.Read();
                        
this.txtPhone.Text = nodeRd.Value;
                        
break;
                    
case "Address":
                        nodeRd.Read();
                        
this.txtAddress.Text = nodeRd.Value;
                        
break;
                    
case "IntroduceText":
                        nodeRd.Read();
                        
this.txtIntroduce.Text = nodeRd.Value;
                        
break;
                    
default:
                        
break;
                }

            }

        }

    }


    
/// <summary>
    
/// 将页面信息存入到指定的XML文件中
    
/// </summary>

     private   void  WriteXmlFromTxt()
    
{
        XmlDocument xmlDoc 
= new XmlDocument();
        xmlDoc.Load(Server.MapPath(
"EnterpriseIntroduce.xml"));
        XmlNode xmlDocSelect 
= xmlDoc.SelectSingleNode("Introduce");
        XmlNodeList nodeList 
= xmlDoc.SelectSingleNode("Introduce").ChildNodes;
        
if (nodeList.Count == 0)
        
{
            XmlElement xmlEl 
= xmlDoc.CreateElement("Name"); //添加Name节点存放公司名称 
            xmlEl.InnerText = this.txtName.Text.ToString().Trim();
            xmlDocSelect.AppendChild(xmlEl);
            xmlEl 
= xmlDoc.CreateElement("Webset"); //添加Name节点存放公司名称 
            xmlEl.InnerText = this.txtWebset.Text.ToString().Trim();
            xmlDocSelect.AppendChild(xmlEl);
            xmlEl 
= xmlDoc.CreateElement("Phone"); //添加Name节点存放公司名称 
            xmlEl.InnerText = this.txtPhone.Text.ToString().Trim();
            xmlDocSelect.AppendChild(xmlEl);
            xmlEl 
= xmlDoc.CreateElement("Address"); //添加Name节点存放公司名称 
            xmlEl.InnerText = this.txtAddress.Text.ToString().Trim();
            xmlDocSelect.AppendChild(xmlEl);
            xmlEl 
= xmlDoc.CreateElement("IntroduceText"); //添加Name节点存放公司名称 
            xmlEl.InnerText = this.txtIntroduce.Text.ToString().Trim();
            xmlDocSelect.AppendChild(xmlEl);
        }

        
else
        
{
            
foreach (XmlNode xn in nodeList)//遍历所有子节点
            {
                XmlElement xeTmp 
= (XmlElement)xn;
                
if (xeTmp.Name == "Name")
                
{
                    xeTmp.InnerText 
= this.txtName.Text.ToString();
                    
continue;
                }

                
if (xeTmp.Name == "Webset")
                
{
                    xeTmp.InnerText 
= this.txtWebset.Text.ToString();
                    
continue;
                }

                
if (xeTmp.Name == "Phone")
                
{
                    xeTmp.InnerText 
= this.txtPhone.Text.ToString();
                    
continue;
                }

                
if (xeTmp.Name == "Address")
                
{
                    xeTmp.InnerText 
= this.txtAddress.Text.ToString();
                    
continue;
                }

                
if (xeTmp.Name == "IntroduceText")
                
{
                    xeTmp.InnerText 
= this.txtIntroduce.Text.ToString();
                    
continue;
                }

            }

        }

        xmlDoc.Save(Server.MapPath(
"EnterpriseIntroduce.xml")); 
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值