다음을 통해 공유


XNode.ReplaceWith 메서드

정의

이 노드를 지정된 콘텐츠로 바꿉니다.

오버로드

ReplaceWith(Object)

이 노드를 지정된 콘텐츠로 바꿉니다.

ReplaceWith(Object[])

이 노드를 지정된 콘텐츠로 바꿉니다.

예제

다음 예제에서는 이 메서드를 사용하여 노드의 내용을 다른 콘텐츠로 바꿉니다.

XElement xmlTree = new XElement("Root",
    new XElement("Child1", "child1 content"),
    new XElement("Child2", "child2 content"),
    new XElement("Child3", "child3 content"),
    new XElement("Child4", "child4 content"),
    new XElement("Child5", "child5 content")
);
XElement child3 = xmlTree.Element("Child3");
child3.ReplaceWith(
    new XElement("NewChild", "new content")
);
Console.WriteLine(xmlTree);
Dim xmlTree As XElement = _
        <Root>
            <Child1>child1 content</Child1>
            <Child2>child2 content</Child2>
            <Child3>child3 content</Child3>
            <Child4>child4 content</Child4>
            <Child5>child5 content</Child5>
        </Root>

Dim child3 As XElement = xmlTree.<Child3>(0)
child3.ReplaceWith(<NewChild>new content</NewChild>)
Console.WriteLine(xmlTree)

이 예제는 다음과 같은 출력을 생성합니다.

<Root>
  <Child1>child1 content</Child1>
  <Child2>child2 content</Child2>
  <NewChild>new content</NewChild>
  <Child4>child4 content</Child4>
  <Child5>child5 content</Child5>
</Root>

설명

이 메서드에 전달할 수 있는 유효한 콘텐츠에 대한 자세한 내용은 XElement 및 XDocument 개체의 유효한 콘텐츠를 참조하세요.

이 메서드는 및 이벤트를 발생 Changed 시킬 Changing 것입니다.

XContainer 자식 노드를 개체의 XNode Singly 연결 목록으로 저장합니다. 즉, 메서드는 ReplaceWith 부모 컨테이너 아래의 직접 자식 노드 목록을 트래버스해야 합니다. 따라서 이 메서드를 사용하면 성능에 영향을 줄 수 있습니다.

ReplaceWith(Object)

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs

이 노드를 지정된 콘텐츠로 바꿉니다.

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

매개 변수

content
Object

이 노드를 대체할 콘텐츠입니다.

예제

다음 예제에서는 이 메서드를 사용하여 노드의 내용을 다른 콘텐츠로 바꿉니다.

XElement xmlTree = new XElement("Root",
    new XElement("Child1", "child1 content"),
    new XElement("Child2", "child2 content"),
    new XElement("Child3", "child3 content"),
    new XElement("Child4", "child4 content"),
    new XElement("Child5", "child5 content")
);
XElement child3 = xmlTree.Element("Child3");
child3.ReplaceWith(
    new XElement("NewChild", "new content")
);
Console.WriteLine(xmlTree);
Dim xmlTree As XElement = _
        <Root>
            <Child1>child1 content</Child1>
            <Child2>child2 content</Child2>
            <Child3>child3 content</Child3>
            <Child4>child4 content</Child4>
            <Child5>child5 content</Child5>
        </Root>

Dim child3 As XElement = xmlTree.<Child3>(0)
child3.ReplaceWith(<NewChild>new content</NewChild>)
Console.WriteLine(xmlTree)

이 예제는 다음과 같은 출력을 생성합니다.

<Root>
  <Child1>child1 content</Child1>
  <Child2>child2 content</Child2>
  <NewChild>new content</NewChild>
  <Child4>child4 content</Child4>
  <Child5>child5 content</Child5>
</Root>

설명

이 메서드는 먼저 부모에서 이 노드를 제거한 다음 이 노드 대신 지정된 콘텐츠를 이 노드의 부모에 추가합니다.

XContainer 자식 노드를 개체의 XNode Singly 연결 목록으로 저장합니다. 즉, 메서드는 ReplaceWith 부모 컨테이너 아래의 직접 자식 노드 목록을 트래버스해야 합니다. 따라서 이 메서드를 사용하면 성능에 영향을 줄 수 있습니다.

이 메서드에 전달할 수 있는 유효한 콘텐츠에 대한 자세한 내용은 XElement 및 XDocument 개체의 유효한 콘텐츠를 참조하세요.

이 메서드는 및 이벤트를 발생 Changed 시킬 Changing 것입니다.

추가 정보

적용 대상

ReplaceWith(Object[])

Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs
Source:
XNode.cs

이 노드를 지정된 콘텐츠로 바꿉니다.

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

매개 변수

content
Object[]

새 콘텐츠의 매개 변수 목록입니다.

예제

다음 예제에서는 LINQ to XML 쿼리의 결과를 이 메서드에 대한 입력으로 사용하는 방법을 보여 드립니다.

XElement srcTree = new XElement("Root",
    new XElement("Element1", 1),
    new XElement("Element2", 2),
    new XElement("Element3", 3),
    new XElement("Element4", 4),
    new XElement("Element5", 5)
);
XElement xmlTree = new XElement("Root",
    new XElement("Child1", 1),
    new XElement("Child2", 2),
    new XElement("Child3", 3),
    new XElement("Child4", 4),
    new XElement("Child5", 5)
);
XElement child3 = xmlTree.Element("Child3");
child3.ReplaceWith(
    from el in srcTree.Elements()
    where (int)el > 3
    select el
);
Console.WriteLine(xmlTree);
Dim srcTree As XElement = _
        <Root>
            <Element1>1</Element1>
            <Element2>2</Element2>
            <Element3>3</Element3>
            <Element4>4</Element4>
            <Element5>5</Element5>
        </Root>

Dim xmlTree As XElement = _
        <Root>
            <Child1>1</Child1>
            <Child2>2</Child2>
            <Child3>3</Child3>
            <Child4>4</Child4>
            <Child5>5</Child5>
        </Root>

Dim child3 As XElement = xmlTree.<Child3>(0)
child3.ReplaceWith( _
    From el In srcTree.Elements() _
    Where (CInt(el) > 3) _
    Select el)

Console.WriteLine(xmlTree)

이 예제는 다음과 같은 출력을 생성합니다.

<Root>
  <Child1>1</Child1>
  <Child2>2</Child2>
  <Element4>4</Element4>
  <Element5>5</Element5>
  <Child4>4</Child4>
  <Child5>5</Child5>
</Root>

설명

이 메서드는 먼저 부모에서 이 노드를 제거한 다음 이 노드 대신 지정된 콘텐츠를 이 노드의 부모에 추가합니다.

XContainer 자식 노드를 개체의 XNode Singly 연결 목록으로 저장합니다. 즉, 메서드는 ReplaceWith 부모 컨테이너 아래의 직접 자식 노드 목록을 트래버스해야 합니다. 따라서 이 메서드를 사용하면 성능에 영향을 줄 수 있습니다.

이 메서드에 전달할 수 있는 유효한 콘텐츠에 대한 자세한 내용은 XElement 및 XDocument 개체의 유효한 콘텐츠를 참조하세요.

이 메서드는 및 이벤트를 발생 Changed 시킬 Changing 것입니다.

추가 정보

적용 대상