zoukankan      html  css  js  c++  java
  • xml文件读写

    创建xml文件,对xml文件进行添加新节点、删除节点、更新节点。创建的如下的xml文件。

    <?xml version="1.0" encoding="UTF-8"?>
    
    -<FileItems>
    
    
    -<FileInfo fileid="12jdkfjdkfkdfjkdfj">
    
    <FileName>test.txt</FileName>
    
    <last_seen>2014-06-09 10:30</last_seen>
    
    <pushFileStaus>完成</pushFileStaus>
    
    <ActionType>上传</ActionType>
    
    <logFilePath>C: est.txt</logFilePath>
    
    <SavedSize>1200</SavedSize>
    
    <localFilePath>C: est.txt</localFilePath>
    
    <HostIP>172.19.8.121</HostIP>
    
    <Port>999</Port>
    
    </FileInfo>

    对xml文件进行查找,可以使用using System.Xml.Linq;命名空间中的XElement(XName)类,使用myXElement.Elements().Where(node => node.Attribute("fileid").Value == "12jdkfjdkfkdfjkdfj");其中myXElement是XElement的实例对象、fileid是xml节点属性名,“12jdkfjdkfkdfjkdfj”是属性的值。

    /// <summary>
            /// MainWindow.xaml 的交互逻辑
            /// </summary>
            public partial class MainWindow : Window
            {
                    public MainWindow()
                    {
                            InitializeComponent();
                    }
                    string FullPath = string.Format(@"{0}{1}{2}", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "FilePulla",  "history.xml");
                    /// <summary>
                    /// 创建xml文件
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    private void button1_Click(object sender, RoutedEventArgs e)
                    { 
                           FileInfo xmlFile= new FileInfo(FullPath);
                           XmlTextWriter xmlWriter = new XmlTextWriter(xmlFile.FullName, Encoding.UTF8);
                           xmlWriter.WriteStartDocument();
                           xmlWriter.WriteStartElement("FileItems");
    
                           xmlWriter.WriteStartElement("FileInfo");
                           xmlWriter.WriteAttributeString("fileid", "12jdkfjdkfkdfjkdfj");
                           xmlWriter.WriteElementString("FileName", "test.txt");
                           xmlWriter.WriteElementString("last_seen", "2014-06-09 10:30");
                           xmlWriter.WriteElementString("pushFileStaus", "完成");
                           xmlWriter.WriteElementString("ActionType", "上传");
                           xmlWriter.WriteElementString("logFilePath", "C:	est.txt");
                           xmlWriter.WriteElementString("SavedSize", "1200");
                           xmlWriter.WriteElementString("localFilePath", "C:	est.txt");
                           xmlWriter.WriteElementString("HostIP", "172.19.8.121");
                           xmlWriter.WriteElementString("Port", "999");
                           xmlWriter.WriteEndElement();
    
                            ////////////////////////////////////////////////////////////////////
                           xmlWriter.WriteStartElement("FileInfo");
                           xmlWriter.WriteAttributeString("fileid", "12jdkfjdkfkdfjkdfj");
                           xmlWriter.WriteElementString("FileName", "test.txt");
                           xmlWriter.WriteElementString("last_seen", "2014-06-09 10:30");
                           xmlWriter.WriteElementString("pushFileStaus", "完成");
                           xmlWriter.WriteElementString("ActionType", "上传");
                           xmlWriter.WriteElementString("logFilePath", "C:	est.txt");
                           xmlWriter.WriteElementString("SavedSize", "1200");
                           xmlWriter.WriteElementString("localFilePath", "C:	est.txt");
                           xmlWriter.WriteElementString("HostIP", "172.19.8.121");
                           xmlWriter.WriteElementString("Port", "999");
                           xmlWriter.WriteEndElement();
    
                           xmlWriter.WriteFullEndElement();
                           xmlWriter.WriteEndDocument();
                           xmlWriter.Close();
    
                    }
                    /// <summary>
                    /// 在已存在的文件上,添加新节点
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    private void button2_Click(object sender, RoutedEventArgs e)
                    {
                            if (File.Exists(FullPath))
                            {
                                    XmlDocument xmlDoc = new XmlDocument();
                                    xmlDoc.Load(FullPath);
                                    XmlNode root = xmlDoc.SelectSingleNode("FileItems");
                                    //查找<bookstore>
                                    XmlElement element = xmlDoc.CreateElement("FileInfo");
                                    element.SetAttribute("fileid", "12jdkfjdkfkdfjkdfjere");
    
                                    XmlElement xesub1 = xmlDoc.CreateElement("FileName");
                                    xesub1.InnerText = "test.txt";
                                    element.AppendChild(xesub1);
                                    //设置文本节点
                                    XmlElement xesub10 = xmlDoc.CreateElement("last_seen");
                                    xesub10.InnerText = "2014-06-09 11:35";
                                    element.AppendChild(xesub10);
    
                                    XmlElement xesub2 = xmlDoc.CreateElement("pushFileStaus");
                                    xesub2.InnerText = "test.txt";
                                    element.AppendChild(xesub2);
                                    XmlElement xesub3 = xmlDoc.CreateElement("ActionType");
                                    xesub3.InnerText = "test.txt";
                                    element.AppendChild(xesub3);
                                    XmlElement xesub4 = xmlDoc.CreateElement("logFilePath");
                                    xesub4.InnerText = "test.txt";
                                    element.AppendChild(xesub4);
                                    XmlElement xesub5 = xmlDoc.CreateElement("SavedSize");
                                    xesub5.InnerText = "test.txt";
                                    element.AppendChild(xesub5);
                                    XmlElement xesub6 = xmlDoc.CreateElement("localFilePath");
                                    xesub6.InnerText = "test.txt";
                                    element.AppendChild(xesub6);
                                    XmlElement xesub7 = xmlDoc.CreateElement("HostIP");
                                    xesub7.InnerText = "test.txt";
                                    element.AppendChild(xesub7);
                                    XmlElement xesub8 = xmlDoc.CreateElement("HostIP");
                                    xesub8.InnerText = "test.txt";
                                    element.AppendChild(xesub8);
                                    XmlElement xesub9 = xmlDoc.CreateElement("Port");
                                    xesub9.InnerText = "test.txt";
                                    element.AppendChild(xesub9);
                                    root.AppendChild(element);
                                    xmlDoc.Save(FullPath);
                            }
                          
                    }
                   /// <summary>
                   /// 删除节点
                   /// </summary>
                   /// <param name="sender"></param>
                   /// <param name="e"></param>
                    private void button3_Click(object sender, RoutedEventArgs e)
                    {
                            XElement xmlDoc = XElement.Load(FullPath);
                            XElement selectedElement = xmlDoc.Elements().Where(xl => xl.Attribute("fileid").Value == "12jdkfjdkfkdfjkdfjere").FirstOrDefault();
                            if (selectedElement == null) return;
                            selectedElement.Remove();
                            xmlDoc.Save(FullPath);
                    }
                    /// <summary>
                    /// 修改节点的值
                    /// </summary>
                    /// <param name="sender"></param>
                    /// <param name="e"></param>
                    private void button5_Click(object sender, RoutedEventArgs e)
                    {
                            XElement xmlDoc = XElement.Load(FullPath);
                            XElement selectedElement = xmlDoc.Elements().Where(xl => xl.Attribute("fileid").Value == "12jdkfjdkfkdfjkdfjere").FirstOrDefault();
                            if (selectedElement == null) return;
                            selectedElement.Elements("pushFileStaus").FirstOrDefault().SetValue("完成");
                            xmlDoc.Save(FullPath);
                    }
            }
  • 相关阅读:
    Caused by: java.lang.UnsatisfiedLinkError: Couldn't load 。。。。
    final,finally,finalize
    Java位运算总结:位运算用途广泛《转》
    Ext.Ajax.request同步请求
    xstream 别名的用法<转>
    MySQL 获得当前日期时间(以及时间的转换)
    关于用xercesImpl包解析xml文件遇中文报错问题
    kafka java示例
    varnish、squid、apache、nginx缓存的对比<转>
    websphere性能设置和日常维护
  • 原文地址:https://www.cnblogs.com/goxmpx/p/3777874.html
Copyright © 2011-2022 走看看