C#和App.config [英] C# and App.config

查看:82
本文介绍了C#和App.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,


我有一个app.config,其中包含重复标签,如下所示:


< filelist>

< file>

< filetype> xxx< / filetype>

< hex> yyyy< / hex>

< / file>


< file>

< filetype> aaa< / filetype>

< hex> ; bbb< / hex>

< / file>

< / filelist>


任何人都可以建议一种方法读出这些数值并说出加载到

arraylist?


谢谢

Msuk

All,

I have a app.config which contains repeating tags as shown below:

<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>

<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>

Can anyone suggest a way to read these values out and say load them into a
arraylist?

Thanks
Msuk

推荐答案

你说他们在app.config中?它们是如何在

app.config中构建的?它们是否在< appSettings>中标签或一些专门的标签。


或者这是一个配置文件与

Configuration命名空间使用的app.config文件无关? />

" msuk" < MS ** @ discussions.microsoft.com>在消息中写道

新闻:5A ********************************** @ microsof t.com ...
You say thay are in the app.config? How are they structured in the
app.config? Are they in the <appSettings> tag or some specialized tag.

Or is this a config file that has no relation to the app.config file used by
the Configuration namespace?

"msuk" <ms**@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
全部,

我有一个app.config,其中包含重复的标签,如下所示:

< filelist>
< file>
< filetype> xxx< / filetype>
< hex> yyyy< / hex>
< / file>
< file>
< filetype> aaa< / filetype>
< hex> bbb< / hex>
< / file>
< / filelist>

任何人都可以建议一种方法来读取这些值,然后将它们加载到
arraylist中吗?

谢谢
Msuk
All,

I have a app.config which contains repeating tags as shown below:

<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>

<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>

Can anyone suggest a way to read these values out and say load them into a
arraylist?

Thanks
Msuk


>我有一个app.config,其中包含重复标记,如下所示:
>I have a app.config which contains repeating tags as shown below:
< filelist>
< file>
< filetype> xxx< / filetype>
< hex> yyyy< / hex>
< / file>

< file>
< ; filetype> aaa< / filetype>
< hex> bbb< / hex& gt;
< / file>
< / filelist>

任何人都可以建议一种方法来读取这些值,然后将它们加载到
arraylist中吗?
<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>

<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>

Can anyone suggest a way to read these values out and say load them into a
arraylist?




你必须编写自己的自定义配置部分处理程序 - 而且它是非常可怕的,因为它可能不是那么可怕声音!


基本上,你需要一个实现

" IConfigurationSectionHandler"接口。这是一个非常简单的

接口,它只包含一个方法 - Create。那个方法

从你的配置文件中获取一个XmlNode,并将它传递给你的类 -

然后你需要解析它并返回一些任意对象(例如

ArrayList)。


所以你的部分处理程序看起来像:

公共类MyFileListHandler:IConfigurationSectionHandler

{

公共对象创建(对象父对象,对象ConfigContext,XmlNode

部分)

{

ArrayList alsResult = new ArrayList();


//你得到的节点包含< filelist> .....< / filelist>

//所以迭代所有< file>包含在其中的标签

XmlNodeList oListOfFiles = section.SelectNodes(" // file");

每个< file>
//节点,抓住它的< filetype>和< hex>

//子节点并存入您的自定义对象

foreach(oListOfFiles中的XmlNode oSingleFileNode)

{

//抓住子节点

XmlNode oFileType =

oSingleFileNode.SelectSingleNode(" // filetype");

string sFileType = oFileType.Value;


XmlNode oHex = oSingleFileNode.SelectSingleNode(" // hex");

string sHex = oHex.Value;


alsResult.Add(new FileTypeHex(sFileType,sHex));

}


返回alsResult;

}

}


最后,你需要在app.config中定义这个自定义部分

文件,以便.NET配置系统知道它:


< configuration>

< configSections>

< section name =" filelist"

type =" YourAssembly.MyFileListHandler" />

< / configSections>


< filelist>

< file>

< filetype> xxx< / filetype>

< hex> yyyy< / hex>

< / file>

< ; file>

< filetype> aaa< / filetype>

< hex> bbb< / hex>

< / file>

< / filelist>

< / configuration>


这应该有用,而且非常简单,一旦你完成它并且

你已经掌握了它!


Marc


================================================= = ==============

Marc Scheuner可能会和你在一起!

伯尔尼,瑞士m.scheuner -at- inova .ch



You''d have to write your own custom config section handler - and it''s
really not that frightening as it may sound!

Basically, you need a class which implements the
"IConfigurationSectionHandler " interface. This is a really simple
interface, which contains only a single method - "Create". That method
takes a XmlNode from your config files, and passes it to your class -
you then need to parse that and return some arbitrary object (e.g. an
ArrayList).

So your section handler would look something like:

public class MyFileListHandler : IConfigurationSectionHandler
{
public object Create(object parent, object ConfigContext, XmlNode
section)
{
ArrayList alsResult = new ArrayList();

// you get the node containing the <filelist>.....</filelist>
// so iterate over all <file> tags contained in it
XmlNodeList oListOfFiles = section.SelectNodes("//file");

// for each <file> node, grab its <filetype> and <hex>
// child nodes and store into your custom object
foreach(XmlNode oSingleFileNode in oListOfFiles)
{
// grab the child nodes
XmlNode oFileType =
oSingleFileNode.SelectSingleNode("//filetype");
string sFileType = oFileType.Value;

XmlNode oHex = oSingleFileNode.SelectSingleNode("//hex");
string sHex = oHex.Value;

alsResult.Add(new FileTypeHex(sFileType, sHex));
}

return alsResult;
}
}

And lastly, you need to define this custom section in your app.config
file, so that the .NET configuration system will know about it:

<configuration>
<configSections>
<section name="filelist"
type="YourAssembly.MyFileListHandler" />
</configSections>

<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>
<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>
</configuration>

That should work and it''s really quite easy, once you''ve done it and
you''ve gotten the hang of it !

Marc

================================================== ==============
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch





感谢您的回复,我仍然有点困惑,想知道您是否

可以帮助我。


public o bject Create(对象父对象,对象ConfigContext,XmlNode

部分)


我创建了下面的函数,想知道我需要传递给

''创建''即


什么是''父'',''ConfigContext''和''XmlNode''。


另外如何找到''FileTypeHex'',因为我的项目没有构建


alsResult.Add(new FileTypeHex(sFileType,sHex)) ;


谢谢

" Marc Scheuner [MVP ADSI]"写道:
Hi,

thanks for your response, I am still a bit confused and was wondering if you
could help me.

public object Create(object parent, object ConfigContext, XmlNode
section)

I have created the function below and wanted to know what I need to pass to
the ''Create'' i.e.

what is the ''parent'', ''ConfigContext'' and the ''XmlNode''.

Also how do I find the ''FileTypeHex'' as my project does not build

alsResult.Add(new FileTypeHex(sFileType, sHex));

Thanks
"Marc Scheuner [MVP ADSI]" wrote:
我有一个app.config,其中包含重复的标签,如下所示:
< filelist>
<文件>
< filetype> xxx< / filetype>
< hex> yyyy< / hex>
< / file>

< file>
< filetype> aaa< / filetype>
< hex> bbb< / hex>
< / file>
< / filelist>
任何人都可以建议一种方法来读取这些值,然后将它们加载到
arraylist中吗?
I have a app.config which contains repeating tags as shown below:
<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>

<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>

Can anyone suggest a way to read these values out and say load them into a
arraylist?



你必须编写自己的自定义配置部分处理程序 - 和它听起来确实不那么令人恐惧!

基本上,你需要一个实现
IConfigurationSectionHandler的课程。接口。这是一个非常简单的界面,它只包含一个方法 - 创建。该方法从配置文件中获取XmlNode,并将其传递给您的类 -
然后您需要解析它并返回一些任意对象(例如,一个
ArrayList)。

所以你的节处理程序看起来像这样:
公共类MyFileListHandler:IConfigurationSectionHandler
公共对象创建(对象父对象,对象ConfigContext,XmlNode
部分)
{ArrayList alsResult = new ArrayList();

//你得到的节点包含< filelist> .....< / filelist>
//所以迭代所有< file>包含在其中的标签
XmlNodeList oListOfFiles = section.SelectNodes(" // file");

//对于每个< file>节点,抓住它的< filetype>和< hex>
//子节点并存入您的自定义对象
foreach(oListOfFiles中的XmlNode oSingleFileNode)
//抓住子节点
XmlNode oFileType =
oSingleFileNode.SelectSingleNode(" // filetype");
string sFileType = oFileType.Value;

XmlNode oHex = oSingleFileNode.SelectSingleNode(" // hex" );
字符串sHex = oHex.Value;

alsResult.Add(new FileTypeHex(sFileType,sHex));
}
返回alsResult;
}


最后,您需要在app.config
文件中定义此自定义部分,以便.NET配置系统知道它:

< configuration>
< configSections>
< section name =" filelist"
type =" YourAssembly.MyFileListHandler" />
< / configSections>

< filelist>
< file>
< filetype> xxx< / filetype>
< ; hex> yyyy< / hex>
< / file>
< file>
< filetype> aaa< / filetype>
< hex> bbb< / hex> ;
< / file>
< / filelist>
< / configuration>

这应该有效,一旦你真的很容易你已经完成了它并且你已经掌握了它!

Marc

============= ===================================== ============= =
Marc Scheuner可能是你的来源!
瑞士伯尔尼m.scheuner -at- inova.ch



You''d have to write your own custom config section handler - and it''s
really not that frightening as it may sound!

Basically, you need a class which implements the
"IConfigurationSectionHandler " interface. This is a really simple
interface, which contains only a single method - "Create". That method
takes a XmlNode from your config files, and passes it to your class -
you then need to parse that and return some arbitrary object (e.g. an
ArrayList).

So your section handler would look something like:

public class MyFileListHandler : IConfigurationSectionHandler
{
public object Create(object parent, object ConfigContext, XmlNode
section)
{
ArrayList alsResult = new ArrayList();

// you get the node containing the <filelist>.....</filelist>
// so iterate over all <file> tags contained in it
XmlNodeList oListOfFiles = section.SelectNodes("//file");

// for each <file> node, grab its <filetype> and <hex>
// child nodes and store into your custom object
foreach(XmlNode oSingleFileNode in oListOfFiles)
{
// grab the child nodes
XmlNode oFileType =
oSingleFileNode.SelectSingleNode("//filetype");
string sFileType = oFileType.Value;

XmlNode oHex = oSingleFileNode.SelectSingleNode("//hex");
string sHex = oHex.Value;

alsResult.Add(new FileTypeHex(sFileType, sHex));
}

return alsResult;
}
}

And lastly, you need to define this custom section in your app.config
file, so that the .NET configuration system will know about it:

<configuration>
<configSections>
<section name="filelist"
type="YourAssembly.MyFileListHandler" />
</configSections>

<filelist>
<file>
<filetype>xxx</filetype>
<hex>yyyy</hex>
</file>
<file>
<filetype>aaa</filetype>
<hex>bbb</hex>
</file>
</filelist>
</configuration>

That should work and it''s really quite easy, once you''ve done it and
you''ve gotten the hang of it !

Marc

================================================== ==============
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch



这篇关于C#和App.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆