讀取/修改/刪除 XML文檔的Demo

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace XMLApp
{
    
class clsXML
    
{
        
private static string _fileName;

        
private static int id;
        
private static string name;
        
private static string title;
        
private static string author;

        
private static XmlDocument xmlDoc = null;
        
private static XmlNamespaceManager nsmgr = null;
        
private static XmlNode rootNode = null;

        
public static int ID
        
{
            
get return id; }
            
set { id = value; }
        }


        
public static string Name
        
{
            
get return name; }
            
set { name = value; }
        }


        
public static string Title
        
{
            
get return title; }
            
set { title = value; }
        }


        
public static string Author
        
{
            
get return author; }
            
set { author = value; }
        }


        
public static void OpenXmlFile(string fileName)
        
{
            _fileName 
= fileName;

            
if (xmlDoc == null)
            
{
                xmlDoc 
= new XmlDocument();
                xmlDoc.Load(fileName);                

                
//nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                
//nsmgr.AddNamespace("myXml", "http://www.publishing.com");

                rootNode 
= xmlDoc.DocumentElement; //SelectSingleNode("//myXml:book", nsmgr);
            }

        }


        
public static void LocateXmlRecord(int id)
        
{
            
if (xmlDoc != null)
            
{

                XmlNode node 
= xmlDoc.SelectSingleNode("Company/Department/Books/book[@id='"+id.ToString()+"']");

                
if (node != null)
                
{
                    
//id = (int)node["id"].InnerText;
                    name = node["Name"].InnerText;

                    title 
= node["Title"].InnerText;
                    author 
= node["Author"].InnerText;
                }
                
            }

        }


        
public static void ModifyXmlRecord(int id)
        
{
            
if (xmlDoc != null)
            
{                
                XmlNode node 
= xmlDoc.SelectSingleNode("Company/Department/Books/book[@id='" + id.ToString() + "']");

                
if (node != null)
                
{
                    node[
"Name"].InnerText = "testName";
                    node[
"Title"].InnerText = "testTitle";
                    node[
"Author"].InnerText = "testAuthor";

                    xmlDoc.Save(_fileName);
                }

            }

        }


        
public static void DeleteXmlRecord(int id)
        
{
            
if (xmlDoc != null)
            
{
                XmlNode fNode 
= xmlDoc.SelectSingleNode("Company/Department/Books");
                XmlNode node 
= xmlDoc.SelectSingleNode("Company/Department/Books/book[@id='" + id.ToString() + "']");

                
if (node != null)
                
{
                    fNode.RemoveChild(node);
                    
                }


                xmlDoc.Save(_fileName);

            }

        }



        
//public static void DeleteXmlRecord(int id)
        
//{
        
//    if (xmlDoc != null)
        
//    {
        
//        XmlNode fNode = xmlDoc.SelectSingleNode("Company/Department/Books");
        
//        XmlNode node = xmlDoc.SelectSingleNode("Company/Department/Books/book[@id='" + id.ToString() + "']");

        
//        if (node != null)
        
//        {
        
//            fNode.RemoveChild(node);
        
//        }

        
//        xmlDoc.Save(_fileName);

        
//    }
        
//}

    }

}

XML文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<Company>
  
<Department id="101">
    
<Department_Name>Cai WuBu</Department_Name>
    
<Manager>Zhang Bin</Manager>
    
<Books>
      
<book id="1">
        
<Name>testName1</Name>
        
<Title>testTitle1</Title>
        
<Author>testAuthor1</Author>
      
</book>
      
<book id="2">
        
<Name>testName</Name>
        
<Title>testTitle</Title>
        
<Author>testAuthor</Author>
      
</book>
      
<book id="3">
        
<Name>book3</Name>
        
<Title>333333</Title>
        
<Author>DinoFung</Author>
      
</book>
    
</Books>
  
</Department>
</Company>