Maintain name-value pairs (LINQ to XML)
Many applications have to maintain information that is best kept as name-value pairs. This information might be configuration information or global settings. LINQ to XML contains methods that make it easy to maintain a set of name-value pairs. You can either keep the information as attributes or as a set of child elements.
One difference between keeping the information as attributes or as child elements is that attributes have the constraint that there can be only one attribute with a particular name for an element. This limitation doesn't apply to child elements.
SetAttributeValue and SetElementValue
The two methods that facilitate keeping name-value pairs are SetAttributeValue and SetElementValue. These two methods have similar semantics.
SetAttributeValue can add, modify, and remove attributes of an element.
- If you call SetAttributeValue with a name of an attribute that doesn't exist, the method creates a new attribute and adds it to the specified element.
- If you call SetAttributeValue with a name of an existing attribute and with some specified content, the contents of the attribute are replaced with the specified content.
- If you call SetAttributeValue with a name of an existing attribute, and specify
null
for the content, the attribute is removed from its parent.
SetElementValue can add, modify, and remove child elements of an element.
- If you call SetElementValue with a name of a child element that doesn't exist, the method creates a new element and adds it to the specified element.
- If you call SetElementValue with a name of an existing element and with some specified content, the contents of the element are replaced with the specified content.
- If you call SetElementValue with a name of an existing element, and specify
null
for the content, the element is removed from its parent.
Example: Use SetAttributeValue
to create and maintain a list of name-value pairs
The following example creates an element with no attributes. It then uses the SetAttributeValue method to create and maintain a list of name-value pairs.
// Create an element with no content.
XElement root = new XElement("Root");
// Add a number of name-value pairs as attributes.
root.SetAttributeValue("Top", 22);
root.SetAttributeValue("Left", 20);
root.SetAttributeValue("Bottom", 122);
root.SetAttributeValue("Right", 300);
root.SetAttributeValue("DefaultColor", "Color.Red");
Console.WriteLine(root);
// Replace the value of Top.
root.SetAttributeValue("Top", 10);
Console.WriteLine(root);
// Remove DefaultColor.
root.SetAttributeValue("DefaultColor", null);
Console.WriteLine(root);
' Create an element with no content.
Dim root As XElement = <Root/>
' Add a number of name-value pairs as attributes.
root.SetAttributeValue("Top", 22)
root.SetAttributeValue("Left", 20)
root.SetAttributeValue("Bottom", 122)
root.SetAttributeValue("Right", 300)
root.SetAttributeValue("DefaultColor", "Color.Red")
Console.WriteLine(root)
' Replace the value of Top.
root.SetAttributeValue("Top", 10)
Console.WriteLine(root)
' Remove DefaultColor.
root.SetAttributeValue("DefaultColor", Nothing)
Console.WriteLine(root)
This example produces the following output:
<Root Top="22" Left="20" Bottom="122" Right="300" DefaultColor="Color.Red" />
<Root Top="10" Left="20" Bottom="122" Right="300" DefaultColor="Color.Red" />
<Root Top="10" Left="20" Bottom="122" Right="300" />
Example: Use SetElementValue
to create and maintain a list of name-value pairs
The following example creates an element with no child elements. It then uses the SetElementValue method to create and maintain a list of name-value pairs.
// Create an element with no content.
XElement root = new XElement("Root");
// Add a number of name-value pairs as elements.
root.SetElementValue("Top", 22);
root.SetElementValue("Left", 20);
root.SetElementValue("Bottom", 122);
root.SetElementValue("Right", 300);
root.SetElementValue("DefaultColor", "Color.Red");
Console.WriteLine(root);
Console.WriteLine("----");
// Replace the value of Top.
root.SetElementValue("Top", 10);
Console.WriteLine(root);
Console.WriteLine("----");
// Remove DefaultColor.
root.SetElementValue("DefaultColor", null);
Console.WriteLine(root);
' Create an element with no content.
Dim root As XElement = <Root/>
' Add a number of name-value pairs as elements.
root.SetElementValue("Top", 22)
root.SetElementValue("Left", 20)
root.SetElementValue("Bottom", 122)
root.SetElementValue("Right", 300)
root.SetElementValue("DefaultColor", "Color.Red")
Console.WriteLine(root)
Console.WriteLine("----")
' Replace the value of Top.
root.SetElementValue("Top", 10)
Console.WriteLine(root)
Console.WriteLine("----")
' Remove DefaultColor.
root.SetElementValue("DefaultColor", Nothing)
Console.WriteLine(root)
This example produces the following output:
<Root>
<Top>22</Top>
<Left>20</Left>
<Bottom>122</Bottom>
<Right>300</Right>
<DefaultColor>Color.Red</DefaultColor>
</Root>
----
<Root>
<Top>10</Top>
<Left>20</Left>
<Bottom>122</Bottom>
<Right>300</Right>
<DefaultColor>Color.Red</DefaultColor>
</Root>
----
<Root>
<Top>10</Top>
<Left>20</Left>
<Bottom>122</Bottom>
<Right>300</Right>
</Root>