O'Reilly logo

LINQ Pocket Reference by Joseph Albahari, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Updating an X-DOM

You can update elements and attributes in the following ways:

  • Call SetValue or reassign the Value property.

  • Call SetElementValue or SetAttributeValue.

  • Call one of the RemoveXXX methods.

  • Call one the of the AddXXX or ReplaceXXX methods, specifying fresh content.

You can also reassign the Name property on XElement objects.

Simple Value Updates

Members

Works on

SetValue (object)

XElement, XAttribute

Value

XElement, XAttribute

The SetValue method replaces an element or attribute’s content with a simple value. Setting the Value property does the same, but accepts string data only. We describe both of these functions in detail later (see the upcoming “Working with Values” section).

An effect of calling SetValue (or reassigning Value ) is that it replaces all child nodes:

	XElement settings = new XElement ("settings",
	                      new XElement ("timeout", 30)
	                    );
	settings.SetValue ("blah");
	Console.WriteLine (settings.ToString( ));

	// RESULT: <settings>blah</settings>

Updating Child Nodes and Attributes

Category

Members

Works on

Add

Add (params object[])

XContainer

 

AddFirst (params object[])

XContainer

Remove

RemoveNodes()

XContainer

 

RemoveAttributes()

XElement

 

RemoveAll()

XElement

Update

ReplaceNodes (params object[])

XContainer

 

ReplaceAttributes (params object[])

XElement

 

ReplaceAll (params object[])

XElement

 

SetElementValue (XName, object)

XElement

 

SetAttributeValue (XName, object)

XElement

The most convenient methods in this group are the last two: SetElementValue ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required