【问题标题】:Is there a helper method to parse an appSettings-like xml?是否有解析类似 appSettings 的 xml 的辅助方法?
【发布时间】:2011-09-07 16:40:57
【问题描述】:

我的意思是像这样的 appSettings,

<appSettings>
    <add key="myKey" value="myValue" />
</appsettings>

结果是一个键值集合,我可以像这样访问它:

string v = config["myKey"];

但它不一定位于 app.config 中,所以我拥有的是字符串或 XmlNode。

NameValueFileSectionHandler.Create 方法显然可以完成这项工作,但是输入需要两个对象,Object parent,Object configContext,另外还有一个 xml 节点,我不知道要传递给他们什么。

【问题讨论】:

  • 为了澄清您的意思是与 app.config 文件相似但不相同的 xml?
  • 格式相同。不同之处在于它不在 app.config(或 MyProgram.exe.config)中

标签: c# .net xml app-config


【解决方案1】:

像这样将字符串解析成字典,

var xml = XElement.Parse("<appSettings><add key=\"myKey\" value=\"myValue\" /></appSettings>");
var dic = xml.Descendants("add").ToDictionary(x => x.Attribute("key").Value, x => x.Attribute("value").Value);

你可以得到这样的值,

var item = dic["myKey"];

你也可以像这样修改字典中的值,

dic["myKey"] = "new val";

您可以使用此代码将修改后的字典转换回 XElement,

var newXml = new XElement("appSettings", dic.Select(d => new XElement("add", new XAttribute("key", d.Key), new XAttribute("value", d.Value))));

【讨论】:

  • 令人印象深刻!只是好奇,我怎样才能找回元素?
  • 什么元素?如果您谈论键的值,例如var item = dic["myKey"];
  • 我的意思是,您将 XElement 解析为单行中的字典。反向也可以在一行中完成吗?我只能想到一个for循环
  • 太棒了。我编辑了您的代码以添加描述,希望您不介意:)
【解决方案2】:

你可以这样做:

Hashtable htResource = new Hashtable();
    XmlDocument document = new XmlDocument();
    document.LoadXml(XmlString);
    foreach (XmlNode node in document.SelectSingleNode("appSettings"))
    {
        if ((node.NodeType != XmlNodeType.Comment) && !htResource.Contains(node.Attributes["name"].Value))
            {
                htResource[node.Attributes["name"].Value] = node.Attributes["value"].Value;
            }
    }

然后您可以使用以下方法访问这些值:

string myValue = htResource["SettingName"].ToString();

希望对你有帮助,

戴夫

【讨论】:

    【解决方案3】:

    它并不是真正的“帮助者”,但该框架包含一些标记为 Configuration API 的东西。这在配置文件中起作用,并将您的配置 XML 转换为类。在此处查看更多详细信息:

    http://msdn.microsoft.com/en-us/library/ms178688(v=VS.80).aspx http://msdn.microsoft.com/en-us/library/2tw134k3.aspx

    这始终是我使用的示例(因为我永远记不起它的确切结构):

    http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx

    http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx#

    还有一个可与 VS 集成的设计器:

    http://csd.codeplex.com/

    谷歌搜索“C# 自定义配置部分”也会为您带来很多结果。

    【讨论】:

    • 只是澄清一下,我不是指自定义配置格式(如黑客链接中建议的那样)。我仍然想要相同的格式。不同的是它不在 app.config 中,输入的只是一个字符串。
    • @Louis 总而言之,我认为没有可用的助手。相反,将字符串写入文件并使用 ConfigurationManager OpenMappedExe 东西或手动解析它。
    【解决方案4】:

    我还没有尝试过,但我认为下面的代码可能会给你你想要的。

    System.Configuration.ConfigurationFileMap fileMap = new System.Configuration.ConfigurationFileMap(yourConfigFileLocation);
    System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
            var setting = configuration.AppSettings["settingName"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-17
      • 2011-02-14
      • 2012-12-03
      • 1970-01-01
      • 2011-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多