XPathNavigator.InsertBefore 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在目前選擇的節點之前建立一個新的兄弟節點。
多載
| 名稱 | Description |
|---|---|
| InsertBefore() |
回傳 XmlWriter 一個物件,用於在目前選擇的節點之前建立新的兄弟節點。 |
| InsertBefore(String) |
利用指定的 XML 字串,在目前選擇的節點前建立一個新的兄弟節點。 |
| InsertBefore(XmlReader) |
利用指定物件的 XmlReader XML 內容,在目前選擇的節點前建立一個新的兄弟節點。 |
| InsertBefore(XPathNavigator) |
在目前選擇的節點之前,使用指定的節點 XPathNavigator 建立一個新的兄弟節點。 |
InsertBefore()
回傳 XmlWriter 一個物件,用於在目前選擇的節點之前建立新的兄弟節點。
public:
virtual System::Xml::XmlWriter ^ InsertBefore();
public virtual System.Xml.XmlWriter InsertBefore();
abstract member InsertBefore : unit -> System.Xml.XmlWriter
override this.InsertBefore : unit -> System.Xml.XmlWriter
Public Overridable Function InsertBefore () As XmlWriter
傳回
XmlWriter一個物件,用於在當前選擇節點之前建立新的兄弟節點。
例外狀況
該位置 XPathNavigator 不允許在當前節點前插入新的兄弟節點。
它 XPathNavigator 不支援編輯。
範例
在以下範例中,使用方法回傳pages的物件,在檔案price中book第一個contosoBooks.xml元素的子元素之前插入XmlWriter一個新InsertBefore元素。
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
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");
XmlWriter pages = navigator.InsertBefore();
pages.WriteElementString("pages", "100");
pages.Close();
navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
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 pages As XmlWriter = navigator.InsertBefore()
pages.WriteElementString("pages", "100")
pages.Close()
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>
備註
以下是使用此 InsertBefore 方法時需注意的重要事項。
此 InsertBefore 方法僅在 被 XPathNavigator 定位於元素、文字、處理指令或註解節點時有效。
此 InsertBefore 方法不影響 的位置 XPathNavigator。
適用於
InsertBefore(String)
利用指定的 XML 字串,在目前選擇的節點前建立一個新的兄弟節點。
public:
virtual void InsertBefore(System::String ^ newSibling);
public virtual void InsertBefore(string newSibling);
abstract member InsertBefore : string -> unit
override this.InsertBefore : string -> unit
Public Overridable Sub InsertBefore (newSibling As String)
參數
- newSibling
- String
新兄弟節點的 XML 資料字串。
例外狀況
XML 字串參數為 null。
該位置 XPathNavigator 不允許在當前節點前插入新的兄弟節點。
它 XPathNavigator 不支援編輯。
XML 字串參數並非良好構想。
範例
在下列範例中,會在pages檔案中第一price個元素的子元素book之前插入新的contosoBooks.xml元素。
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
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");
navigator.InsertBefore("<pages>100</pages>");
navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
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")
navigator.InsertBefore("<pages>100</pages>")
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>
備註
要建立新元素節點,請將所有 XML 語法納入 XML 字串參數中。 新 book 節點的字串為 InsertBefore("<book/>")。 在當前節點的文字節點前插入文字「book」的字串為 InsertBefore("book")。 如果 XML 字串包含多個節點,則所有節點都會被加入。
以下是使用此 InsertBefore 方法時需注意的重要事項。
此 InsertBefore 方法僅在 被 XPathNavigator 定位於元素、文字、處理指令或註解節點時有效。
此 InsertBefore 方法不影響 的位置 XPathNavigator。
適用於
InsertBefore(XmlReader)
利用指定物件的 XmlReader XML 內容,在目前選擇的節點前建立一個新的兄弟節點。
public:
virtual void InsertBefore(System::Xml::XmlReader ^ newSibling);
public virtual void InsertBefore(System.Xml.XmlReader newSibling);
abstract member InsertBefore : System.Xml.XmlReader -> unit
override this.InsertBefore : System.Xml.XmlReader -> unit
Public Overridable Sub InsertBefore (newSibling As XmlReader)
參數
例外狀況
該 XmlReader 物件處於錯誤狀態或關閉狀態。
XmlReader物件參數為 null。
該位置 XPathNavigator 不允許在當前節點前插入新的兄弟節點。
它 XPathNavigator 不支援編輯。
物件參數的 XmlReader XML 內容並非良好格式。
範例
以下範例中,使用指定的物件在檔案pages中第一個price元素book的子元素之前插入contosoBooks.xml一個新XmlReader元素。
http://www.contoso.com/books命名空間的指定是為了讓新的兄弟元素插入時,使用與 XML 文件相同的命名空間。
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
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");
XmlReader pages = XmlReader.Create(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
navigator.InsertBefore(pages);
navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
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 pages As XmlReader = XmlReader.Create(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))
navigator.InsertBefore(pages)
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>
備註
以下是使用此 InsertBefore 方法時需注意的重要事項。
如果 XmlReader 物件位於一串 XML 節點上,則該序列中的所有節點都會被加入。
此 InsertBefore 方法僅在 被 XPathNavigator 定位於元素、文字、處理指令或註解節點時有效。
此 InsertBefore 方法不影響 的位置 XPathNavigator。
適用於
InsertBefore(XPathNavigator)
在目前選擇的節點之前,使用指定的節點 XPathNavigator 建立一個新的兄弟節點。
public:
virtual void InsertBefore(System::Xml::XPath::XPathNavigator ^ newSibling);
public virtual void InsertBefore(System.Xml.XPath.XPathNavigator newSibling);
abstract member InsertBefore : System.Xml.XPath.XPathNavigator -> unit
override this.InsertBefore : System.Xml.XPath.XPathNavigator -> unit
Public Overridable Sub InsertBefore (newSibling As XPathNavigator)
參數
- newSibling
- XPathNavigator
XPathNavigator一個放置在節點上,作為新增的兄弟節點的物件。
例外狀況
XPathNavigator物件參數為 null。
該位置 XPathNavigator 不允許在當前節點前插入新的兄弟節點。
它 XPathNavigator 不支援編輯。
範例
以下範例中,使用指定物件中的pages節點,插入一個新price元素,置於book檔案中contosoBooks.xml第一個XPathNavigator元素的子元素之前。
http://www.contoso.com/books命名空間的指定是為了讓新的兄弟元素插入時,使用與 XML 文件相同的命名空間。
XmlDocument document = new XmlDocument();
document.Load("contosoBooks.xml");
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");
XmlDocument childNodes = new XmlDocument();
childNodes.Load(new StringReader("<pages xmlns=\"http://www.contoso.com/books\">100</pages>"));
XPathNavigator childNodesNavigator = childNodes.CreateNavigator();
navigator.InsertBefore(childNodesNavigator);
navigator.MoveToParent();
Console.WriteLine(navigator.OuterXml);
Dim document As XmlDocument = New XmlDocument()
document.Load("contosoBooks.xml")
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 childNodes As XmlDocument = New XmlDocument()
childNodes.Load(New StringReader("<pages xmlns='http://www.contoso.com/books'>100</pages>"))
Dim childNodesNavigator As XPathNavigator = childNodes.CreateNavigator()
navigator.InsertBefore(childNodesNavigator)
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>
備註
以下是使用此 InsertBefore 方法時需注意的重要事項。
如果 XPathNavigator 物件位於一串 XML 節點上,則該序列中的所有節點都會被加入。
此 InsertBefore 方法僅在 被 XPathNavigator 定位於元素、文字、處理指令或註解節點時有效。
此 InsertBefore 方法不影響 的位置 XPathNavigator。