c#操作xml文件示例
时间:2023-12-11 06:40:42
1. 新增XML文件
XMLToolV2 _xmlHelper = new XMLToolV2(@"C:\20140311blogs.xml");//xml保存路径或者读取路径
_xmlHelper.Create("Person", "utf-8");//跟节点名称:person;encode:utf-8
XmlElement _person = _xmlHelper.CreateElec("Name", "Yan-Zhiwei");//在跟节点后创建person节点
_xmlHelper.SetAttribute(_person, "Gender", "Man");//设置person节点属性Gender
_xmlHelper.SetAttribute(_person, "Address", "shanghai");//设置person节点属性Address
_xmlHelper.Save();//保存xml文件
上述代码实现效果:
那在Person节点继续增加节点,实现也很简单
_xmlHelper.Create("Person", "utf-8");//跟节点名称:person;encode:utf-8
XmlElement _person = _xmlHelper.CreateElec("Name", "Yan-Zhiwei");//在跟节点后创建person节点
_xmlHelper.SetAttribute(_person, "Gender", "Man");//设置person节点属性Gender
_xmlHelper.SetAttribute(_person, "Address", "shanghai");//设置person节点属性Address
XmlElement _workLh = _xmlHelper.CreateElec(_person, "Work", "shanghai LH");//在person节点下增加work节点
_xmlHelper.SetAttribute(_workLh, "Year", "2013~");//设置work节点属性Title
XmlElement _workRK = _xmlHelper.CreateElec(_person, "Work", "shanghai Ranking");//在person节点下增加work节点
_xmlHelper.SetAttribute(_workRK, "Year", "2011~2013");//设置work节点属性Title
_xmlHelper.Save();//保存xml文件
上述代码实现效果:
2.读取节点的值,个人感觉xpath方式比linq to xml更为明了方便
XMLToolV2 _xmlHelper = new XMLToolV2(@"C:\20140311blogs.xml");//xml保存路径或者读取路径
XmlNode _person = _xmlHelper.Select("Person/Name/text()");
Console.WriteLine("Name:" + _person.InnerText);
XmlNode _gender = _xmlHelper.Select("Person/Name[@Gender='Man']");
Console.WriteLine("Gender:" + _gender.Attributes["Gender"].Value);
Console.WriteLine("Address:" + _gender.Attributes["Address"].Value);
Console.WriteLine("-----------------------------");
XmlNodeList _workRecord = _xmlHelper.SelectNodeList("Person/Name/Work");
XMLToolV2.Loop(_workRecord, (XmlNode node) =>
{
Console.WriteLine("Work:" + node.InnerText + " Year:" + node.Attributes["Year"].Value);
});
上述代码实现效果:
3.格式化显示XML
XMLToolV2 _xmlHelper = new XMLToolV2(@"C:\20140311blogs.xml");//xml保存路径或者读取路径
string _xmlString = _xmlHelper.ShowXml();
Console.WriteLine(XMLToolV2.FormatXml(_xmlString, "utf-8"));
上述代码实现效果:
4.XMLToolV2源代码
public class XMLToolV2
{
static string _xmlPath;
static XmlDocument _xmlDoc { get; set; }
static XmlElement _xmlRoot;
public XMLToolV2(string xmlPath)
{
_xmlPath = xmlPath;
LoadXmlDoc();
}
public XmlNode Select(string xPath)
{
if (string.IsNullOrEmpty(xPath))
throw new ArgumentNullException("xPath");
return _xmlDoc.SelectSingleNode(xPath);
}
public XmlNodeList SelectNodeList(string xPath)
{
if (string.IsNullOrEmpty(xPath))
throw new ArgumentNullException("xPath");
return _xmlDoc.SelectNodes(xPath);
}
public void Create(string rootName, string encode)
{
CreateXmlDoc(rootName, encode);
}
private void CreateXmlDoc(string rootName, string encode)
{
if (!File.Exists(_xmlPath))
{
if (string.IsNullOrEmpty(rootName))
throw new ArgumentNullException(rootName);
_xmlDoc = new XmlDocument();
XmlDeclaration _xmldecl = _xmlDoc.CreateXmlDeclaration("1.0", encode, null);
_xmlDoc.AppendChild(_xmldecl);
_xmlRoot = _xmlDoc.CreateElement("", rootName, "");
_xmlDoc.AppendChild(_xmlRoot);
}
}
public void LoadXmlDoc()
{
if (File.Exists(_xmlPath))
{
_xmlDoc = new XmlDocument();
_xmlDoc.Load(_xmlPath);
_xmlRoot = _xmlDoc.DocumentElement;
}
}
public void Save()
{
if (_xmlDoc != null)
{
_xmlDoc.Save(_xmlPath);
}
}
public XmlElement CreateElec(string elecName, string elecValue)
{
XmlElement _xElec = CreateElement(_xmlRoot, elecName, elecValue);
return _xElec;
}
private XmlElement CreateElement(XmlNode _xmldocSelect, string elecName, string elecValue)
{
if (_xmldocSelect != null)
{
XmlElement _xElec = _xmlDoc.CreateElement(elecName);
_xElec.InnerText = elecValue;
_xmldocSelect.AppendChild(_xElec);
return _xElec;
}
return null;
}
public XmlElement CreateElec(XmlElement xmlParentElec, string elecName, string elecValue)
{
if (xmlParentElec != null)
{
XmlElement _xElec = CreateElement(xmlParentElec, elecName, elecValue);
return _xElec;
}
return null;
}
public void SetAttribute(XmlElement xElement, string attrName, string attrValue)
{
if (xElement != null)
{
xElement.SetAttribute(attrName, attrValue);
}
}
public int Check(string xpath)
{
if (string.IsNullOrEmpty(xpath))
throw new ArgumentNullException("xpath");
return SelectNodeList(xpath).Count;
}
public string ShowXml()
{
if (_xmlDoc != null)
{
return _xmlDoc.OuterXml;
}
return string.Empty;
}
public static string FormatXml(string xmlString, string encode)
{
if (string.IsNullOrEmpty(xmlString))
throw new ArgumentNullException("xmlString");
if (string.IsNullOrEmpty(encode))
throw new ArgumentNullException("encode");
MemoryStream _mstream = new MemoryStream();
XmlTextWriter _writer = new XmlTextWriter(_mstream, null);
XmlDocument _xDoc = new XmlDocument();
_writer.Formatting = Formatting.Indented;
_xDoc.LoadXml(xmlString);
_xDoc.WriteTo(_writer);
_writer.Flush();
_writer.Close();
Encoding _encoding = Encoding.GetEncoding(encode);
string _xmlString = _encoding.GetString(_mstream.ToArray());
_mstream.Close();
return _xmlString;
}
public static void Loop(XmlNodeList nodeList, Action<XmlNode> xmlNode)
{
if (nodeList != null)
{
foreach (XmlNode xNode in nodeList)
{
xmlNode(xNode);
}
}
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Android Studio 3.0的新特性与适配
![](https://img.aspxhome.com/file/2023/1/139111_0s.jpg)
c# 获取照片的经纬度和时间的示例代码
Android 6.0动态权限申请教程
Java设计模式之接口隔离原则精解
![](https://img.aspxhome.com/file/2023/5/80935_0s.png)
C#实现对二维数组排序的方法
详解Springboot Oauth2 Server搭建Oauth2认证服务
![](https://img.aspxhome.com/file/2023/9/125639_0s.gif)
JAVA中对List进行查询
Android ItemDecoration 实现分组索引列表的示例代码
![](https://img.aspxhome.com/file/2023/1/98661_0s.gif)
游戏开发之随机概率的选择算法
SpringBoot集成Jpa对数据进行排序、分页、条件查询和过滤操作
![](https://img.aspxhome.com/file/2023/7/70507_0s.png)
Flutter runApp到渲染上屏分析详解
![](https://img.aspxhome.com/file/2023/6/106056_0s.jpg)
mybatis中使用大于小于等于的正确方法
基于java实现画图板功能
![](https://img.aspxhome.com/file/2023/7/103837_0s.jpg)
基于Ant路径匹配规则AntPathMatcher的注意事项
![](https://img.aspxhome.com/file/2023/9/76629_0s.png)
SpringBoot使用Sharding-JDBC实现数据分片和读写分离的方法
![](https://img.aspxhome.com/file/2023/7/57827_0s.jpg)
分享Spring Cloud OpenFeign 的五个优化技巧
![](https://img.aspxhome.com/file/2023/1/79891_0s.webp)
Android实现图片添加阴影效果的2种方法
![](https://img.aspxhome.com/file/2023/0/100610_0s.jpg)
Java中的Static class详解及实例代码
JDK动态代理,代理接口没有实现类,实现动态代理方式
Android—基于微信开放平台v3SDK开发(微信支付填坑)
![](https://img.aspxhome.com/file/2023/6/137346_0s.png)