开发者

can't get two connecting strings from XML (web.config)

开发者 https://www.devze.com 2022-12-31 21:50 出处:网络
XmlTextReader reader = new XmlTextReader(Window1.cfg.FSAddress); bool[] startreading = {false , false};
            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
   开发者_如何学C                     if (reader.Name == "connectionStrings")
                            startreading[0] = true;
                        break;
                    case XmlNodeType.EndElement:
                        if (startreading[1])
                            if (reader.Name == "add")
                                startreading[0] = true;
                        break;
                }
            }

I take first one but ... then happens something strange and I'm missing second

sorry for my english . btw - Im getting it not from web project.


Why would you use SAX-based approach for reading configuration files? These are usually negligibly small, so loading entire document into an XmlDocument and then traversing it using XPath will be a much better solution.


You can use ConfigurationManager.ConnectionStrings to read the connection strings from the connection string section of your web.config file.

Or you can use ConfigurationManager.GetSection to get a section from the config file. If it is a predefined section you can cast it to this type, or you can implement your own custom section (see System.Configuration.ConfigurationSection for a sample).

Oh yes ... you have to include the System.Configuration into your references to work with these classes.


Why try so hard?

Use this: ConfigurationManager.ConnectionStrings

Which is a collection of ConnectionStrings

Use ConfigurationManager.ConnectionStrings['nameOfTheConnectionString'].ConnectionString() to access the string itself.

ConfigurationManager lives in the System.Configuration namespace


I can't tell what you're trying to do. But maybe XPath will be a lot easier than SAX.

0

精彩评论

暂无评论...
验证码 换一张
取 消