You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: xml/System.Xml/XmlTextReader.xml
+9-9Lines changed: 9 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -282,7 +282,7 @@
282
282
> [!NOTE]
283
283
> In version 1.1 of the [!INCLUDE[dnprdnshort](~/includes/dnprdnshort-md.md)], partially trusted code cannot set the `XmlResolver` property. The workaround is to create an <xref:System.Xml.XmlUrlResolver> with the necessary credentials, pass the URI to the <xref:System.Xml.XmlUrlResolver.GetEntity%2A?displayProperty=nameWithType> method, and then construct the `XmlTextReader` using the resulting <xref:System.IO.Stream> object. The workaround is described in the following C# code.
284
284
285
-
```
285
+
```csharp
286
286
// Create a resolver with the necessary credentials.
287
287
XmlUrlResolver resolver = new XmlUrlResolver();
288
288
NetworkCredential nc = new NetworkCredential(SecurelyStoredUserName, SecurelyStoredPassword, SecurelyStoredDomain);
@@ -1510,20 +1510,20 @@ XmlTextReader reader = new XmlTextReader(s);
1510
1510
1511
1511
The following XML contains an attribute in a specific namespace:
1512
1512
1513
-
```
1513
+
```xml
1514
1514
<test xmlns:dt="urn:datatypes" dt:type="int"/>
1515
1515
```
1516
1516
1517
1517
You can lookup the `dt:type` attribute using one argument (prefix and local name) or two arguments (local name and namespace URI):
In the following XML, if the reader is positioned on the `href` attribute, the prefix `a` is resolved by calling `reader.LookupNamespace("a")`. The returned string is `urn:456`.
2092
2092
2093
-
```
2093
+
```xml
2094
2094
<root xmlns:a="urn:456">
2095
2095
<item>
2096
2096
<ref href="a:b"/>
@@ -3225,7 +3225,7 @@ abc<tag/>
3225
3225
3226
3226
In the following XML, if the reader is positioned on the start tag, `ReadChars` returns `test` and positions the reader after the end tag.
3227
3227
3228
-
```
3228
+
```xml
3229
3229
<Item>test</Item>
3230
3230
```
3231
3231
@@ -3245,7 +3245,7 @@ abc<tag/>
3245
3245
3246
3246
For example, using the following XML:
3247
3247
3248
-
```
3248
+
```xml
3249
3249
<thing>
3250
3250
some text
3251
3251
</thing>
@@ -3255,7 +3255,7 @@ abc<tag/>
3255
3255
3256
3256
The reader is positioned on the `<item>` element at the end of the while loop.
3257
3257
3258
-
```
3258
+
```csharp
3259
3259
if (XmlNodeType.Element == reader.NodeType && "thing" == reader.Name)
If the reader is positioned on a leaf node already (such as the `<x>` node or the text node `abc`), calling `Skip` is the same as calling <xref:System.Xml.XmlTextReader.Read%2A>.
Copy file name to clipboardExpand all lines: xml/System.Xml/XmlTextWriter.xml
+12-12Lines changed: 12 additions & 12 deletions
Original file line number
Diff line number
Diff line change
@@ -42,7 +42,7 @@
42
42
43
43
`XmlTextWriter` maintains a namespace stack corresponding to all the namespaces defined in the current element stack. Using `XmlTextWriter` you can declare namespaces manually.
The above C# code produces the following output. `XmlTextWriter` promotes the namespace declaration to the root element to avoid having it duplicated on the two child elements. The child elements pick up the prefix from the namespace declaration.
56
56
57
-
```
57
+
```xml
58
58
<root xmlns:x="urn:1">
59
59
<x:item/>
60
60
<x:item/>
@@ -63,14 +63,14 @@ w.WriteEndElement();
63
63
64
64
`XmlTextWriter` also allows you to override the current namespace declaration. In the following example, the namespace URI "123" is overridden by "abc" to produce the XML element `<x:node xmlns:x="abc"/>`.
65
65
66
-
```
66
+
```csharp
67
67
w.WriteStartElement("x","node","123");
68
68
w.WriteAttributeString("xmlns","x",null,"abc");
69
69
```
70
70
71
71
By using the write methods that take a prefix as an argument you can also specify which prefix to use. In the following example, two different prefixes are mapped to the same namespace URI to produce the XML text `<x:root xmlns:x="urn:1"><y:item xmlns:y="urn:1"/></x:root>`.
72
72
73
-
```
73
+
```csharp
74
74
XmlTextWriter w = new XmlTextWriter(Console.Out);
75
75
w.WriteStartElement("x","root","urn:1");
76
76
w.WriteStartElement("y","item","urn:1");
@@ -81,7 +81,7 @@ w.Close();
81
81
82
82
If there are multiple namespace declarations mapping different prefixes to the same namespace URI, `XmlTextWriter` walks the stack of namespace declarations backwards and picks the closest one.
83
83
84
-
```
84
+
```csharp
85
85
XmlTextWriter w = new XmlTextWriter(Console.Out);
86
86
w.Formatting = Formatting.Indented;
87
87
w.WriteStartElement("x","root","urn:1");
@@ -94,7 +94,7 @@ w.Close();
94
94
95
95
In the above C# example, because the `WriteAttributeString` call does not specify a prefix, the writer uses the last prefix pushed onto the namespace stack, and produces the following XML:
96
96
97
-
```
97
+
```xml
98
98
<x:root xmlns:x="urn:1">
99
99
<y:item y:attr="123" xmlns:y="urn:1" />
100
100
</x:root>
@@ -110,7 +110,7 @@ w.Close();
110
110
111
111
To write strongly typed data, use the <xref:System.Xml.XmlConvert> class to convert data types to string. For example, the following C# code converts the data from `Double` to `String` and writes the element `<price>19.95</price>`.
If the `Indented` option is set, child elements are indented using the <xref:System.Xml.XmlTextWriter.Indentation%2A> and <xref:System.Xml.XmlTextWriter.IndentChar%2A> properties. Only element content is indented. The following C# code writes out HTML elements including mixed content:
472
472
473
-
```
473
+
```csharp
474
474
XmlTextWriter w = new XmlTextWriter(Console.Out);
475
475
w.Formatting = Formatting.Indented;
476
476
w.WriteStartElement("ol");
@@ -485,7 +485,7 @@ XmlTextWriter w = new XmlTextWriter(Console.Out);
485
485
486
486
The above code produces the following output:
487
487
488
-
```
488
+
```xml
489
489
<ol>
490
490
<li>The big <b>E</b><i>lephant</i> walks slowly.</li>
491
491
</ol>
@@ -547,7 +547,7 @@ XmlTextWriter w = new XmlTextWriter(Console.Out);
547
547
548
548
Indentation is performed on following node types: `DocumentType`, `Element`, `Comment`, `ProcessingInstruction`, and `CDATASection`. All other node types are not affected. The `XmlTextWriter` does not indent the internal DTD subset. However, you could do the following to apply formatting to the internal DTD subset.
In the following XML string, if the reader is positioned on the `href` attribute, the prefix `a` is resolved by calling `reader.LookupNamespace("a", true)`. The returned string is `urn:456`.
If <xref:System.Xml.XmlValidatingReader.EntityHandling%2A> is set to `ExpandCharEntities`, the following C# code returns the attribute value as two text nodes and one entity reference node:
This property represents the xml:lang scope within which the current node resides. For example, here is an XML fragment with `xml:lang` set to U.S. English in the root element:
The following C# code copies an entire XML input document to the console:
3354
3354
3355
-
```
3355
+
```csharp
3356
3356
XmlReader reader = XmlReader.Create(myfile);
3357
3357
XmlWriter writer = XmlWriter.Create(Console.Out);
3358
3358
writer.WriteNode(reader, false);
3359
3359
```
3360
3360
3361
3361
If you have moved off the root node and are positioned elsewhere in the document the following C# example correctly writes out the nodes.
3362
3362
3363
-
```
3363
+
```csharp
3364
3364
XmlReader reader = XmlReader.Create(myfile);
3365
3365
reader.Read(); // Read PI
3366
3366
reader.Read(); // Read Comment
@@ -3631,7 +3631,7 @@ while (!reader.EOF){
3631
3631
## Remarks
3632
3632
This method can be used to write the XML declaration (rather than <xref:System.Xml.XmlWriter.WriteStartDocument%2A>). This could result in the encoding attribute being incorrectly written. For example, the following C# code would result in an invalid XML document because the default encoding is UTF-8.
After calling this method you can either write attributes, or create content using <xref:System.Xml.XmlWriter.WriteComment%2A>, <xref:System.Xml.XmlWriter.WriteString%2A>, or `WriteStartElement` for child elements. You can close the element with either <xref:System.Xml.XmlWriter.WriteEndElement%2A> or <xref:System.Xml.XmlWriter.WriteFullEndElement%2A>. For example, the following C# code:
0 commit comments