XElement.ReplaceAll Metodo

Definizione

Sostituisce i nodi figlio e gli attributi di questo elemento con il contenuto specificato.

Overload

ReplaceAll(Object)

Sostituisce i nodi figlio e gli attributi di questo elemento con il contenuto specificato.

ReplaceAll(Object[])

Sostituisce i nodi figlio e gli attributi di questo elemento con il contenuto specificato.

Esempio

Nell'esempio seguente vengono passati i risultati di una query LINQ a questo metodo, sostituendo il contenuto di un elemento con i risultati della query. Esegue una query sull'elemento con il relativo contenuto sostituito.

XElement xmlTree = new XElement("Root",
    new XElement("Data", 1),
    new XElement("Data", 2),
    new XElement("Data", 3),
    new XElement("Data", 4),
    new XElement("Data", 5)
);

Console.WriteLine(xmlTree);
Console.WriteLine("-----");

xmlTree.ReplaceAll(
    from el in xmlTree.Elements()
    where (int)el >= 3
    select new XElement("NewData", (int)el)
);

Console.WriteLine(xmlTree);
Dim xmlTree As XElement = _
    <Root>
        <Data>1</Data>
        <Data>2</Data>
        <Data>3</Data>
        <Data>4</Data>
        <Data>5</Data>
    </Root>

Console.WriteLine(xmlTree)
Console.WriteLine("-----")

xmlTree.ReplaceAll( _
    From el In xmlTree.Elements _
    Where el.Value >= 3 _
    Select <NewData><%= el.Value %></NewData> _
)

Console.WriteLine(xmlTree)

Nell'esempio viene prodotto l'output seguente:

<Root>
  <Data>1</Data>
  <Data>2</Data>
  <Data>3</Data>
  <Data>4</Data>
  <Data>5</Data>
</Root>
-----
<Root>
  <NewData>3</NewData>
  <NewData>4</NewData>
  <NewData>5</NewData>
</Root>

Commenti

Questo metodo usa la semantica snapshot, ovvero crea una copia separata del nuovo contenuto prima di sostituire il contenuto dell'elemento corrente con il nuovo contenuto. Ciò significa che è possibile eseguire una query sul contenuto dell'elemento corrente e usare i risultati della query come nuovo contenuto specificato.

Per informazioni dettagliate sul contenuto valido che può essere passato a questa funzione, vedere Contenuto valido di oggetti XElement e XDocument.

Questo metodo genererà gli Changed eventi e Changing .

ReplaceAll(Object)

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

Sostituisce i nodi figlio e gli attributi di questo elemento con il contenuto specificato.

public:
 void ReplaceAll(System::Object ^ content);
public void ReplaceAll (object content);
public void ReplaceAll (object? content);
member this.ReplaceAll : obj -> unit
Public Sub ReplaceAll (content As Object)

Parametri

content
Object

Contenuto che sostituisce i nodi figlio e gli attributi di questo elemento.

Esempio

Nell'esempio seguente viene utilizzato questo metodo.

XElement root = new XElement("Root",
    new XElement("Child", "child content")
);

// ReplaceAll with an XElement object.
root.ReplaceAll(new XElement("NewChild", "n"));
Console.WriteLine(root);

// ReplaceAll with an XAttribute object.
root.ReplaceAll(new XAttribute("NewAttribute", "n"));
Console.WriteLine(root);

// ReplaceAll with a string.
root.ReplaceAll("Some text");
Console.WriteLine(root);

// ReplaceAll with a double.
double dbl = 12.345;
root.ReplaceAll(dbl);
Console.WriteLine(root);

// ReplaceAll with a DateTime object.
DateTime dt = new DateTime(2006, 10, 6, 12, 30, 00);
root.ReplaceAll(dt);
Console.WriteLine(root);

// ReplaceAll with a string array.
// Any collection other than a collection of XElement or XAttribute objects
// are converted to strings. The strings are concatenated and added.
string[] stringArray = {
    "abc",
    "def",
    "ghi"
};
root.ReplaceAll(stringArray);
Console.WriteLine(root);

// ReplaceAll with an array of XElement objects.
XElement[] ellArray = {
    new XElement("NewChild1", 1),
    new XElement("NewChild2", 2),
    new XElement("NewChild3", 3)
};
root.ReplaceAll(ellArray);
Console.WriteLine(root);

// ReplaceAll with an array of XAttribute objects.
XAttribute[] attArray = {
    new XAttribute("NewAtt1", 1),
    new XAttribute("NewAtt2", 2),
    new XAttribute("NewAtt3", 3)
};
root.ReplaceAll(attArray);
Console.WriteLine(root);
Dim root As XElement = _
    <Root>
        <Child>child content</Child>
    </Root>

' ReplaceAll with an XElement object.
root.ReplaceAll(<NewChild>n</NewChild>)
Console.WriteLine(root)

' ReplaceAll with an XAttribute object.
root.ReplaceAll(New XAttribute("NewAttribute", "n"))
Console.WriteLine(root)

' ReplaceAll with a string.
root.ReplaceAll("Some text")
Console.WriteLine(root)

' ReplaceAll with a double.
Dim dbl As Double = 12.345
root.ReplaceAll(dbl)
Console.WriteLine(root)

' ReplaceAll with a DateTime object.
Dim dt As DateTime = New DateTime(2006, 10, 6, 12, 30, 0)
root.ReplaceAll(dt)
Console.WriteLine(root)

' ReplaceAll with a string array.
' Any collection other than a collection of XElement or XAttribute objects
' are converted to strings. The strings are concatenated and added.
Dim stringArray As String() = { _
    "abc", _
    "def", _
    "ghi" _
}
root.ReplaceAll(stringArray)
Console.WriteLine(root)

' ReplaceAll with an array of XElement objects.
Dim ellArray As XElement() = { _
    New XElement("NewChild1", 1), _
    New XElement("NewChild2", 2), _
    New XElement("NewChild3", 3) _
}
root.ReplaceAll(ellArray)
Console.WriteLine(root)

' ReplaceAll with an array of XAttribute objects.
Dim attArray As XAttribute() = { _
New XAttribute("NewAtt1", 1), _
New XAttribute("NewAtt2", 2), _
New XAttribute("NewAtt3", 3) _
}
root.ReplaceAll(attArray)
Console.WriteLine(root)

Nell'esempio viene prodotto l'output seguente:

<Root>
  <NewChild>n</NewChild>
</Root>
<Root NewAttribute="n" />
<Root>Some text</Root>
<Root>12.345</Root>
<Root>2006-10-06T12:30:00</Root>
<Root>abcdefghi</Root>
<Root>
  <NewChild1>1</NewChild1>
  <NewChild2>2</NewChild2>
  <NewChild3>3</NewChild3>
</Root>
<Root NewAtt1="1" NewAtt2="2" NewAtt3="3" />

Commenti

Questo metodo rimuove prima di tutto il contenuto e gli attributi esistenti. Aggiunge quindi l'oggetto specificato content.

Questo metodo usa la semantica snapshot, ovvero crea una copia separata del nuovo contenuto prima di sostituire il contenuto dell'elemento corrente con il nuovo contenuto. Ciò significa che è possibile eseguire una query sul contenuto dell'elemento corrente e usare i risultati della query come nuovo contenuto specificato.

Per informazioni dettagliate sul contenuto valido che può essere passato a questa funzione, vedere Contenuto valido di oggetti XElement e XDocument.

Questo metodo genererà gli Changed eventi e Changing .

Vedi anche

Si applica a

ReplaceAll(Object[])

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

Sostituisce i nodi figlio e gli attributi di questo elemento con il contenuto specificato.

public:
 void ReplaceAll(... cli::array <System::Object ^> ^ content);
public void ReplaceAll (params object[] content);
public void ReplaceAll (params object?[] content);
member this.ReplaceAll : obj[] -> unit
Public Sub ReplaceAll (ParamArray content As Object())

Parametri

content
Object[]

Elenco di parametri di oggetti contenuto.

Esempio

Nell'esempio seguente vengono passati i risultati di una query LINQ a questo metodo, sostituendo il contenuto di un elemento con i risultati della query.

XElement xmlTree1 = new XElement("Root",
    new XElement("Child1", 1),
    new XElement("Child2", 2),
    new XElement("Child3", 3),
    new XElement("Child4", 4),
    new XElement("Child5", 5),
    new XElement("Child6", 6)
);

XElement root = new XElement("Root",
    new XElement("Child", "child content")
);

root.ReplaceAll(
    from el in xmlTree1.Elements()
    where((int)el >= 3 && (int)el <= 5)
    select el
);
Console.WriteLine(root);
Dim xmlTree1 As XElement = _
        <Root>
            <Child1>1</Child1>
            <Child2>2</Child2>
            <Child3>3</Child3>
            <Child4>4</Child4>
            <Child5>5</Child5>
            <Child6>6</Child6>
        </Root>

Dim root As XElement = <Root>
                           <Child>child content</Child>
                       </Root>

root.ReplaceAll( _
    From el In xmlTree1.Elements() _
    Where el.Value >= 3 And el.Value <= 5 _
    Select el _
)

Console.WriteLine(root)

Nell'esempio viene prodotto l'output seguente:

<Root>
  <Child3>3</Child3>
  <Child4>4</Child4>
  <Child5>5</Child5>
</Root>

Commenti

Questo metodo rimuove prima di tutto il contenuto e gli attributi esistenti. Aggiunge quindi l'oggetto specificato content.

Questo metodo usa la semantica snapshot, ovvero crea una copia separata del nuovo contenuto prima di sostituire il contenuto dell'elemento corrente con il nuovo contenuto. Ciò significa che è possibile eseguire una query sul contenuto dell'elemento corrente e usare i risultati della query come nuovo contenuto specificato.

Per informazioni dettagliate sul contenuto valido che può essere passato a questa funzione, vedere Contenuto valido di oggetti XElement e XDocument.

Questo metodo genererà gli Changed eventi e Changing .

Vedi anche

Si applica a