using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;namespace ProjectTool
{
/// <summary>
/// xml文件操作类
/// </summary>
class XmlHelper
{
XmlDocument xmlDoc = new XmlDocument();
/// <summary>
/// xml节点
/// </summary>
private XmlNode _XmlNode;
private XmlElement _XmlElement;
/// <summary>
/// 插入节点
/// </summary>
public void InsertElement(string xmlPath, string compressedFilePath, string compressedTargetPath)
{
xmlDoc.Load(xmlPath);//加载xml文件
_XmlNode = xmlDoc.SelectSingleNode("bookstore");
_XmlElement = xmlDoc.CreateElement("path");
_XmlElement.SetAttribute("CompressedFilePath", compressedFilePath);
XmlElement xesub1 = xmlDoc.CreateElement("CompressedTargetPath");
xesub1.InnerText = compressedTargetPath;
_XmlElement.AppendChild(xesub1);
_XmlNode.AppendChild(_XmlElement);
xmlDoc.Save(xmlPath);
}
/// <summary>
/// 获取子节点和子节点文本的值
/// </summary>
/// <param name="CompressedFilePath">子节点的值</param>
/// <param name="compressedTargetPath">子节点文本的值</param>
public void Read(string CompressedFilePath, string compressedTargetPath)
{
xmlDoc.Load(@"D:\Temporary File\bookstore.xml");
XmlNode xn = xmlDoc.SelectSingleNode("bookstore"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
CompressedFilePath = xe.GetAttribute("CompressedFilePath");//显示属性值 XmlNodeList xnf1 = xe.ChildNodes;
foreach (XmlNode xn2 in xnf1)
{
compressedTargetPath = xn2.InnerText;//显示子节点点文本
}
}
}
}
}求改进,这两个方法虽然实现了,但是其通用性太差了
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;namespace ProjectTool
{
/// <summary>
/// xml文件操作类
/// </summary>
class XmlHelper
{
XmlDocument xmlDoc = new XmlDocument();
/// <summary>
/// xml节点
/// </summary>
private XmlNode _XmlNode;
private XmlElement _XmlElement;
/// <summary>
/// 插入节点
/// </summary>
public void InsertElement(string xmlPath, string compressedFilePath, string compressedTargetPath)
{
xmlDoc.Load(xmlPath);//加载xml文件
_XmlNode = xmlDoc.SelectSingleNode("bookstore");
_XmlElement = xmlDoc.CreateElement("path");
_XmlElement.SetAttribute("CompressedFilePath", compressedFilePath);
XmlElement xesub1 = xmlDoc.CreateElement("CompressedTargetPath");
xesub1.InnerText = compressedTargetPath;
_XmlElement.AppendChild(xesub1);
_XmlNode.AppendChild(_XmlElement);
xmlDoc.Save(xmlPath);
}
/// <summary>
/// 获取子节点和子节点文本的值
/// </summary>
/// <param name="CompressedFilePath">子节点的值</param>
/// <param name="compressedTargetPath">子节点文本的值</param>
public void Read(string CompressedFilePath, string compressedTargetPath)
{
xmlDoc.Load(@"D:\Temporary File\bookstore.xml");
XmlNode xn = xmlDoc.SelectSingleNode("bookstore"); XmlNodeList xnl = xn.ChildNodes; foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
CompressedFilePath = xe.GetAttribute("CompressedFilePath");//显示属性值 XmlNodeList xnf1 = xe.ChildNodes;
foreach (XmlNode xn2 in xnf1)
{
compressedTargetPath = xn2.InnerText;//显示子节点点文本
}
}
}
}
}求改进,这两个方法虽然实现了,但是其通用性太差了
{
xmlDoc.Load(xmlPath);//加载xml文件
_XmlNode = xmlDoc.SelectSingleNode("bookstore");
_XmlElement = xmlDoc.CreateElement("path");
_XmlElement.SetAttribute("CompressedFilePath", compressedFilePath);
XmlElement xesub1 = xmlDoc.CreateElement("CompressedTargetPath");
xesub1.InnerText = compressedTargetPath;
_XmlElement.AppendChild(xesub1);
_XmlNode.AppendChild(_XmlElement);
xmlDoc.Save(xmlPath);
}
面向对象的编程思想中不是又一个模块化的说法么,插那都能使,但是我这个方法只能用于本程序,特异性太大了,比如_XmlElement.SetAttribute("CompressedFilePath", compressedFilePath);添加子节点这一句,想要通用,目前我只能想到把它提成参数,但是这样的话,这个方法会有多达6个的参数。不用说,这也通用不了