XmlDocument.CreateNode 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.
Membuat sebuah XmlNode.
Overload
| Nama | Deskripsi |
|---|---|
| CreateNode(String, String, String) |
XmlNode Membuat dengan jenis node yang ditentukan, , Namedan NamespaceURI. |
| CreateNode(XmlNodeType, String, String) |
XmlNode Membuat dengan yang ditentukan XmlNodeType, , Namedan NamespaceURI. |
| CreateNode(XmlNodeType, String, String, String) |
XmlNode Membuat dengan yang ditentukan XmlNodeType, , Prefix, Namedan NamespaceURI. |
CreateNode(String, String, String)
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
XmlNode Membuat dengan jenis node yang ditentukan, , Namedan NamespaceURI.
public:
virtual System::Xml::XmlNode ^ CreateNode(System::String ^ nodeTypeString, System::String ^ name, System::String ^ namespaceURI);
public virtual System.Xml.XmlNode CreateNode(string nodeTypeString, string name, string namespaceURI);
public virtual System.Xml.XmlNode CreateNode(string nodeTypeString, string name, string? namespaceURI);
abstract member CreateNode : string * string * string -> System.Xml.XmlNode
override this.CreateNode : string * string * string -> System.Xml.XmlNode
Public Overridable Function CreateNode (nodeTypeString As String, name As String, namespaceURI As String) As XmlNode
Parameter
- nodeTypeString
- String
Versi string dari XmlNodeType simpul baru. Parameter ini harus menjadi salah satu nilai yang tercantum dalam tabel di bawah ini.
- name
- String
Nama yang memenuhi syarat dari simpul baru. Jika nama berisi titik dua, nama tersebut diurai ke dalam Prefix komponen dan LocalName .
- namespaceURI
- String
URI namespace dari simpul baru.
Mengembalikan
Yang baru XmlNode.
Pengecualian
Nama tidak disediakan dan XmlNodeType memerlukan nama; atau nodeTypeString bukan salah satu string yang tercantum di bawah ini.
Contoh
Contoh berikut membuat elemen baru dan menyisipkannya ke dalam dokumen.
using System;
using System.Xml;
public class Sample {
public static void Main() {
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book>" +
" <title>Oberon's Legacy</title>" +
" <price>5.95</price>" +
"</book>");
// Create a new element node.
XmlNode newElem = doc.CreateNode("element", "pages", "");
newElem.InnerText = "290";
Console.WriteLine("Add the new element to the document...");
XmlElement root = doc.DocumentElement;
root.AppendChild(newElem);
Console.WriteLine("Display the modified XML document...");
Console.WriteLine(doc.OuterXml);
}
}
Imports System.Xml
public class Sample
public shared sub Main()
Dim doc as XmlDocument = new XmlDocument()
doc.LoadXml("<book>" & _
" <title>Oberon's Legacy</title>" & _
" <price>5.95</price>" & _
"</book>")
' Create a new element node.
Dim newElem as XmlNode = doc.CreateNode("element", "pages", "")
newElem.InnerText = "290"
Console.WriteLine("Add the new element to the document...")
Dim root as XmlElement = doc.DocumentElement
root.AppendChild(newElem)
Console.WriteLine("Display the modified XML document...")
Console.WriteLine(doc.OuterXml)
end sub
end class
Keterangan
Parameter nodeTypeString peka huruf besar/kecil dan harus menjadi salah satu nilai dalam tabel berikut.
| nodeTypeString | XmlNodeType |
|---|---|
| atribut | Attribute |
| cdatasection | CDATA |
| komentar | Komentar |
| dokumen | Dokumen |
| documentfragment | DocumentFragment |
| jenis dokumen | Jenis Dokumen |
| elemen | Elemen |
| entityreference | EntityReference |
| processinginstruction | Instruksi Pemrosesan |
| signifikanwhitespace | SignificantWhitespace |
| kirim pesan teks | Text |
| spasi kosong | Whitespace |
Meskipun metode ini membuat objek baru dalam konteks dokumen, metode ini tidak secara otomatis menambahkan objek baru ke pohon dokumen. Untuk menambahkan objek baru, Anda harus secara eksplisit memanggil salah satu metode sisipan simpul.
Tabel berikut menunjukkan kepada Anda apa yang diizinkan NodeType[row] di dalam NodeType[column] lain sesuai dengan rekomendasi W3C Extensible Markup Language (XML) 1.0.
| Jenis node | Dokumen | Jenis Dokumen | XmlDeclaration | Elemen | Attribute | Text | CDATA | Markup | EntityReference |
|---|---|---|---|---|---|---|---|---|---|
Document |
no | no | no | no | no | no | no | no | no |
DocumentType |
yes | no | no | no | no | no | no | no | no |
XmlDeclaration |
Ya* | no | no | no | no | no | no | no | no |
Element |
yes | no | no | yes | no | no | no | no | Ya† |
Attribute |
no | no | no | Ya‡ | no | no | no | no | no |
Text |
no | no | no | yes | yes | no | no | no | yes |
CDATA |
no | no | no | yes | no | no | no | no | Ya† |
Markup
§ |
yes | no | no | yes | no | no | no | no | no |
EntityReference |
no | no | no | yes | yes | no | no | no | yes |
* Simpul XmlDeclaration harus menjadi anak pertama dari simpul Dokumen.
Markup§ termasuk ProcessingInstruction dan Comment node.
Element† dan CDATA simpul hanya diizinkan dalam EntityReference simpul ketika EntityReference simpul bukan anak dari simpulAttribute.
‡ Atribut bukan anak dari simpul Element . Atribut terkandung di dalam koleksi atribut milik simpul Element .
Metode ini adalah ekstensi Microsoft ke Model Objek Dokumen (DOM).
Berlaku untuk
CreateNode(XmlNodeType, String, String)
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
XmlNode Membuat dengan yang ditentukan XmlNodeType, , Namedan NamespaceURI.
public:
virtual System::Xml::XmlNode ^ CreateNode(System::Xml::XmlNodeType type, System::String ^ name, System::String ^ namespaceURI);
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string name, string namespaceURI);
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string name, string? namespaceURI);
abstract member CreateNode : System.Xml.XmlNodeType * string * string -> System.Xml.XmlNode
override this.CreateNode : System.Xml.XmlNodeType * string * string -> System.Xml.XmlNode
Public Overridable Function CreateNode (type As XmlNodeType, name As String, namespaceURI As String) As XmlNode
Parameter
- type
- XmlNodeType
Dari XmlNodeType simpul baru.
- name
- String
Nama yang memenuhi syarat dari simpul baru. Jika nama berisi titik dua maka diurai ke dalam Prefix komponen dan LocalName .
- namespaceURI
- String
URI namespace dari simpul baru.
Mengembalikan
Yang baru XmlNode.
Pengecualian
Nama tidak disediakan dan XmlNodeType memerlukan nama.
Contoh
Contoh berikut membuat elemen baru dan menyisipkannya ke dalam dokumen XML.
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");
//Create a new node and add it to the document.
XmlNode elem = doc.CreateNode(XmlNodeType.Element, "price", null);
elem.InnerText = "19.95";
doc.DocumentElement.AppendChild(elem);
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("<book genre='novel' ISBN='1-861001-57-5'>" & _
"<title>Pride And Prejudice</title>" & _
"</book>")
'Create a new node and add it to the document.
Dim elem As XmlNode = doc.CreateNode(XmlNodeType.Element, "price", Nothing)
elem.InnerText = "19.95"
doc.DocumentElement.AppendChild(elem)
Console.WriteLine("Display the modified XML...")
doc.Save(Console.Out)
End Sub
End Class
Keterangan
Meskipun metode ini membuat objek baru dalam konteks dokumen, metode ini tidak secara otomatis menambahkan objek baru ke pohon dokumen. Untuk menambahkan objek baru, Anda harus secara eksplisit memanggil salah satu metode sisipan simpul.
Tabel berikut menunjukkan kepada Anda apa yang diizinkan NodeType[row] di dalam NodeType[column] lain sesuai dengan rekomendasi W3C Extensible Markup Language (XML) 1.0.
| Jenis node | Dokumen | Jenis Dokumen | XmlDeclaration | Elemen | Attribute | Text | CDATA | Markup | EntityReference |
|---|---|---|---|---|---|---|---|---|---|
Document |
no | no | no | no | no | no | no | no | no |
DocumentType |
yes | no | no | no | no | no | no | no | no |
XmlDeclaration |
Ya* | no | no | no | no | no | no | no | no |
Element |
yes | no | no | yes | no | no | no | no | Ya† |
Attribute |
no | no | no | Ya‡ | no | no | no | no | no |
Text |
no | no | no | yes | yes | no | no | no | yes |
CDATA |
no | no | no | yes | no | no | no | no | Ya† |
Markup§ |
yes | no | no | yes | no | no | no | no | no |
EntityReference |
no | no | no | yes | yes | no | no | no | yes |
* Simpul XmlDeclaration harus menjadi anak pertama dari simpul Dokumen.
Markup§ termasuk ProcessingInstruction dan Comment node.
Element† dan CDATA simpul hanya diizinkan dalam EntityReference simpul ketika EntityReference simpul bukan anak dari simpulAttribute.
‡ Atribut bukan anak dari simpul Element . Atribut terkandung di dalam koleksi atribut milik simpul Element .
Metode ini adalah ekstensi Microsoft ke Model Objek Dokumen (DOM).
Berlaku untuk
CreateNode(XmlNodeType, String, String, String)
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
- Sumber:
- XmlDocument.cs
XmlNode Membuat dengan yang ditentukan XmlNodeType, , Prefix, Namedan NamespaceURI.
public:
virtual System::Xml::XmlNode ^ CreateNode(System::Xml::XmlNodeType type, System::String ^ prefix, System::String ^ name, System::String ^ namespaceURI);
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string prefix, string name, string namespaceURI);
public virtual System.Xml.XmlNode CreateNode(System.Xml.XmlNodeType type, string? prefix, string name, string? namespaceURI);
abstract member CreateNode : System.Xml.XmlNodeType * string * string * string -> System.Xml.XmlNode
override this.CreateNode : System.Xml.XmlNodeType * string * string * string -> System.Xml.XmlNode
Public Overridable Function CreateNode (type As XmlNodeType, prefix As String, name As String, namespaceURI As String) As XmlNode
Parameter
- type
- XmlNodeType
Dari XmlNodeType simpul baru.
- prefix
- String
Awalan simpul baru.
- name
- String
Nama lokal simpul baru.
- namespaceURI
- String
URI namespace dari simpul baru.
Mengembalikan
Yang baru XmlNode.
Pengecualian
Nama tidak disediakan dan XmlNodeType memerlukan nama.
Contoh
Contoh berikut menambahkan elemen baru ke dokumen.
using System;
using System.Xml;
public class Sample {
public static void Main() {
// Create a new document containing information about a book
XmlDocument doc = new XmlDocument();
doc.LoadXml("<book>" +
" <title>Oberon's Legacy</title>" +
" <price>5.95</price>" +
"</book>");
// Create a new element node for the ISBN of the book
// It is possible to supply a prefix for this node, and specify a qualified namespace.
XmlNode newElem;
newElem = doc.CreateNode(XmlNodeType.Element, "g", "ISBN", "https://global.ISBN/list");
newElem.InnerText = "1-861001-57-5";
// Add the new element to the document
XmlElement root = doc.DocumentElement;
root.AppendChild(newElem);
// Display the modified XML document
Console.WriteLine(doc.OuterXml);
//Output:
// <book><title>Oberon's Legacy</title><price>5.95</price><g:ISBN xmlns:g="https://global.ISBN/list">1-861001-57-5</g:ISBN></book>
}
}
Imports System.Xml
public class Sample
public shared sub Main()
Dim doc as XmlDocument = new XmlDocument()
doc.LoadXml("<book>" & _
" <title>Oberon's Legacy</title>" & _
" <price>5.95</price>" & _
"</book>")
' Create a new element node.
' It is possible to supply a prefix for this node, and specify a qualified namespace
Dim newElem as XmlNode
newElem = doc.CreateNode(XmlNodeType.Element,"g", "ISBN","https://global.ISBN/list")
newElem.InnerText = "1-861001-57-5"
' Add the new element to the document
Dim root as XmlElement = doc.DocumentElement
root.AppendChild(newElem)
' Display the modified XML document
Console.WriteLine(doc.OuterXml)
' Output:
' <book><title>Oberon's Legacy</title><price>5.95</price><g:ISBN xmlns:g="https://global.ISBN/list">1-861001-57-5</g:ISBN></book>
end sub
end class
Keterangan
Meskipun metode ini membuat objek baru dalam konteks dokumen, metode ini tidak secara otomatis menambahkan objek baru ke pohon dokumen. Untuk menambahkan objek baru, Anda harus secara eksplisit memanggil salah satu metode sisipan simpul.
Tabel berikut menunjukkan kepada Anda apa yang diizinkan NodeType[row] di dalam NodeType[column] lain sesuai dengan rekomendasi W3C Extensible Markup Language (XML) 1.0.
| Dokumen | Jenis Dokumen | XmlDeclaration | Elemen | Attribute | Text | CDATA | Markup | EntityReference | |
|---|---|---|---|---|---|---|---|---|---|
Document |
no | no | no | no | no | no | no | no | no |
DocumentType |
yes | no | no | no | no | no | no | no | no |
XmlDeclaration |
Ya* | no | no | no | no | no | no | no | no |
Element |
yes | no | no | yes | no | no | no | no | Ya† |
Attribute |
no | no | no | Ya‡ | no | no | no | no | no |
Text |
no | no | no | yes | yes | no | no | no | yes |
CDATA |
no | no | no | yes | no | no | no | no | Ya† |
Markup§ |
yes | no | no | yes | no | no | no | no | no |
EntityReference |
no | no | no | yes | yes | no | no | no | yes |
* Simpul XmlDeclaration harus menjadi anak pertama dari simpul Dokumen.
Markup§ termasuk ProcessingInstruction dan Comment node.
Element† dan CDATA simpul hanya diizinkan dalam EntityReference simpul ketika EntityReference simpul bukan anak dari simpulAttribute.
‡ Atribut bukan anak dari simpul Element . Atribut terkandung di dalam koleksi atribut milik simpul Element .
Metode ini adalah ekstensi Microsoft ke Model Objek Dokumen (DOM).