Repeated XML elements include index

When Microsoft.Extensions.Configuration.Xml is used to read an XML document that has repeated XML elements without a Name attribute, the Configuration entries created with these repeated elements now have an index appended to their configuration path.

Version introduced

.NET 6

Previous behavior

Consider the following XML snippets that show repeated elements without a distinguishing Name attribute.

  <Data ConnectionString="TestConnectionString" />
  <Data Provider="MySql" />
        <Level2 Key1="Value1" />
        <Level2 Key2="Value2" />

The configurations created from these XML files were:

Data:ConnectionString = TestConnectionString
Data:Provider = MySql


Level1:Level2:Key1 = Value1
Level1:Level2:Key2 = Value2


New behavior

The configurations created from the XML files in the Previous behavior section are now:

Data:0:ConnectionString = TestConnectionString
Data:1:Provider = MySql


Level1:Level2:0:Key1 = Value1
Level1:Level2:1:Key2 = Value2


Type of breaking change

This change can affect binary compatibility.

Reason for change

This change was introduced to fully support repeated XML elements that don't have a Name attribute. The previous behavior only allowed for repeated elements to set unique values (using attributes or subelements). If repeated XML elements had the same attribute, an exception was thrown.

To get the original behavior, you can update your XML to collapse the two attributes into the same element. For example:

        <Level2 Key1="Value1" Key2="Value2" />

Alternatively, you can update your code to expect indices (such as 0, 1, 2) in the IConfiguration keys:




Affected APIs

See also