Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.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# can';t从XML(web.config)获取两个连接字符串_C#_.net_Xml - Fatal编程技术网

C# can';t从XML(web.config)获取两个连接字符串

C# can';t从XML(web.config)获取两个连接字符串,c#,.net,xml,C#,.net,Xml,我选第一个,但是。。。然后发生了一些奇怪的事情,我错过了第二个 对不起我的英语。顺便说一句,我不是从web项目获得的。您可以使用ConfigurationManager.ConnectionString从web.config文件的连接字符串部分读取连接字符串 或者可以使用ConfigurationManager.GetSection从配置文件中获取节。如果它是预定义的节,您可以将其强制转换为这种类型,或者您可以实现自己的自定义节(有关示例,请参见System.Configuration.Conf

我选第一个,但是。。。然后发生了一些奇怪的事情,我错过了第二个
对不起我的英语。顺便说一句,我不是从web项目获得的。

您可以使用
ConfigurationManager.ConnectionString
web.config
文件的连接字符串部分读取连接字符串

或者可以使用
ConfigurationManager.GetSection
从配置文件中获取节。如果它是预定义的节,您可以将其强制转换为这种类型,或者您可以实现自己的自定义节(有关示例,请参见
System.Configuration.ConfigurationSection


哦,是的。。。要使用这些类,您必须将
系统.Configuration
包含到引用中。

您可以使用
ConfigurationManager.ConnectionStrings
web.config
文件的连接字符串部分读取连接字符串

或者可以使用
ConfigurationManager.GetSection
从配置文件中获取节。如果它是预定义的节,您可以将其强制转换为这种类型,或者您可以实现自己的自定义节(有关示例,请参见
System.Configuration.ConfigurationSection


哦,是的。。。您必须在引用中包含
System.Configuration
,才能使用这些类。

为什么要使用基于SAX的方法读取配置文件?它们通常很小,所以将整个文档加载到
XmlDocument
中,然后使用XPath遍历它将是一个更好的解决方案。

为什么要使用基于SAX的方法读取配置文件?它们通常很小,所以将整个文档加载到
XmlDocument
中,然后使用XPath遍历它将是一个更好的解决方案。

为什么要这么努力呢

使用以下命令: ConfigurationManager.ConnectionString

它是连接字符串的集合

使用
ConfigurationManager.ConnectionString['NameOfConnectionString'].ConnectionString()
访问字符串本身

ConfigurationManager
位于
系统中。配置
命名空间

为什么要这么努力

使用以下命令: ConfigurationManager.ConnectionString

它是连接字符串的集合

使用
ConfigurationManager.ConnectionString['NameOfConnectionString'].ConnectionString()
访问字符串本身


ConfigurationManager
位于
系统中。配置
命名空间

我不知道您想做什么。但是XPath可能比SAX容易得多。

我不知道您想做什么。但是XPath可能比SAX容易得多

            XmlTextReader reader = new XmlTextReader(Window1.cfg.FSAddress);
            bool[] startreading = {false , false};
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:    // Узел является элементом.
                        if (startreading[0])
                        {
                            if (reader.Name == "add")
                                if (startreading[1])
                                {
                                    id2.Text = reader.GetAttribute(1);
                                    return;
                                }
                                else
                                {
                                    id1.Text = reader.GetAttribute(1);
                                    startreading[1] = true;
                                    startreading[0] = false;
                                }
                        }
                        else
                        if (reader.Name == "connectionStrings")
                            startreading[0] = true;
                        break;
                    case XmlNodeType.EndElement:
                        if (startreading[1])
                            if (reader.Name == "add")
                                startreading[0] = true;
                        break;
                }
            }