XPathNavigator.SetTypedValue(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
設定目前節點的類型值。
public:
virtual void SetTypedValue(System::Object ^ typedValue);
public virtual void SetTypedValue (object typedValue);
abstract member SetTypedValue : obj -> unit
override this.SetTypedValue : obj -> unit
Public Overridable Sub SetTypedValue (typedValue As Object)
參數
- typedValue
- Object
節點的新的型別值。
例外狀況
XPathNavigator 不支援指定物件的型別。
指定的值不能是 null
。
XPathNavigator 不是位於項目或屬性節點上。
範例
在下列範例中 SetTypedValue ,方法是用來使用 XmlNamespaceManager 物件來更新檔案中的所有 price
contosoBooks.xml
專案,以解析 XPath 運算式中的命名空間前置詞。
XmlReaderSettings^ settings = gcnew XmlReaderSettings();
settings->Schemas->Add("http://www.contoso.com/books", "contosoBooks.xsd");
settings->ValidationType = ValidationType::Schema;
XmlReader^ reader = XmlReader::Create("contosoBooks.xml", settings);
XmlDocument^ document = gcnew XmlDocument();
document->Load(reader);
XPathNavigator^ navigator = document->CreateNavigator();
navigator->MoveToChild("bookstore", "http://www.contoso.com/books");
navigator->MoveToChild("book", "http://www.contoso.com/books");
navigator->MoveToChild("price", "http://www.contoso.com/books");
Decimal^ price = gcnew Decimal(19.99);
navigator->SetTypedValue(price);
navigator->MoveToParent();
Console::WriteLine(navigator->OuterXml);
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");
settings.ValidationType = ValidationType.Schema;
XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);
XmlDocument document = new XmlDocument();
document.Load(reader);
XPathNavigator navigator = document.CreateNavigator();
navigator.MoveToChild("bookstore", "http://www.contoso.com/books");
navigator.MoveToChild("book", "http://www.contoso.com/books");
navigator.MoveToChild("price", "http://www.contoso.com/books");
Decimal price = 19.99M;
navigator.SetTypedValue(price);
navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd")
settings.ValidationType = ValidationType.Schema
Dim reader As XmlReader = XmlReader.Create("contosoBooks.xml", settings)
Dim document As XmlDocument = New XmlDocument()
document.Load(reader)
Dim navigator As XPathNavigator = document.CreateNavigator()
navigator.MoveToChild("bookstore", "http://www.contoso.com/books")
navigator.MoveToChild("book", "http://www.contoso.com/books")
navigator.MoveToChild("price", "http://www.contoso.com/books")
Dim price As New Decimal(19.99)
navigator.SetTypedValue(price)
navigator.MoveToParent()
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>
備註
XmlDocument當物件建立為架構驗證 XmlReader 物件的結果時,會根據 XML 檔的 XML 架構定義語言 (XSD) 架構來驗證方法所指定的 SetTypedValue 值。 如果指定的值根據 XML 檔的架構無效, XmlSchemaException 則會擲回 。
只有在 SetTypedValue 位於專案或屬性節點上的 時 XPathNavigator ,方法才有效。