XmlDocument.CreateNode Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Skapar en XmlNode.
Överlagringar
| Name | Description |
|---|---|
| CreateNode(String, String, String) |
Skapar en XmlNode med den angivna nodtypen , Nameoch NamespaceURI. |
| CreateNode(XmlNodeType, String, String) |
Skapar ett XmlNode med angivet XmlNodeType, Name, och NamespaceURI. |
| CreateNode(XmlNodeType, String, String, String) |
Skapar en XmlNode med angivet XmlNodeType, Prefix, Nameoch NamespaceURI. |
CreateNode(String, String, String)
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
Skapar en XmlNode med den angivna nodtypen , Nameoch 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
Parametrar
- nodeTypeString
- String
Strängversion av den XmlNodeType nya noden. Den här parametern måste vara ett av de värden som anges i tabellen nedan.
- name
- String
Det kvalificerade namnet på den nya noden. Om namnet innehåller ett kolon parsas det i Prefix och LocalName komponenter.
- namespaceURI
- String
Namnområdes-URI:n för den nya noden.
Returer
Den nya XmlNode.
Undantag
Namnet angavs inte och XmlNodeType kräver ett namn, eller nodeTypeString är inte en av strängarna som anges nedan.
Exempel
I följande exempel skapas ett nytt element och infogas i dokumentet.
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
Kommentarer
Parametern nodeTypeString är skiftlägeskänslig och måste vara ett av värdena i följande tabell.
| nodeTypeString | XmlNodeType |
|---|---|
| attribut | Attribute |
| cdatasection | CDATA |
| kommentar | Comment |
| dokument | Dokument |
| dokumentfragment | DocumentFragment |
| documenttype | Dokumenttyp |
| element | Element |
| entityreference | EntityReference |
| processinginstruction | bearbetningsinstruktion |
| significantwhitespace | SignificantWhitespace |
| text | Text |
| tomt utrymme | Whitespace |
Även om den här metoden skapar det nya objektet i dokumentets kontext, lägger det inte automatiskt till det nya objektet i dokumentträdet. Om du vill lägga till det nya objektet måste du uttryckligen anropa någon av nodinfogningsmetoderna.
I följande tabell visas vad NodeType[row] tillåts i en annan NodeType[kolumn] enligt rekommendationen W3C Extensible Markup Language (XML) 1.0.
| Nodtyp | Dokument | Dokumenttyp | XML-deklaration | Element | Attribute | Text | CDATA | Markup-språk | EntityReference |
|---|---|---|---|---|---|---|---|---|---|
Document |
Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej |
DocumentType |
Ja | Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej |
XmlDeclaration |
Ja* | Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej |
Element |
Ja | Nej | Nej | Ja | Nej | Nej | Nej | Nej | Ja† |
Attribute |
Nej | Nej | Nej | Ja‡ | Nej | Nej | Nej | Nej | Nej |
Text |
Nej | Nej | Nej | Ja | Ja | Nej | Nej | Nej | Ja |
CDATA |
Nej | Nej | Nej | Ja | Nej | Nej | Nej | Nej | Ja† |
Markup
§ |
Ja | Nej | Nej | Ja | Nej | Nej | Nej | Nej | Nej |
EntityReference |
Nej | Nej | Nej | Ja | Ja | Nej | Nej | Nej | Ja |
* Noden XmlDeclaration måste vara den första underordnade av dokumentnoden.
§ Markup innehåller ProcessingInstruction och Comment noder.
Element† och CDATA noder tillåts endast i EntityReference noder när noden inte är underordnad EntityReference en Attribute nod.
‡ Attribut är inte underordnade till en Element nod. Attribut finns i en attributsamling som tillhör en Element nod.
Den här metoden är ett Microsoft-tillägg till dokumentobjektmodellen (DOM).
Gäller för
CreateNode(XmlNodeType, String, String)
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
Skapar ett XmlNode med angivet XmlNodeType, Name, och 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
Parametrar
- type
- XmlNodeType
Den XmlNodeType nya nodens.
- name
- String
Det kvalificerade namnet på den nya noden. Om namnet innehåller ett kolon parsas det i Prefix och LocalName komponenter.
- namespaceURI
- String
Namnområdes-URI:n för den nya noden.
Returer
Den nya XmlNode.
Undantag
Namnet angavs inte och XmlNodeType kräver ett namn.
Exempel
I följande exempel skapas ett nytt element och infogas i ett XML-dokument.
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
Kommentarer
Även om den här metoden skapar det nya objektet i dokumentets kontext, lägger det inte automatiskt till det nya objektet i dokumentträdet. Om du vill lägga till det nya objektet måste du uttryckligen anropa någon av nodinfogningsmetoderna.
I följande tabell visas vad NodeType[row] tillåts i en annan NodeType[kolumn] enligt rekommendationen W3C Extensible Markup Language (XML) 1.0.
| Nodtyp | Dokument | Dokumenttyp | XML-deklaration | Element | Attribute | Text | CDATA | Markup-språk | EntityReference |
|---|---|---|---|---|---|---|---|---|---|
Document |
Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej |
DocumentType |
Ja | Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej |
XmlDeclaration |
Ja* | Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej |
Element |
Ja | Nej | Nej | Ja | Nej | Nej | Nej | Nej | Ja† |
Attribute |
Nej | Nej | Nej | Ja‡ | Nej | Nej | Nej | Nej | Nej |
Text |
Nej | Nej | Nej | Ja | Ja | Nej | Nej | Nej | Ja |
CDATA |
Nej | Nej | Nej | Ja | Nej | Nej | Nej | Nej | Ja† |
Markup§ |
Ja | Nej | Nej | Ja | Nej | Nej | Nej | Nej | Nej |
EntityReference |
Nej | Nej | Nej | Ja | Ja | Nej | Nej | Nej | Ja |
* Noden XmlDeclaration måste vara den första underordnade av dokumentnoden.
§ Markup innehåller ProcessingInstruction och Comment noder.
Element† och CDATA noder tillåts endast i EntityReference noder när noden inte är underordnad EntityReference en Attribute nod.
‡ Attribut är inte underordnade till en Element nod. Attribut finns i en attributsamling som tillhör en Element nod.
Den här metoden är ett Microsoft-tillägg till dokumentobjektmodellen (DOM).
Gäller för
CreateNode(XmlNodeType, String, String, String)
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
- Källa:
- XmlDocument.cs
Skapar en XmlNode med angivet XmlNodeType, Prefix, Nameoch 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
Parametrar
- type
- XmlNodeType
Den XmlNodeType nya nodens.
- prefix
- String
Prefixet för den nya noden.
- name
- String
Det lokala namnet på den nya noden.
- namespaceURI
- String
Namnområdes-URI:n för den nya noden.
Returer
Den nya XmlNode.
Undantag
Namnet angavs inte och XmlNodeType kräver ett namn.
Exempel
I följande exempel läggs ett nytt element till i dokumentet.
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
Kommentarer
Även om den här metoden skapar det nya objektet i dokumentets kontext, lägger det inte automatiskt till det nya objektet i dokumentträdet. Om du vill lägga till det nya objektet måste du uttryckligen anropa någon av nodinfogningsmetoderna.
I följande tabell visas vad NodeType[row] tillåts i en annan NodeType[kolumn] enligt rekommendationen W3C Extensible Markup Language (XML) 1.0.
| Dokument | Dokumenttyp | XML-deklaration | Element | Attribute | Text | CDATA | Markup-språk | EntityReference | |
|---|---|---|---|---|---|---|---|---|---|
Document |
Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej |
DocumentType |
Ja | Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej |
XmlDeclaration |
Ja* | Nej | Nej | Nej | Nej | Nej | Nej | Nej | Nej |
Element |
Ja | Nej | Nej | Ja | Nej | Nej | Nej | Nej | Ja† |
Attribute |
Nej | Nej | Nej | Ja‡ | Nej | Nej | Nej | Nej | Nej |
Text |
Nej | Nej | Nej | Ja | Ja | Nej | Nej | Nej | Ja |
CDATA |
Nej | Nej | Nej | Ja | Nej | Nej | Nej | Nej | Ja† |
Markup§ |
Ja | Nej | Nej | Ja | Nej | Nej | Nej | Nej | Nej |
EntityReference |
Nej | Nej | Nej | Ja | Ja | Nej | Nej | Nej | Ja |
* XmlDeclaration-noden måste vara den första underordnade i dokumentnoden.
§ Markup innehåller ProcessingInstruction och Comment noder.
Element† och CDATA noder tillåts endast i EntityReference noder när noden inte är underordnad EntityReference en Attribute nod.
‡ Attribut är inte underordnade till en Element nod. Attribut finns i en attributsamling som tillhör en Element nod.
Den här metoden är ett Microsoft-tillägg till dokumentobjektmodellen (DOM).