您的位置:首页 > 编程语言 > C#

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# XML