C#读写XML文件
2018-03-21 17:02
459 查看
读写xml文件需要引用using System.Xml;命名空间。
下面给几个函数例子分别读写xml文件。
创建XML文件: //------------【函数:创建xml文件】------------
//filePath为Excel文件路径名
//nodeName根节点名称
//------------------------------------------------------------------------
public static bool CreateNewXML(string filePath,string nodeName)
{
bool result = true;
try
{
//判断文件夹是否存在
string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));
if (mystr1 != "error"&& nodeName!="")
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode header = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(header);
XmlElement xm = xmlDoc.CreateElement(nodeName);
xmlDoc.AppendChild(xm);
xmlDoc.Save(filePath);
}
else
{
result = false;
}
}
catch
{
result = false;
}
return result;
}
向XML文件中添加有个节点: //------------【函数:创建新的节点node】------------
//filePath为Excel文件路径名
//nodeName节点名称
//nodeID节点属性名称
//nodeIDValue节点属性值
//nodeText节点内容
//------------------------------------------------------------------------
public static bool AppendNewNode(string filePath,string nodeName,string nodeID,string nodeIDValue,string nodeText)
{
bool result = true;
try
{
//判断文件夹是否存在
string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));
if (mystr1 != "error" && nodeName != "")
{
XmlDocument xmlDoc = new XmlDocument();//新建XML文件
XmlElement xm = null;
if (!File.Exists(filePath))
{
XmlNode header = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(header);
xm = xmlDoc.CreateElement(nodeName);
xm.SetAttribute(nodeID, nodeIDValue);
xm.InnerText = nodeText;
xmlDoc.AppendChild(xm);
}
else
{
xmlDoc.Load(filePath);//加载XML文件
XmlElement root = xmlDoc.DocumentElement;
xm = xmlDoc.CreateElement(nodeName);
xm.SetAttribute(nodeID, nodeIDValue);
xm.InnerText = nodeText;
root.AppendChild(xm);
}
xmlDoc.Save(filePath);
}
else
{
result = false;
}
}
catch
{
result = false;
}
return result;
}
为XML文件指定节点添加一个子节点: //------------【函数:为节点node创建新的子节点node】------------
//filePath为Excel文件路径名
//nodeName节点名称
//nodeID节点属性名称
//nodeIDValue节点属性值
//nodeText节点内容
//FatherNode父节点
//------------------------------------------------------------------------
public static bool AppendNodeToNode(string filePath, string nodeName, string nodeID, string nodeIDValue, string nodeText,string FatherNode)
{
bool result = true;
try
{
//判断文件是否存在
if (File.Exists(filePath) && nodeName != ""&&FatherNode !="")
{
XmlDocument xmlDoc = new XmlDocument();//新建XML文件
xmlDoc.Load(filePath);//加载XML文件
XmlElement xm = xmlDoc.CreateElement(nodeName);
xm.SetAttribute(nodeID, nodeIDValue);
xm.InnerText = nodeText;
XmlNode root = xmlDoc.GetElementsByTagName(FatherNode)[0];
root.AppendChild(xm);
xmlDoc.Save(filePath);
}
else
{
result = false;
}
}
catch
{
result = false;
}
return result;
}
读取XML文件中指定节点的值: //------------【函数:获取节点node中的值】------------
//filePath为Excel文件路径名
//nodeName节点名称
//------------------------------------------------------------------------
public static string ReadNodeFromXML(string filePath,string nodeName)
{
string result = "error";
try
{
//判断文件是否存在
if (File.Exists(filePath) && nodeName != "")
{
XmlDocument xmlDoc = new XmlDocument();//新建XML文件
xmlDoc.Load(filePath);//加载XML文件
XmlNode xm = xmlDoc.GetElementsByTagName(nodeName)[0];
result = xm.InnerText;
}
else
{
result = "error";
}
}
catch
{
result = "error";
}
return result;
}
下面给几个函数例子分别读写xml文件。
创建XML文件: //------------【函数:创建xml文件】------------
//filePath为Excel文件路径名
//nodeName根节点名称
//------------------------------------------------------------------------
public static bool CreateNewXML(string filePath,string nodeName)
{
bool result = true;
try
{
//判断文件夹是否存在
string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));
if (mystr1 != "error"&& nodeName!="")
{
XmlDocument xmlDoc = new XmlDocument();
XmlNode header = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(header);
XmlElement xm = xmlDoc.CreateElement(nodeName);
xmlDoc.AppendChild(xm);
xmlDoc.Save(filePath);
}
else
{
result = false;
}
}
catch
{
result = false;
}
return result;
}
向XML文件中添加有个节点: //------------【函数:创建新的节点node】------------
//filePath为Excel文件路径名
//nodeName节点名称
//nodeID节点属性名称
//nodeIDValue节点属性值
//nodeText节点内容
//------------------------------------------------------------------------
public static bool AppendNewNode(string filePath,string nodeName,string nodeID,string nodeIDValue,string nodeText)
{
bool result = true;
try
{
//判断文件夹是否存在
string mystr1 = NiceFileProduce.CheckAndCreatPath(NiceFileProduce.DecomposePathAndName(filePath, NiceFileProduce.DecomposePathEnum.PathOnly));
if (mystr1 != "error" && nodeName != "")
{
XmlDocument xmlDoc = new XmlDocument();//新建XML文件
XmlElement xm = null;
if (!File.Exists(filePath))
{
XmlNode header = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(header);
xm = xmlDoc.CreateElement(nodeName);
xm.SetAttribute(nodeID, nodeIDValue);
xm.InnerText = nodeText;
xmlDoc.AppendChild(xm);
}
else
{
xmlDoc.Load(filePath);//加载XML文件
XmlElement root = xmlDoc.DocumentElement;
xm = xmlDoc.CreateElement(nodeName);
xm.SetAttribute(nodeID, nodeIDValue);
xm.InnerText = nodeText;
root.AppendChild(xm);
}
xmlDoc.Save(filePath);
}
else
{
result = false;
}
}
catch
{
result = false;
}
return result;
}
为XML文件指定节点添加一个子节点: //------------【函数:为节点node创建新的子节点node】------------
//filePath为Excel文件路径名
//nodeName节点名称
//nodeID节点属性名称
//nodeIDValue节点属性值
//nodeText节点内容
//FatherNode父节点
//------------------------------------------------------------------------
public static bool AppendNodeToNode(string filePath, string nodeName, string nodeID, string nodeIDValue, string nodeText,string FatherNode)
{
bool result = true;
try
{
//判断文件是否存在
if (File.Exists(filePath) && nodeName != ""&&FatherNode !="")
{
XmlDocument xmlDoc = new XmlDocument();//新建XML文件
xmlDoc.Load(filePath);//加载XML文件
XmlElement xm = xmlDoc.CreateElement(nodeName);
xm.SetAttribute(nodeID, nodeIDValue);
xm.InnerText = nodeText;
XmlNode root = xmlDoc.GetElementsByTagName(FatherNode)[0];
root.AppendChild(xm);
xmlDoc.Save(filePath);
}
else
{
result = false;
}
}
catch
{
result = false;
}
return result;
}
读取XML文件中指定节点的值: //------------【函数:获取节点node中的值】------------
//filePath为Excel文件路径名
//nodeName节点名称
//------------------------------------------------------------------------
public static string ReadNodeFromXML(string filePath,string nodeName)
{
string result = "error";
try
{
//判断文件是否存在
if (File.Exists(filePath) && nodeName != "")
{
XmlDocument xmlDoc = new XmlDocument();//新建XML文件
xmlDoc.Load(filePath);//加载XML文件
XmlNode xm = xmlDoc.GetElementsByTagName(nodeName)[0];
result = xm.InnerText;
}
else
{
result = "error";
}
}
catch
{
result = "error";
}
return result;
}
相关文章推荐
- C#读写xml文件
- C#读写XML文件
- c#读写XML文件
- c#读写XML文件2008-07-14 11:48c#读写xml文件
- C#.net读写XML文件
- c#读写xml文件
- C# 读写XML配置文件
- C# 里XML文件读写
- C#读写xml文件
- [转]C#读写xml文件
- c#读写XML文件
- c#读写xml文件
- c#通用配置文件读写类与格式转换(xml,ini,json)
- C#实现XML文件读写
- C#中XmlTextWriter读写xml文件详细介绍
- C#读写xml文件
- c#读写xml文件
- C# 读写XML文件
- c#通用配置文件读写类与格式转换(xml,ini,json)
- C# XML配置文件读写类(用于程序配置保存)