当前位置 : 主页 > 网页制作 > xml >

.net – 读/写/修改XML

来源:互联网 收集:自由互联 发布时间:2021-06-13
我有这样的 XML文件 ?xml version="1.0" encoding="utf-8" ? Configurations EmailConfiguration userNamexxxx/userName passwordxxx/password displayNamexxxxx/displayName hostAddressxxxx/hostAddress sslEnablexxx/sslEnable portxxx/port /Email
我有这样的 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,我没有事先编译它.此代码按原样提供,不作任何担保或保证.

网友评论