| |

VerySource

 Forgot password?
 Register
Search
View: 970|Reply: 6

A little doubt about the NameValueCollection in the configuration file

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-22 11:40:01
| Show all posts |Read mode
I started writing a configuration very early, but I found that the configuration that comes with net is not clear.
 
Look at the code below app.config
 
<? xml version = "1.0" encoding = "utf-8"?>
<configuration>
  <configSections>
    
    <section name = "myCustomerSectionA" type = "System.Configuration.NameValueSectionHandler" />
    
    <section name = "myCustomerSectionB" type = "ConfigurationDemoII.CustomerSectionHandlers, ConfigurationDemoII" />
   
  </ configSections>
  <myCustomerSectionA>
    <add key = "myKeyA" value = "myKeyAValueA" />
    <add key = "myKeyA" value = "myKeyAValueB" />
    <add key = "myKeyB" value = "myKeyBValueA" />
    <add key = "myKeyB" value = "myKeyBValueB" />
  </ myCustomerSectionA>
  <myCustomerSectionB>
    <add key = "myKeyA" value = "myKeyAValueA" />
    <add key = "myKeyA" value = "myKeyAValueB" />
    <add key = "myKeyB" value = "myKeyBValueA" />
    <add key = "myKeyB" value = "myKeyBValueB" />
  </ myCustomerSectionB>
  
</ configuration>
 
app.cs
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Xml;
using System.Configuration;
namespace ConfigurationDemoII
{
    class Program
    {
        static void Main (string [] args)
        {
            try
            {
                TestSysNameValueCollection ();
                Console.WriteLine ("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";);
                TestMyNameValueCollection ();
            }
            catch (Exception ex)
            {
                Console.WriteLine (ex.Message);
                Console.WriteLine (ex.StackTrace);
            }
        }
        private static void TestSysNameValueCollection ()
        {
            NameValueCollection nvc = (NameValueCollection) ConfigurationManager.GetSection ("myCustomerSectionA");
            foreach (string key in nvc.AllKeys)
            {
                Console.WriteLine (key);
                Console.WriteLine (nvc [key]);
            }
        }
        private static void TestMyNameValueCollection ()
        {
            NameValueCollection nvc = (NameValueCollection) ConfigurationManager.GetSection ("myCustomerSectionB");
            foreach (string key in nvc.AllKeys)
            {
                Console.WriteLine (key);
                Console.WriteLine (nvc [key]);
            }
        }
    }
    class CustomerSectionHandlers: IConfigurationSectionHandler
    {
        #region IConfigurationSectionHandler Members
        object System.Configuration.IConfigurationSectionHandler.Create (object parent, object configContext, System.Xml.XmlNode section)
        {
            NameValueCollection col = new NameValueCollection ();
            foreach (XmlNode childNode in section.ChildNodes)
            {
                XmlElement root = (XmlElement) childNode;
                if (root.Name == "add")
                {
                    string key = root.GetAttribute ("key");
                    string value = root.GetAttribute ("value");
                    col.Add (key, value);
                }
                else if (root.Name == "remove")
                {
                }
                else if (root.Name == "clear")
                {
                }
            }
            return col;
        }
        #endregion
    }
}

 
 
 
Results of the run
 
myKeyA
myKeyAValueB
myKeyB
myKeyBValueB
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
myKeyA
myKeyAValueA, myKeyAValueB
myKeyB
myKeyBValueA, myKeyBValueB
Press any key to continue ...
 
Can you see any difference? The namevaluecollection that comes with Sys seems to flush out the information of the same key that was added later. If the configuration itself is designed this way, why use a NameValueCollection container?
 
I did not find relevant information in several articles about configuration in cnblog http://msdn.microsoft.com/msdn-online/shared/components/ratings/ratings.aspx? ContentID = _978498&HideDiscuss = 1 A similar introduction
 
OK, open Reflect and take a look.
internal static object CreateStatic (object parent, XmlNode section, string keyAttriuteName, string valueAttributeName)
{
      ReadOnlyNameValueCollection collection1;
      if (parent == null)
      {
            collection1 = new ReadOnlyNameValueCollection (new CaseInsensitiveHashCodeProvider (CultureInfo.InvariantCulture), new CaseInsensitiveComparer (CultureInfo.InvariantCulture));
      }
      else
      {
            ReadOnlyNameValueCollection collection2 = (ReadOnlyNameValueCollection) parent;
            collection1 = new ReadOnlyNameValueCollection (collection2);
      }
      HandlerBase.CheckForUnrecognizedAttributes (section);
      foreach (XmlNode node1 in section.ChildNodes)
      {
            if (! HandlerBase.IsIgnorableAlsoCheckForNonElement (node1))
            {
                  if (node1.Name == "add")
                  {
                        string text1 = HandlerBase.RemoveRequiredAttribute (node1, keyAttriuteName);
                        string text2 = HandlerBase.RemoveRequiredAttribute (node1, valueAttributeName, true);
                        HandlerBase.CheckForUnrecognizedAttributes (node1);
                        collection1 [text1] = text2;
                  }
                  else
                  {
                        if (node1.Name == "remove")
                        {
                              string text3 = HandlerBase.RemoveRequiredAttribute (node1, keyAttriuteName);
                              HandlerBase.CheckForUnrecognizedAttributes (node1);
                              collection1.Remove (text3);
                              continue;
                        }
                        if (node1.Name.Equals ("clear"))
{HandlerBase.CheckForUnrecognizedAttributes (node1);
                              collection1.Clear ();
                              continue;
                        }
                        HandlerBase.ThrowUnrecognizedElement (node1);
                  }
            }
      }
      collection1.SetReadOnly ();
      return collection1;
}


 ReadOnlyNameValueCollection This thing SDK does not introduce HandlerBase This thing has not been studied in depth, halo

 
Just be lazy and wait for the cow to answer
 
The past cattle, please leave a good opinion
Reply

Use magic Report

0

Threads

32

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-2-2 14:54:02
| Show all posts
The landlord found a phenomenon, but I couldn't see where the doubt was.
If you write the NameValueSectionHandler yourself, to achieve the same effect, it is not difficult to keep only the last value with the same key.
NameValueCollection cannot have the same key, so add is to add values ​​with the same key together. (Realization of the landlord)
As for the specific implementation of the Framework's NameValueSectionHandler, I didn't dig into it, because it is not difficult to achieve the same effect, and it saves this effort.

--- Personal point of view, do not like to blame.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-2-19 17:45:01
| Show all posts
To江南小色

My doubt is that since the Configuration that comes with net returns a NameValueCollection, then it should not be "retain only the last value with the same key", because the characteristic of the container of namevaluecollection is that one key corresponds to multiple values, and it can completely return A hashtable or other container, there is no need to confuse the user. What I want to know is if some of the code I wrote did not pay attention to this problem or MS bug.
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-5 17:15:01
| Show all posts
Hashtables under Collections are not allowed to have the same key, so the landlord should use it in a very idiomatic way, using value as the key and key as the value, which can be blocked when the value is different.
If you really need it, you can construct a chained hash table yourself and put an ArrayList in the hashtable.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-7 09:30:01
| Show all posts
Study
Reply

Use magic Report

0

Threads

32

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-3-20 18:15:01
| Show all posts
NameValueCollection cannot have one key corresponding to multiple values. When Add (key, value) is encountered, the same keys are encountered, and the values ​​are added together, separated by commas.
Just look at its count property, and there isn't much.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-10 12:00:01
| Show all posts
NameValueCollection can have multiple values ​​per key

The key is that different methods are called and the results are different.

If you use nc.Add, add value to the same key
If it is nc.Set/nc[] is set or replace.

nc.Get / nc[] Yes return string
nc.GetValue returns the corresponding multi-value array

If you can't have more value

URLs like ?method=post&method=get&method=head are not parsed
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list