Прочетете на английски Редактиране

Споделяне чрез


XmlDocument.CreateElement Method

Definition

Creates an XmlElement.

Overloads

CreateElement(String)

Creates an element with the specified name.

CreateElement(String, String)

Creates an XmlElement with the qualified name and NamespaceURI.

CreateElement(String, String, String)

Creates an element with the specified Prefix, LocalName, and NamespaceURI.

CreateElement(String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Creates an element with the specified name.

C#
public System.Xml.XmlElement CreateElement(string name);

Parameters

name
String

The qualified name of the element. If the name contains a colon then the Prefix property reflects the part of the name preceding the colon and the LocalName property reflects the part of the name after the colon. The qualified name cannot include a prefix of 'xmlns'.

Returns

The new XmlElement.

Examples

The following example creates a new element and adds it to the document.

C#
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.
    //The text node is the content of the price element.
    XmlElement elem = doc.CreateElement("price");
    XmlText text = doc.CreateTextNode("19.95");
    doc.DocumentElement.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(text);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}

Remarks

Note that the instance returned implements the XmlElement interface, so default attributes would be created directly on the returned object.

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

CreateElement(String, String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Creates an XmlElement with the qualified name and NamespaceURI.

C#
public System.Xml.XmlElement CreateElement(string qualifiedName, string namespaceURI);
C#
public System.Xml.XmlElement CreateElement(string qualifiedName, string? namespaceURI);

Parameters

qualifiedName
String

The qualified name of the element. If the name contains a colon then the Prefix property will reflect the part of the name preceding the colon and the LocalName property will reflect the part of the name after the colon. The qualified name cannot include a prefix of 'xmlns'.

namespaceURI
String

The namespace URI of the element.

Returns

The new XmlElement.

Remarks

The following C# code

C#
XmlElement elem;
elem=doc.CreateElement("xy:item", "urn:abc");

results in an element that is equivalent to the following XML text.

<xy:item
       xmlns:xy="urn:abc"/>

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference node is not a child of an Attribute node.

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

CreateElement(String, String, String)

Source:
XmlDocument.cs
Source:
XmlDocument.cs
Source:
XmlDocument.cs

Creates an element with the specified Prefix, LocalName, and NamespaceURI.

C#
public virtual System.Xml.XmlElement CreateElement(string prefix, string localName, string namespaceURI);
C#
public virtual System.Xml.XmlElement CreateElement(string? prefix, string localName, string? namespaceURI);

Parameters

prefix
String

The prefix of the new element (if any). String.Empty and null are equivalent.

localName
String

The local name of the new element.

namespaceURI
String

The namespace URI of the new element (if any). String.Empty and null are equivalent.

Returns

The new XmlElement.

Examples

The following example adds a new element to the existing XML document.

C#
using System;
using System.IO;
using System.Xml;

public class Sample {

  public static void Main() {

    // Create the XmlDocument.
    XmlDocument doc = new XmlDocument();
    string xmlData = "<book xmlns:bk='urn:samples'></book>";

    doc.Load(new StringReader(xmlData));

    // Create a new element and add it to the document.
    XmlElement elem = doc.CreateElement("bk", "genre", "urn:samples");
    elem.InnerText = "fantasy";
    doc.DocumentElement.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}

Remarks

The following C# code

C#
XmlElement elem;
elem=doc.CreateElement("xy", "item", "urn:abc");

creates an element equivalent to the following XML text:

<xy:item xmlns:xy="urn:abc"/>

Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. To add the new object, you must explicitly call one of the node insert methods.

According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Element nodes are allowed within Document and Element nodes, and in EntityReference nodes when the EntityReference is outside an Attribute node.

This method is a Microsoft extension to the Document Object Model (DOM).

Applies to

.NET 10 и други версии
Продукт Версии
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0