xx...">

Read / Write / Modify XML

I have an XML file like this

<?xml version="1.0" encoding="utf-8" ?> <Configurations> <EmailConfiguration> <userName>xxxx</userName> <password>xxx</password> <displayName>xxxxx</displayName> <hostAddress>xxxx</hostAddress> <sslEnable>xxx</sslEnable> <port>xxx</port> </EmailConfiguration> <LogConfiguration> <logEnable>true</logEnable> <generalEnable>true</generalEnable> <warningEnable>true</warningEnable> <errorEnable>true</errorEnable> </LogConfiguration> </Configurations> 

and I use it as a configuration file for my code, and I want to get their values ​​(innerText) like this

 bool logEnable = value comes from XML (logEnable) bool warningEnable = value comes from XML (warningEnable) bool errorEnable = value comes from XML (errorEnable) bool generalEnable = value comes from XML (generalEnable) 

So, how can I read their values ​​to assign them to boolean variables, and if I wanted to change one of their values ​​with false, how could I do this?

Thanks...

Yours faithfully...

Ps: If you wrote more explanatory codes, that would be so appreciated.

Thanks again...

+4
source share
3 answers
 public class Options { public string UserName { get; set; } public string Password { get; set; } public string DisplayName { get; set; } public string HostAddress { get; set; } public bool SSL { get; set; } public string Port { get; set; } public bool LogEnable { get; set; } public bool GeneralEnable { get; set; } public bool WarningEnable { get; set; } public bool ErrorEnable { get; set; } public static Options Load(string path) { Options options = new Options(); XmlDocument xml = new XmlDocument(); xml.Load(path); XmlNodeReader input = new XmlNodeReader(xml); while (input.Read()) { var elementname = input.Name.ToLower(); switch (elementname) { case "username": options.UserName = input.Value; break; // all other cases case "logenable": options.LogEnable = Boolean.Parse(input.Value); break; // continue with other cases } } } public static void Save(Options options, string path) { XmlTextWriter writer = new XmlTextWriter(path); xmlWriter.WriteStartDocument(true); xmlWriter.WriteStartElement("configuration"); xmlWriter.WriteStartElement("emailConfiguration"); xmlWriter.WriteStartElement("userName"); xmlWriter.WriteString(options.UserName); xmlWriter.WriteEndElemement(); // continue for all elements xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("logConfiguration"); xmlWriter.WriteStartElement("logEnable"); xmlWriter.WriteString(options.LogEnable.ToString()); xmlWriter.WriteEndElemement(); // continue for all elements xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.Close(); } } 

I left some work for you to finish;) In addition, I did not write that it was Visual Studio, and I did not compile it before starting work. This code is provided without warranty or guarantee .;)

This is the basic process for reading / writing XML in .NET, although there are many options. You can use XPath queries, or if you are using .NET 3.5, you can use Linq to Sql, which will give you street credit for cool kids. But the above sample should lift you up and run fast, just promise that you will go on some research on these other things, you will feel better.

+5
source

The best practice would be to actually use the web.config or app.config file. It also facilitates this process.

+1
source

I found something like this, and it actually satisfied me as I want :) But I want to know what is an efficient way to extract data from XML? Here is the tho code:

 XmlDocument doc = new XmlDocument(); doc.Load(HttpContext.Current.Server.MapPath("config.xml")); logEnable = Convert.ToBoolean(doc["Configurations"]["LogConfiguration"]["logEnable"].InnerText); warningEnable = Convert.ToBoolean(doc["Configurations"]["LogConfiguration"]["warningEnable"].InnerText); errorEnable = Convert.ToBoolean(doc["Configurations"]["LogConfiguration"]["errorEnable"].InnerText); generalEnable = Convert.ToBoolean(doc["Configurations"]["LogConfiguration"]["generalEnable"].InnerText); 
0
source

All Articles