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")); 
    }

posted on 2007-10-19 10:56  LongSky  阅读(142)  评论(0编辑  收藏  举报

导航