微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.net – 读/写/修改XML

我有这样的 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>

我正在使用它作为我的代码配置文件,我想要检索它们的值(innerText)

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)

那么如何读取它们的值以将它们分配给布尔变量,如果我想用false改变它们的一个值,我怎么能这样做呢?

谢谢…

问候…

P.s:如果你写了更多的解释性代码,我们将非常感激.

再次感谢…

解决方法

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,我没有事先编译它.此代码按原样提供,不作任何担保或保证.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。