Skip to content

Commit cbbccfa

Browse files
authored
Try to fix output display (#11758)
1 parent d0d959c commit cbbccfa

File tree

6 files changed

+51
-42
lines changed

6 files changed

+51
-42
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
// <Snippet1>
22
using System;
3-
using System.IO;
43
using System.Xml;
54

65
public class Sample
76
{
8-
public static void Main()
7+
public static void CreateTextNodeExample()
98
{
10-
//Create the XmlDocument.
11-
XmlDocument doc = new XmlDocument();
9+
// Create the XmlDocument.
10+
XmlDocument doc = new();
1211
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
1312
"<title>Pride And Prejudice</title>" +
1413
"</book>");
1514

16-
//Create a new node and add it to the document.
17-
//The text node is the content of the price element.
15+
// Create a new node and add it to the document.
16+
// The text node is the content of the price element.
1817
XmlElement elem = doc.CreateElement("price");
1918
XmlText text = doc.CreateTextNode("19.95");
2019
doc.DocumentElement.AppendChild(elem);
@@ -24,14 +23,4 @@ public static void Main()
2423
doc.Save(Console.Out);
2524
}
2625
}
27-
/*
28-
The example displays the following output:
29-
30-
Display the modified XML...
31-
<?xml version="1.0" encoding="us-ascii"?>
32-
<book genre="novel" ISBN="1-861001-57-5">
33-
<title>Pride And Prejudice</title>
34-
<price>19.95</price>
35-
</book>
36-
*/
37-
// </Snippet1>
26+
// </Snippet1>

snippets/csharp/System.Xml/XmlDocument/CreateElement/source1.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
using System.IO;
44
using System.Xml;
55

6-
public class Sample {
7-
8-
public static void Main() {
9-
6+
public class Sample1
7+
{
8+
public static void CreateElementExample()
9+
{
1010
// Create the XmlDocument.
11-
XmlDocument doc = new XmlDocument();
11+
XmlDocument doc = new();
1212
string xmlData = "<book xmlns:bk='urn:samples'></book>";
1313

1414
doc.Load(new StringReader(xmlData));
@@ -22,4 +22,4 @@ public static void Main() {
2222
doc.Save(Console.Out);
2323
}
2424
}
25-
// </Snippet1>
25+
// </Snippet1>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
1-
' <Snippet1>
21
Option Explicit
32
Option Strict
4-
3+
' <Snippet1>
54
Imports System.IO
65
Imports System.Xml
76

87
Public Class Sample
9-
108
Public Shared Sub Main()
11-
'Create the XmlDocument.
9+
10+
' Create the XmlDocument.
1211
Dim doc As New XmlDocument()
1312
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
1413
"<title>Pride And Prejudice</title>" & _
1514
"</book>")
1615

17-
'Create a new node and add it to the document.
18-
'The text node is the content of the price element.
16+
' Create a new node and add it to the document.
17+
' The text node is the content of the price element.
1918
Dim elem As XmlElement = doc.CreateElement("price")
2019
Dim text As XmlText = doc.CreateTextNode("19.95")
2120
doc.DocumentElement.AppendChild(elem)
2221
doc.DocumentElement.LastChild.AppendChild(text)
2322

2423
Console.WriteLine("Display the modified XML...")
2524
doc.Save(Console.Out)
25+
2626
End Sub
2727
End Class
28-
' The example displays the following output:
29-
'
30-
' Display the modified XML...
31-
' <?xml version="1.0" encoding="utf-8"?>
32-
' <book genre="novel" ISBN="1-861001-57-5">
33-
' <title>Pride And Prejudice</title>
34-
' <price>19.95</price>
35-
' </book>
3628
' </Snippet1>

xml/System.Xml/XmlDocument.xml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,17 @@ The following example creates a new element and adds it to the document.
10291029
[!code-csharp[Classic WebData XmlDocument.CreateElement Example#1](~/snippets/csharp/System.Xml/XmlDocument/CreateElement/source.cs#1)]
10301030
[!code-vb[Classic WebData XmlDocument.CreateElement Example#1](~/snippets/visualbasic/VS_Snippets_Data/Classic WebData XmlDocument.CreateElement Example/VB/source.vb#1)]
10311031
1032+
The example produces the following output:
1033+
1034+
```output
1035+
Display the modified XML...
1036+
<?xml version="1.0" encoding="utf-8"?>
1037+
<book genre="novel" ISBN="1-861001-57-5">
1038+
<title>Pride And Prejudice</title>
1039+
<price>19.95</price>
1040+
</book>
1041+
```
1042+
10321043
]]></format>
10331044
</remarks>
10341045
</Docs>
@@ -1986,21 +1997,22 @@ The following example adds significant white space to the document.
19861997
<Docs>
19871998
<param name="text">The text for the Text node.</param>
19881999
<summary>Creates an <see cref="T:System.Xml.XmlText" /> with the specified text.</summary>
1989-
<returns>The new <see langword="XmlText" /> node.</returns>
2000+
<returns>The new node.</returns>
19902001
<remarks>
19912002
<format type="text/markdown"><![CDATA[
19922003
19932004
## Remarks
1994-
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.
19952005
1996-
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.
2006+
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.
2007+
2008+
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.
19972009
19982010
## Examples
19992011
20002012
The following example creates a new element and adds it to the document.
20012013
2002-
[!code-csharp[Classic WebData XmlDocument.CreateElement Example#1](~/snippets/csharp/System.Xml/XmlDocument/CreateElement/source.cs#1)]
2003-
[!code-vb[Classic WebData XmlDocument.CreateElement Example#1](~/snippets/visualbasic/VS_Snippets_Data/Classic WebData XmlDocument.CreateElement Example/VB/source.vb#1)]
2014+
:::code language="csharp" source="~/snippets/csharp/System.Xml/XmlDocument/CreateElement/source.cs" id="Snippet1":::
2015+
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_Data/Classic WebData XmlDocument.CreateElement Example/VB/source.vb" id="Snippet1":::
20042016
20052017
]]></format>
20062018
</remarks>

0 commit comments

Comments
 (0)