XPathNavigator.SetValue(String) 메서드

정의

현재 노드의 값을 설정합니다.

public:
 virtual void SetValue(System::String ^ value);
public virtual void SetValue (string value);
abstract member SetValue : string -> unit
override this.SetValue : string -> unit
Public Overridable Sub SetValue (value As String)

매개 변수

value
String

노드의 새 값

예외

value 매개 변수가 null입니다.

XPathNavigator가 루트 노드 또는 네임스페이스 노드에 있거나 지정된 값이 잘못되었습니다.

XPathNavigator에서는 편집을 지원하지 않습니다.

예제

다음 예제 SetValue 에서 메서드는 XPath 식의 contosoBooks.xml 네임스페이스 접두사를 확인하기 위해 개체를 사용하여 XmlNamespaceManager 파일의 모든 price 요소를 업데이트하는 데 사용됩니다.

XmlDocument^ document = gcnew XmlDocument();
document->Load("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XmlNamespaceManager^ manager = gcnew XmlNamespaceManager(navigator->NameTable);
manager->AddNamespace("bk", "http://www.contoso.com/books");

for each (XPathNavigator^ nav in navigator->Select("//bk:price", manager))
{
    if(nav->Value == "11.99")
    {
        nav->SetValue("12.99");
    }
}

Console::WriteLine(navigator->OuterXml);
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");

foreach (XPathNavigator nav in navigator.Select("//bk:price", manager))
{
    if (nav.Value == "11.99")
    {
        nav.SetValue("12.99");
    }
}

Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
manager.AddNamespace("bk", "http://www.contoso.com/books")

For Each nav As XPathNavigator In navigator.Select("//bk:price", manager)
    If nav.Value = "11.99" Then
        nav.SetValue("12.99")
    End If
Next

Console.WriteLine(navigator.OuterXml)

이 예제에서는 contosoBooks.xml 파일을 입력으로 사용합니다.

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

설명

  • XPathNavigator 가 복합 콘텐츠(자식 요소)가 있는 요소 노드에 있는 경우 메서드를 SetValue 호출하면 자식 요소가 새 문자열 값이 포함된 텍스트 노드로 바뀝니다.

  • 메서드는 SetValue 루트 노드 또는 네임스페이스 노드를 업데이트할 수 없습니다.

적용 대상