操作XML--C#

前端之家收集整理的这篇文章主要介绍了操作XML--C#前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;

namespace operate_xml
{
class operate_xml1
{
public XmlDocument myXmlDocument;

/// <summary>
/// 带参构造函数
/// </summary>
/// <param name="xml_name">Xml的路径</param>
public operate_xml1(string xml_name)
{
myXmlDocument = new XmlDocument();
myXmlDocument.Load(xml_name);
}
/// <summary>
/// 更新节点
/// </summary>
/// <param name="xml_name"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="weapon"></param>
/// <returns></returns>
public bool updata_xml(string xml_name,string name,string value,string weapon)
{
bool isSuccess = false;
XmlNode node;

node = myXmlDocument.DocumentElement; //获取XML文档的根
try
{
foreach (XmlNode node1 in node.ChildNodes)
foreach (XmlNode node2 in node1.ChildNodes)
{
if (node2.InnerText == name)
{
node2.NextSibling.InnerText = value;//获取下一个节点
node2.NextSibling.NextSibling.InnerText = weapon;//获取下一个节点
isSuccess = true;
}
}
myXmlDocument.Save(xml_name);

}
catch (Exception ex)
{
}
return isSuccess;
}
/// <summary>
/// 添加节点
/// </summary>
/// <param name="xml_name"></param>
/// <param name="name"></param>
/// <param name="power"></param>
/// <param name="weapon"></param>
/// <returns></returns>
public bool CreatNode(string xml_name,string power,string weapon)
{
bool isSuccess = false;
XmlNode node;
node = myXmlDocument.DocumentElement;
try
{
XmlElement subElement = myXmlDocument.createElement_x("body");

XmlElement subElement1 = myXmlDocument.createElement_x("name");
subElement1.InnerText = name;
XmlElement subElement2 = myXmlDocument.createElement_x("power");
subElement2.InnerText = power;
XmlElement subElement3 = myXmlDocument.createElement_x("weapon");
subElement3.InnerText = weapon;

subElement.AppendChild(subElement1);
subElement.AppendChild(subElement2);
subElement.AppendChild(subElement3);

node.AppendChild(subElement);
myXmlDocument.Save(xml_name);

isSuccess = true;
}
catch (Exception ex)
{ }
return isSuccess;
}
/// <summary>
/// 删除节点,第一种方法
/// </summary>
/// <param name="xml_name"></param>
/// <param name="name"></param>
/// <returns></returns>
public bool DeleteNode(string xml_name,string name)
{
bool isSuccess = false;
XmlNode node;
node = myXmlDocument.DocumentElement;
try
{
foreach (XmlNode node1 in node.ChildNodes)
foreach (XmlNode node2 in node1.ChildNodes)
if (node2.InnerText == name)
node2.ParentNode.RemoveAll();
myXmlDocument.Save(xml_name);//记得要保存
isSuccess = true;
}
catch (Exception ex)
{ }
return isSuccess;
}
/// <summary>
/// 删除节点,第二种方法
/// </summary>
/// <param name="xml_name"></param>
/// <param name="name"></param>
/// <returns></returns>
public bool DeleteNode1(string xml_name,string name)
{
bool isSuccess = false;
XmlNode node1;
XmlNodeList nodelist = myXmlDocument.SelectNodes("histroy/body/name");//选择所有匹配name的节点
foreach (XmlNode node in nodelist)
{
if (node.InnerText == name)
{
node1 = node.ParentNode;
node1.ParentNode.RemoveChild(node1);
isSuccess = true;
}
}
myXmlDocument.Save(xml_name);
return isSuccess;
}
/// <summary>
/// 查询人物
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public XmlNode getxmlnode(string name)
{
XmlNode xmlNode=null;
XmlNodeList nodelist = myXmlDocument.SelectNodes("//histroy//body//name");//选择所有匹配name的节点
foreach (XmlNode node in nodelist)
{
if (node.InnerText == name)
{
xmlNode = node.ParentNode;
}
}
return xmlNode;
}
/// <summary>
/// 使用dataset显示整个XML文件
/// </summary>
/// <returns></returns>
public DataSet GetDs()
{
DataSet ds = new DataSet();
try
{
System.IO.StringReader read = new System.IO.StringReader(myXmlDocument.SelectSingleNode("//histroy").OuterXml);
ds.ReadXml(read);
read.Close();
}
catch
{ }
return ds;
}

}

}

原文链接:https://www.f2er.com/xml/299494.html

猜你在找的XML相关文章