2008-12-11 69 views
4

我有这样的读/写/修改XML

<?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> 

一个XML文件,我用它作为我的代码的配置文件,我想这样的

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) 
检索它们的值(的innerText)

那么如何读取它们的值以将它们分配给布尔变量,并且如果我想用false更改其中的一个值,我将如何能够做到这一点?

谢谢...

问候......

P.S:如果你写更多的解释代码,这将是这么多的赞赏。再次

谢谢...

回答

5
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(); 
    } 
} 

我留给你完成一些工作;)另外,我没有写,这是Visual Studio中,我的手之前没有对其进行编译。此代码按原样提供,不提供任何保证或担保。 ;)

这是.NET中的基本XML读/写过程,虽然有很多选项。您可以使用XPath查询,或者如果您使用的是.NET 3.5,则可以使用Linq to Sql,这将为您带来酷酷的孩子们的街头信誉。但是上面的示例应该能让你快速运行,并且保证你也会对这些其他事情做一些研究,你会更好。

+0

谢谢,我会做一些调查,现在:)感谢帮助。 – Tarik 2008-12-11 19:19:22

1

最佳做法是实际使用web.config或app.config文件。它也使这个过程变得更加简单。

+0

是的,他可以用这些值抛弃appSettings,它确实更容易。创建自定义配置部分并不容易。但他的基本问题是阅读和编写XML文件。不应该使用XML文件。阅读和编写XML是一件很好理解的事情。 – NerdFury 2008-12-11 19:25:59

0

我发现了这样的事情,这实际上满足了我的想法:) 但我想知道这是从XML检索数据的有效方法? 下面是代码寿:

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);