xml文件读写

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

<?xml version="1.0" encoding="UTF-8"?>

-<FileItems>

-<FileInfo fileid="12jdkfjdkfkdfjkdfj">

<FileName>test.txt</FileName>

<last_seen>-- :</last_seen>

<pushFileStaus>完成</pushFileStaus>

<ActionType>上传</ActionType>

<logFilePath>C: est.txt</logFilePath>

<SavedSize></SavedSize>

<localFilePath>C: est.txt</localFilePath>

<HostIP>172.19.8.121</HostIP>

<Port></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:\test.txt");
xmlWriter.WriteElementString("SavedSize", "");
xmlWriter.WriteElementString("localFilePath", "C:\test.txt");
xmlWriter.WriteElementString("HostIP", "172.19.8.121");
xmlWriter.WriteElementString("Port", "");
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:\test.txt");
xmlWriter.WriteElementString("SavedSize", "");
xmlWriter.WriteElementString("localFilePath", "C:\test.txt");
xmlWriter.WriteElementString("HostIP", "172.19.8.121");
xmlWriter.WriteElementString("Port", "");
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);
}
}
上一篇:Python中文注释报错的解决方法


下一篇:Vue 项目在其他电脑 npm run dev 运行报错的解决方法