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

c#中XML文件的读写操作

2013年08月23日 ⁄ 综合 ⁄ 共 5855字 ⁄ 字号 评论关闭

XML文件内容如下:

 

<?xml version="1.0" encoding="utf-8"?>
<keys>
  
<key>
    
<keyname>2</keyname>
    
<musicsource>CD</musicsource>
    
<musicaddr>2</musicaddr>
    
<playway>播放</playway>
  
</key>
  
<key>
    
<keyname>5</keyname>
    
<musicsource>MP3</musicsource>
    
<musicaddr>7</musicaddr>
    
<playway>停止</playway>
  
</key>
  
<key>
    
<keyname>3</keyname>
    
<musicsource>MP3</musicsource>
    
<musicaddr>8</musicaddr>
    
<playway>播放</playway>
  
</key>
  
<key>
    
<keyname>1</keyname>
    
<musicsource>MP3</musicsource>
    
<musicaddr>1</musicaddr>
    
<playway>连续播放</playway>
  
</key>
  
<key>
    
<keyname>4</keyname>
    
<musicsource>调谐器</musicsource>
    
<musicaddr>1</musicaddr>
    
<playway>循环单曲</playway>
  
</key>
</keys>

 

将XML节点读到LISTVIE上:

 

private void int_6243()                                                     
        
{
            
this.list_6243.Items.Clear();
            
try
            
{
                XmlDocument MyXMLDoc 
= new XmlDocument();
                MyXMLDoc.Load(Application .StartupPath  
+ "/XML_6243.xml");               

                XmlNodeList MyNodes 
= MyXMLDoc.GetElementsByTagName("keyname");
                
foreach (XmlNode MyNode in MyNodes)
                
{
                    
string[] subItem ={ MyNode.InnerText, """""" };
                    
this.list_6243.Items.Add(new ListViewItem(subItem));
                    
                }

               
// this.list_6243.Sorting = SortOrder.Ascending;
                MyNodes = MyXMLDoc.GetElementsByTagName("musicsource");
                
int i = 0;
                
foreach (XmlNode MyNode in MyNodes)
                
{
                    
this.list_6243.Items[i++].SubItems[1].Text = MyNode.InnerText;
                }

                MyNodes 
= MyXMLDoc.GetElementsByTagName("musicaddr");
                i 
= 0;
                
foreach (XmlNode MyNode in MyNodes)
                
{
                    
this.list_6243.Items[i++].SubItems[3].Text = MyNode.InnerText;
                }

                MyNodes 
= MyXMLDoc.GetElementsByTagName("playway");
                i 
= 0;
                
foreach (XmlNode MyNode in MyNodes)
                
{
                    
this.list_6243.Items[i++].SubItems[2].Text = MyNode.InnerText;
                }

                
                
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

            
        }

 

添加新节点到XML:

 

 private void btn6243_Click(object sender, EventArgs e)
        
{
            
if (this.numAddr.Value != 0 && this.comMusic.Text != "" && this.numKey.Value != 0 && this.comPlayWay.Text != "")
            
{
                XmlDocument MyXMLDoc 
= new XmlDocument();
                MyXMLDoc.Load(Application .StartupPath  
+ "/XML_6243.xml");
                
try
                
{
                    
string myfind = this.numKey.Value.ToString();
                    
string myfindstring = "keys/key [keyname=" + '"' + myfind + '"' + "]";
                    XmlNode MyNode 
= MyXMLDoc.SelectSingleNode(myfindstring);
                    
if (MyNode != null)
                    
{
                        MyXMLDoc.DocumentElement.RemoveChild(MyNode);
//检查节点是否已经存在,存在的话则删除
                       
                    }


                    XmlElement MyNewKey 
= MyXMLDoc.CreateElement("key");//新建节点

                    XmlElement MyKeyName 
= MyXMLDoc.CreateElement("keyname");//键码
                    MyKeyName.InnerText = this.numKey.Value.ToString();
                    MyNewKey.AppendChild(MyKeyName);

                    XmlElement MyMusicSource 
= MyXMLDoc.CreateElement("musicsource");//音源
                    MyMusicSource.InnerText = this.comMusic.Text;
                    MyNewKey.AppendChild(MyMusicSource);

                    XmlElement MyPlayWay 
= MyXMLDoc.CreateElement("musicaddr");//音源地址
                    MyPlayWay.InnerText = this.numAddr.Value.ToString();
                    MyNewKey.AppendChild(MyPlayWay);

                    XmlElement MyMusicAddr 
= MyXMLDoc.CreateElement("playway");//播放方式
                    MyMusicAddr.InnerText = this.comPlayWay.Text;
                    MyNewKey.AppendChild(MyMusicAddr);

                    MyXMLDoc.DocumentElement.AppendChild(MyNewKey);

                    
//插入节点
                    XmlTextWriter MyWriter = new XmlTextWriter(Application .StartupPath  + "/XML_6243.xml"null);
                    MyWriter.Formatting 
= Formatting.Indented;
                    MyXMLDoc.WriteContentTo(MyWriter);
                    MyWriter.Close();
                }

                
catch (Exception ex)
                
{
                    MessageBox.Show(ex.Message);
                }

                
this.int_6243();
            }

            
else
                MessageBox.Show(
"信息没有填写完整,请检查!""提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

 

删除XML节点:

 

void menu_Click(object sender, EventArgs e)
        
{
            
//throw new Exception("The method or operation is not implemented.");
            try
            
{
                
string strvalue = this.list_6243.SelectedItems[0].SubItems[0].Text;
                XmlDocument MyXMLDoc 
= new XmlDocument();
                MyXMLDoc.Load(Application .StartupPath  
+ "/XML_6243.xml");
                
string strPath = "keys/key [keyname=" + '"' + strvalue + '"' + "]";
                XmlNode node 
= MyXMLDoc.SelectSingleNode(strPath);
                
if (node != null)
                
{
                    MyXMLDoc.DocumentElement.RemoveChild(node);
                   
/* XmlTextWriter MyWriter = new XmlTextWriter(Application .StartupPath  + "/XML_6243.xml", null);
                    MyWriter.Formatting = Formatting.Indented;
                    MyXMLDoc.WriteContentTo(MyWriter);
                    MyWriter.Close();
*/

                    MyXMLDoc.Save(Application .StartupPath  
+ "/XML_6243.xml");
                }

                MessageBox.Show(
"键码"+strvalue +"删除成功!","

抱歉!评论已关闭.