C#对xml的操作
xml的写操作:
public void WriteXML(string FileName,string server,string IDServiceid,string name)
{
if (!File.Exists(FileName))
{
XmlTextWriter textWriter = new XmlTextWriter(FileName,null);
textWriter.Formatting = Formatting.Indented;
// 开始写过程,调用WriteStartDocument方法
textWriter.WriteStartDocument();
// 写入说明
textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
textWriter.WriteComment("YtConfig.xml in root dir");
//创建一个节点
textWriter.WriteStartElement("Server");
//textWriter.WriteElementString("MapServers","");
//textWriter.WriteElementString("site", "w3sky.com");
textWriter.WriteEndElement();
// 写文档结束,调用WriteEndDocument方法
textWriter.WriteEndDocument();
// 关闭textWriter
textWriter.Close();
}
//初始化XML文档操作类
XmlDocument myDoc = new XmlDocument();
//加载XML文件
myDoc.Load(FileName);
//添加元素--UserCode
XmlElement ele = myDoc.CreateElement("UserCode");
XmlText text = myDoc.CreateTextNode(server);
//添加元素--UserName
XmlElement ele1 = myDoc.CreateElement("UserName");
XmlText text1 = myDoc.CreateTextNode(IDServiceid);
//添加元素--UserPwd
XmlElement ele2 = myDoc.CreateElement("UserPwd");
XmlText text2 = myDoc.CreateTextNode(name);
//添加节点 User要对应我们xml文件中的节点名字
XmlNode newElem = myDoc.CreateNode("element", "User", "");
//在节点中添加元素
newElem.AppendChild(ele);
newElem.LastChild.AppendChild(text);
newElem.AppendChild(ele1);
newElem.LastChild.AppendChild(text1);
newElem.AppendChild(ele2);
newElem.LastChild.AppendChild(text2);
XmlNode roots = myDoc.CreateNode("element", "MapServers", "");
roots.AppendChild(newElem);
//将节点添加到文档中
XmlElement root = myDoc.DocumentElement;
root.AppendChild(roots);
//保存
myDoc.Save(FileName);
}
生成的xml为:
<?xml version="1.0"?>
<!--First Comment XmlTextWriter Sample Example-->
<!--YtConfig.xml in root dir-->
<Server>
<MapServers>
<User>
<UserCode>
</UserCode>
<UserName>
</UserName>
<UserPwd>
</UserPwd>
</User>
</MapServers>
</Server>
读操作:
public string ReadXML(string path, string id) {
string stat = null;
string FileName = AppDomain.CurrentDomain.BaseDirectory;
FileName += path + ".xml";
string type = path + "s";
if (File.Exists(FileName))
{
//初始化XML文档操作类
XmlDocument myDoc = new XmlDocument();
//加载XML文件
myDoc.Load(FileName);
if (myDoc.SelectSingleNode(type).ChildNodes.Count != 0)
{
XmlNodeList nodeList = myDoc.SelectSingleNode(type).ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("IDServiceid") == id)
{
stat = "fail";
break;
}
else { stat = "ok"; }
}
}
else { stat = "ok"; }
}
else
{
return stat = "ok";
}
return stat;
}
删除节点操作:
public string DelXML(string id, string path)
{
string stat = null;
string FileName = AppDomain.CurrentDomain.BaseDirectory;
string type = path + "s";
FileName += path + ".xml";
if (File.Exists(FileName))
{
//初始化XML文档操作类
XmlDocument myDoc = new XmlDocument();
//加载XML文件
myDoc.Load(FileName);
if (myDoc.SelectSingleNode(type).ChildNodes.Count != 0)
{
XmlNodeList nodeList = myDoc.SelectSingleNode(type).ChildNodes;
foreach (XmlNode xn in nodeList)
{
XmlElement xe = (XmlElement)xn;
if (xe.GetAttribute("IDServiceid") == id)
{
myDoc.SelectSingleNode(type).RemoveChild(xe);
stat = "ok";
myDoc.Save(FileName);
break;
}
else
{
stat = "error";
}
}
}
else {
stat = "error";
}
}
else
{
stat = "error";
}
return stat;
}