Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# app.config控制器和写入程序_C#_App Config_Tex - Fatal编程技术网

C# app.config控制器和写入程序

C# app.config控制器和写入程序,c#,app-config,tex,C#,App Config,Tex,我需要有关app.config控件和写入的帮助。我有一个乳胶项目。我需要编写配置来更改PDF的章节。例如,我有3章,但我现在不需要2章。因此,我只想在我的主文本中\include包含\include chap1和\include chap3。我有app.config <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add ke

我需要有关app.config控件和写入的帮助。我有一个乳胶项目。我需要编写配置来更改PDF的章节。例如,我有3章,但我现在不需要2章。因此,我只想在我的
主文本中
\include
包含
\include chap1
\include chap3
。我有app.config

 <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
     <appSettings>
        <add key="\include" value="chap1" />
        <add key="\include" value="chap2" />
        <add key="\include" value="chap3" />
     </appSettings>
    </configuration>

我可以用什么方法控制和使用这个配置。有可能吗


谢谢。

app.config只是一个XML文件。。。因此,要快速简便地解决一个简单的配置文件,只需将其视为:

using System.Xml.Linq;

// Create a list just in case you want to remove specific elements later
List<XElement> toRemove = new List<XElement>();

// Load the config file
XDocument doc = XDocument.Load("app.config");

// Get the appSettings element as a parent
XContainer appSettings = doc.Element("configuration").Element("appSettings");

// step through the "add" elements
foreach (XElement xe in appSettings.Elements("add"))
{
    // Get the values
    string addKey = xe.Attribute("key").Value;
    string addValue = xe.Attribute("value").Value;

    // if you want to remove it...
    if (addValue == "something")
    {
        // you can't remove it directly in the foreach loop since it breaks the enumerable
        // add it to a list and do it later
        toRemove.Add(xe);
    }
}

// Remove the elements you've selected
foreach (XElement xe in toRemove)
{
    xe.Remove();
}

// Add any new Elements that you want
appSettings.Add(new XElement("add", 
    new XAttribute("key", "\\inculde"),
    new XAttribute("value", "chapX")));
使用System.Xml.Linq;
//创建一个列表,以防以后要删除特定元素
List toRemove=新列表();
//加载配置文件
XDocument doc=XDocument.Load(“app.config”);
//获取appSettings元素作为父元素
XContainer appSettings=doc.Element(“配置”).Element(“appSettings”);
//逐步完成“添加”元素
foreach(appSettings.Elements(“添加”)中的XElement xe)
{
//获取值
字符串addKey=xe.Attribute(“key”).Value;
字符串addValue=xe.Attribute(“value”).value;
//如果你想删除它。。。
如果(addValue==“某物”)
{
//您不能在foreach循环中直接删除它,因为它会破坏可枚举性
//将其添加到列表中,稍后再执行
toRemove.Add(xe);
}
}
//删除选定的图元
foreach(toRemove中的XElement xe)
{
xe.Remove();
}
//添加所需的任何新元素
appSettings.Add(新元素(“Add”,
新的XAttribute(“键”、“\\inculde”),
新的XAttribute(“value”、“chapX”);
如果你确切地知道你想要做什么,你可能会使用更有针对性的解决方案


但是,对于您的场景,您可能希望将此add元素加载到集合中,根据需要对其进行处理(添加/删除/更新等),然后将其作为.config文件中的“add”元素再次写回。

app.config只是一个XML文件。。。因此,要快速简便地解决一个简单的配置文件,只需将其视为:

using System.Xml.Linq;

// Create a list just in case you want to remove specific elements later
List<XElement> toRemove = new List<XElement>();

// Load the config file
XDocument doc = XDocument.Load("app.config");

// Get the appSettings element as a parent
XContainer appSettings = doc.Element("configuration").Element("appSettings");

// step through the "add" elements
foreach (XElement xe in appSettings.Elements("add"))
{
    // Get the values
    string addKey = xe.Attribute("key").Value;
    string addValue = xe.Attribute("value").Value;

    // if you want to remove it...
    if (addValue == "something")
    {
        // you can't remove it directly in the foreach loop since it breaks the enumerable
        // add it to a list and do it later
        toRemove.Add(xe);
    }
}

// Remove the elements you've selected
foreach (XElement xe in toRemove)
{
    xe.Remove();
}

// Add any new Elements that you want
appSettings.Add(new XElement("add", 
    new XAttribute("key", "\\inculde"),
    new XAttribute("value", "chapX")));
使用System.Xml.Linq;
//创建一个列表,以防以后要删除特定元素
List toRemove=新列表();
//加载配置文件
XDocument doc=XDocument.Load(“app.config”);
//获取appSettings元素作为父元素
XContainer appSettings=doc.Element(“配置”).Element(“appSettings”);
//逐步完成“添加”元素
foreach(appSettings.Elements(“添加”)中的XElement xe)
{
//获取值
字符串addKey=xe.Attribute(“key”).Value;
字符串addValue=xe.Attribute(“value”).value;
//如果你想删除它。。。
如果(addValue==“某物”)
{
//您不能在foreach循环中直接删除它,因为它会破坏可枚举性
//将其添加到列表中,稍后再执行
toRemove.Add(xe);
}
}
//删除选定的图元
foreach(toRemove中的XElement xe)
{
xe.Remove();
}
//添加所需的任何新元素
appSettings.Add(新元素(“Add”,
新的XAttribute(“键”、“\\inculde”),
新的XAttribute(“value”、“chapX”);
如果你确切地知道你想要做什么,你可能会使用更有针对性的解决方案

但是,对于您的场景,您可能希望将此add元素加载到集合中,根据需要对其进行处理(添加/删除/更新等),然后将其作为.config文件中的“add”元素再次写回