C#读写XML文件

    xiaoxiao2021-12-14  18

    使用Configuration读取xml文件

    XML文件格式

    <appSettings> <add key="WebPath" value="http://www.baidu.com"/> </appSettings>

    读取:

    System.Configuration.ConfigurationManager.AppSettings["WebPath"];

    xml文件

    <?xml version="1.0" encoding="utf-8"?> <widget id="io.cordova.hellocordova" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> <content src="index.html" /> <feature name="echo"> <param name="win" value="EchoCommand" /> </feature> <feature name="File"> <param name="win" value="File" /> </feature> <feature name="MacAddress"> <param name="win" value="MacAddress" /> </feature> <feature name="devices"> <param name="win" value="Device" /> </feature> <access origin="*"> <Root xmlns=""> <Child1>1</Child1> <Child2>2</Child2> <Child3>3</Child3> <Child4>4</Child4> <Child5>5</Child5> </Root> </access> <allow-intent href="http://*/*" /> <allow-intent href="https://*/*" /> <allow-intent href="tel:*" /> <allow-intent href="sms:*" /> <allow-intent href="mailto:*" /> <allow-intent href="geo:*" /> </widget>

    读写类

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Linq; namespace Demo.Xml { class XMlDocumentHandler { private string configxml; public string Configxml { get { return "..//..//config.xml"; } set { configxml = value; } } static void Main(string[] args) { try { XMlDocumentHandler xmlHandler = new XMlDocumentHandler(); System.Xml.Linq.XDocument xmlDoc = xmlHandler.IntiXmlMessage(); //xmlHandler.LoadPluginFeatures(xmlDoc); xmlHandler.AddXmlNode(xmlDoc); xmlDoc.Save(xmlHandler.Configxml); } catch (Exception ex) { Console.WriteLine(ex.Message); } //Console.ReadLine(); } public Dictionary<string, string> xmlMsg = new Dictionary<string, string>(); public string ContentSrc; /// <summary> /// Load config.xml and Get Document Project /// </summary> public System.Xml.Linq.XDocument IntiXmlMessage() { if (File.Exists(Configxml)) { System.Xml.Linq.XDocument xmlDoc; using (StreamReader strRead = new StreamReader(Configxml)) { xmlDoc = System.Xml.Linq.XDocument.Parse(strRead.ReadToEnd()); } return xmlDoc; } return null; } /// <summary> /// Get Nodes Vale /// </summary> /// <param name="document">XDocumnet Project</param> private void LoadPluginFeatures(System.Xml.Linq.XDocument document) { var features = from feats in document.Descendants() where feats.Name.LocalName == "feature" select feats; foreach (var feature in features) { string name = (string)feature.Attribute("name"); var value = (from results in feature.Descendants() where results.Name.LocalName == "param" && ((string)results.Attribute("name") == "win") select results).FirstOrDefault(); if (value!=null) { xmlMsg.Add((string)value.Attribute("name"), (string)value.Attribute("value")); } } //获取Content节点的值 var contentsTag = (from results in document.Descendants() where results.Name.LocalName == "content" select results).FirstOrDefault(); if (contentsTag != null) { var src = contentsTag.Attribute("src"); ContentSrc = (string)src.Value; } } /// <summary> /// Add XmlDocument Node /// </summary> private void AddXmlNode(System.Xml.Linq.XDocument xDoc) { var value = (from results in xDoc.Descendants() where results.Name.LocalName == "access" select results).FirstOrDefault(); XElement xmlTree = new XElement("Root", new XElement("Child1", 1), new XElement("Child2", 2), new XElement("Child3", 3), new XElement("Child4", 4), new XElement("Child5", 5) ); value.Add(xmlTree); } } }
    转载请注明原文地址: https://ju.6miu.com/read-970051.html

    最新回复(0)