XElement.ReplaceAttributes Method

Definition

Replaces the attributes of this element with the specified content.

Overloads

ReplaceAttributes(Object[])

Replaces the attributes of this element with the specified content.

ReplaceAttributes(Object)

Replaces the attributes of this element with the specified content.

Examples

The following example creates an element with three attributes. It then uses this method to replace all of the attributes of the element with a single attribute.

C#
XElement root = new XElement("Root",
    new XAttribute("Att1", 1),
    new XAttribute("Att2", 2),
    new XAttribute("Att3", 3)
);
root.ReplaceAttributes(
    new XAttribute("NewAtt1", 101)
);
Console.WriteLine(root);

This example produces the following output:

XML
<Root NewAtt1="101" />

Remarks

This method will raise the Changed and the Changing events.

For details about the valid content that can be passed to this function, see Valid Content of XElement and XDocument Objects.

ReplaceAttributes(Object[])

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Replaces the attributes of this element with the specified content.

C#
public void ReplaceAttributes(params object[] content);
C#
public void ReplaceAttributes(params object?[] content);

Parameters

content
Object[]

A parameter list of content objects.

Examples

The following example creates an element with three attributes. It then replaces the attributes with other attributes.

C#
XElement root = new XElement("Root",
    new XAttribute("Att1", 1),
    new XAttribute("Att2", 2),
    new XAttribute("Att3", 3)
);
root.ReplaceAttributes(
    new XAttribute("NewAtt1", 101),
    new XAttribute("NewAtt2", 102),
    new XAttribute("NewAtt3", 103)
);
Console.WriteLine(root);

This example produces the following output:

XML
<Root NewAtt1="101" NewAtt2="102" NewAtt3="103" />

Remarks

This method first removes existing attributes. It then adds the specified content.

For details about the valid content that can be passed to this function, see Valid Content of XElement and XDocument Objects.

This method will raise the Changed and the Changing events.

See also

Applies to

.NET 10 i druge verzije
Proizvod Verzije
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0

ReplaceAttributes(Object)

Source:
XElement.cs
Source:
XElement.cs
Source:
XElement.cs

Replaces the attributes of this element with the specified content.

C#
public void ReplaceAttributes(object content);
C#
public void ReplaceAttributes(object? content);

Parameters

content
Object

The content that will replace the attributes of this element.

Examples

The following example creates an element with three attributes. It then uses this method to replace all of the attributes of the element with a single attribute.

C#
XElement root = new XElement("Root",
    new XAttribute("Att1", 1),
    new XAttribute("Att2", 2),
    new XAttribute("Att3", 3)
);
root.ReplaceAttributes(
    new XAttribute("NewAtt1", 101)
);
Console.WriteLine(root);

This example produces the following output:

XML
<Root NewAtt1="101" />

Remarks

This method first removes existing attributes. It then adds the specified content.

For details about the valid content that can be passed to this function, see Valid Content of XElement and XDocument Objects.

This method will raise the Changed and the Changing events.

See also

Applies to

.NET 10 i druge verzije
Proizvod Verzije
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0