netcore操作xml文件
- NetCore
- 2021/10/29 15:33:00
- 人已阅读
简介netcore操作xml文件,类似于framework操作xml读取appsettings节点
1.反序列化xml文件:
var xmls = XmlHelper.Load<TagList>(ServerExtension.MapPath("Configs/TagList.Config", true)); var tagList = xmls.tagList;
2.xml文件示例:
<?xml version="1.0" encoding="utf-8"?> <!-- ================== TAGS配置 ================== --> <Tags xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Tag key="tag0" value="项目Note" count="0" isNew="false"/> <Tag key="tag1" value="worknote" count="0" isNew="false"/> <Tag key="tag2" value="C#" count="0" isNew="false"/> <Tag key="tag3" value="NetCore" count="0" isNew="false"/> <Tag key="tag4" value="数据仓库" count="0" isNew="false"/> <Tag key="tag5" value="Python" count="0" isNew="false"/> <Tag key="tag6" value="前端心得" count="0" isNew="false"/> <Tag key="tag7" value="工具使用" count="0" isNew="false"/> <Tag key="tag8" value="NodeJs" count="0" isNew="false"/> <Tag key="tag9" value="VueJs" count="0" isNew="false"/> <Tag key="tag10" value="PostgreSQL" count="0" isNew="false"/> <Tag key="tag11" value="SqlServer" count="0" isNew="false"/> <Tag key="tag12" value="FreeSql" count="0" isNew="false"/> <Tag key="tag13" value="ElementUI" count="0" isNew="false"/> </Tags>
3.xml文件model:
using System; using System.Collections.Generic; using System.Xml.Serialization; namespace Jabil.Web.Models { [XmlRoot(ElementName = "Tags")] public class TagList { List<Tag> tags = new List<Tag>(); [XmlElement(ElementName = "Tag")] public List<Tag> tagList { get { return tags; } set { tags = value; } } } public class Tag { [XmlAttribute(AttributeName = "key")] public string key { get; set; } [XmlAttribute(AttributeName = "value")] public string value { get; set; } [XmlAttribute(AttributeName = "count")] public int count { get; set; } [XmlAttribute(AttributeName = "isNew")] public bool isNew { get; set; } } }
文章评论
- 消灭零回复