XElement.ReplaceAll メソッド

定義

この要素の子ノードおよび属性を、指定された内容で置き換えます。

オーバーロード

ReplaceAll(Object)

この要素の子ノードおよび属性を、指定された内容で置き換えます。

ReplaceAll(Object[])

この要素の子ノードおよび属性を、指定された内容で置き換えます。

次の例では、LINQ クエリの結果をこのメソッドに渡し、要素の内容をクエリ結果に置き換えます。 内容が置き換えられた要素に対してクエリを実行します。

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)  

この例を実行すると、次の出力が生成されます。

<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>  

注釈

このメソッドはスナップショット セマンティクスを使用します。つまり、現在の要素の内容を新しいコンテンツに置き換える前に、新しいコンテンツの個別のコピーを作成します。 つまり、現在の要素の内容に対してクエリを実行し、クエリの結果を指定した新しいコンテンツとして使用できます。

この関数に渡すことができる有効なコンテンツの詳細については、「 XElement オブジェクトと XDocument オブジェクトの有効なコンテンツ」を参照してください。

このメソッドは、イベントと Changed イベントを Changing 発生させます。

ReplaceAll(Object)

この要素の子ノードおよび属性を、指定された内容で置き換えます。

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)

パラメーター

content
Object

この要素の子ノードおよび属性を置き換える内容。

次の例では、このメソッドを使用します。

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)  

この例を実行すると、次の出力が生成されます。

<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" />  

注釈

このメソッドは、最初に既存のコンテンツと属性を削除します。 次に、指定した content.

このメソッドはスナップショット セマンティクスを使用します。つまり、現在の要素の内容を新しいコンテンツに置き換える前に、新しいコンテンツの個別のコピーを作成します。 つまり、現在の要素の内容に対してクエリを実行し、クエリの結果を指定した新しいコンテンツとして使用できます。

この関数に渡すことができる有効なコンテンツの詳細については、「 XElement オブジェクトと XDocument オブジェクトの有効なコンテンツ」を参照してください。

このメソッドは、イベントと Changed イベントを Changing 発生させます。

こちらもご覧ください

適用対象

ReplaceAll(Object[])

この要素の子ノードおよび属性を、指定された内容で置き換えます。

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())

パラメーター

content
Object[]

コンテンツ オブジェクトのパラメーター リスト。

次の例では、LINQ クエリの結果をこのメソッドに渡し、要素の内容をクエリ結果に置き換えます。

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)  

この例を実行すると、次の出力が生成されます。

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

注釈

このメソッドは、最初に既存のコンテンツと属性を削除します。 次に、指定した content.

このメソッドはスナップショット セマンティクスを使用します。つまり、現在の要素の内容を新しいコンテンツに置き換える前に、新しいコンテンツの個別のコピーを作成します。 つまり、現在の要素の内容に対してクエリを実行し、クエリの結果を指定した新しいコンテンツとして使用できます。

この関数に渡すことができる有効なコンテンツの詳細については、「 XElement オブジェクトと XDocument オブジェクトの有効なコンテンツ」を参照してください。

このメソッドは、イベントと Changed イベントを Changing 発生させます。

こちらもご覧ください

適用対象