XmlDocument.ReadNode(XmlReader) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
public:
virtual System::Xml::XmlNode ^ ReadNode(System::Xml::XmlReader ^ reader);
public virtual System.Xml.XmlNode ReadNode(System.Xml.XmlReader reader);
public virtual System.Xml.XmlNode? ReadNode(System.Xml.XmlReader reader);
abstract member ReadNode : System.Xml.XmlReader -> System.Xml.XmlNode
override this.ReadNode : System.Xml.XmlReader -> System.Xml.XmlNode
Public Overridable Function ReadNode (reader As XmlReader) As XmlNode
Parameter
- reader
- XmlReader
Sumber XML.
Mengembalikan
Simpul baru XmlNode atau null jika tidak ada lagi.
Pengecualian
Pembaca diposisikan pada jenis node yang tidak diterjemahkan ke simpul DOM yang valid (misalnya, EndElement atau EndEntity).
Contoh
Contoh berikut menggunakan ReadNode untuk membuat simpul baru lalu menyisipkan simpul baru ke dalam dokumen.
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<bookstore>" +
"<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>" +
"</bookstore>");
//Create a reader.
XmlTextReader reader = new XmlTextReader("cd.xml");
reader.MoveToContent(); //Move to the cd element node.
//Create a node representing the cd element node.
XmlNode cd = doc.ReadNode(reader);
//Insert the new node into the document.
doc.DocumentElement.AppendChild(cd);
Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Public Class Sample
Public Shared Sub Main()
'Create the XmlDocument.
Dim doc As New XmlDocument()
doc.LoadXml("<bookstore>" & _
"<book genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"</book>" & _
"</bookstore>")
'Create a reader.
Dim reader As New XmlTextReader("cd.xml")
reader.MoveToContent() 'Move to the cd element node.
'Create a node representing the cd element node.
Dim cd As XmlNode = doc.ReadNode(reader)
'Insert the new node into the document.
doc.DocumentElement.AppendChild(cd)
Console.WriteLine("Display the modified XML...")
doc.Save(Console.Out)
End Sub
End Class
Contohnya menggunakan file, cd.xml, sebagai input.
<!-- sample CD -->
<cd genre='alternative'>
<title>Americana</title>
<artist>Offspring</artist>
</cd>
Keterangan
Membaca satu XmlNode dari pembaca yang diberikan dan memposisikan pembaca pada simpul berikutnya. Metode ini membuat jenis XmlNode pencocokan NodeType tempat pembaca saat ini diposisikan. (Jika pembaca berada dalam status awal, ReadNode memajukan pembaca ke simpul pertama dan kemudian beroperasi pada simpul tersebut.)
Jika pembaca diposisikan pada awal elemen, ReadNode membaca semua atribut dan simpul anak apa pun, hingga dan menyertakan tag akhir dari simpul saat ini. Yang XmlNode dikembalikan berisi sub-pohon yang mewakili semuanya yang dibaca. Pembaca diposisikan segera setelah tag akhir.
ReadNode juga dapat membaca atribut, tetapi dalam hal ini tidak memajukan pembaca ke atribut berikutnya. Ini memungkinkan Anda untuk menulis kode C# berikut:
XmlDocument doc = new XmlDocument();
while (reader.MoveToNextAttribute())
{
XmlNode a = doc.ReadNode(reader);
// Do some more processing.
}
ReadNode mengonsumsi nilai atribut sekalipun, yang berarti setelah memanggil ReadNode pada atribut, XmlReader.ReadAttributeValue mengembalikan false.
Catatan Bagi Inheritor
Metode ini memiliki permintaan warisan. Kepercayaan penuh diperlukan untuk mengambil ReadNode alih metode.
Metode ini adalah ekstensi Microsoft ke Model Objek Dokumen (DOM).