Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>
23 changes: 6 additions & 17 deletions snippets/csharp/System.Xml/XmlDocument/CreateElement/source.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
// <Snippet1>
using System;
using System.IO;
using System.Xml;

public class Sample
{
public static void Main()
public static void CreateTextNodeExample()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
// Create the XmlDocument.
XmlDocument doc = new();
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.
// 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);
Expand All @@ -24,14 +23,4 @@ public static void Main()
doc.Save(Console.Out);
}
}
/*
The example displays the following output:

Display the modified XML...
<?xml version="1.0" encoding="us-ascii"?>
<book genre="novel" ISBN="1-861001-57-5">
<title>Pride And Prejudice</title>
<price>19.95</price>
</book>
*/
// </Snippet1>
// </Snippet1>
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
using System.IO;
using System.Xml;

public class Sample {

public static void Main() {

public class Sample1
{
public static void CreateElementExample()
{
// Create the XmlDocument.
XmlDocument doc = new XmlDocument();
XmlDocument doc = new();
string xmlData = "<book xmlns:bk='urn:samples'></book>";

doc.Load(new StringReader(xmlData));
Expand All @@ -22,4 +22,4 @@ public static void Main() {
doc.Save(Console.Out);
}
}
// </Snippet1>
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
' <Snippet1>
Option Explicit
Option Strict

' <Snippet1>
Imports System.IO
Imports System.Xml

Public Class Sample

Public Shared Sub Main()
'Create the XmlDocument.

' 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.
'The text node is the content of the price element.
' Create a new node and add it to the document.
' The text node is the content of the price element.
Dim elem As XmlElement = doc.CreateElement("price")
Dim text As XmlText = doc.CreateTextNode("19.95")
doc.DocumentElement.AppendChild(elem)
doc.DocumentElement.LastChild.AppendChild(text)

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

End Sub
End Class
' The example displays the following output:
'
' Display the modified XML...
' <?xml version="1.0" encoding="utf-8"?>
' <book genre="novel" ISBN="1-861001-57-5">
' <title>Pride And Prejudice</title>
' <price>19.95</price>
' </book>
' </Snippet1>
22 changes: 17 additions & 5 deletions xml/System.Xml/XmlDocument.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,17 @@ The following example creates a new element and adds it to the document.
[!code-csharp[Classic WebData XmlDocument.CreateElement Example#1](~/snippets/csharp/System.Xml/XmlDocument/CreateElement/source.cs#1)]
[!code-vb[Classic WebData XmlDocument.CreateElement Example#1](~/snippets/visualbasic/VS_Snippets_Data/Classic WebData XmlDocument.CreateElement Example/VB/source.vb#1)]
The example produces the following output:
```output
Display the modified XML...
<?xml version="1.0" encoding="utf-8"?>
<book genre="novel" ISBN="1-861001-57-5">
<title>Pride And Prejudice</title>
<price>19.95</price>
</book>
```
]]></format>
</remarks>
</Docs>
Expand Down Expand Up @@ -1986,21 +1997,22 @@ The following example adds significant white space to the document.
<Docs>
<param name="text">The text for the Text node.</param>
<summary>Creates an <see cref="T:System.Xml.XmlText" /> with the specified text.</summary>
<returns>The new <see langword="XmlText" /> node.</returns>
<returns>The new node.</returns>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
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](https://www.w3.org/TR/1998/REC-xml-19980210), Text nodes are only allowed within Element, Attribute and EntityReference nodes.
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](https://www.w3.org/TR/1998/REC-xml-19980210), Text nodes are only allowed within Element, Attribute and EntityReference nodes.
## Examples
The following example creates a new element and adds it to the document.
[!code-csharp[Classic WebData XmlDocument.CreateElement Example#1](~/snippets/csharp/System.Xml/XmlDocument/CreateElement/source.cs#1)]
[!code-vb[Classic WebData XmlDocument.CreateElement Example#1](~/snippets/visualbasic/VS_Snippets_Data/Classic WebData XmlDocument.CreateElement Example/VB/source.vb#1)]
:::code language="csharp" source="~/snippets/csharp/System.Xml/XmlDocument/CreateElement/source.cs" id="Snippet1":::
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_Data/Classic WebData XmlDocument.CreateElement Example/VB/source.vb" id="Snippet1":::
]]></format>
</remarks>
Expand Down