1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
|
//
// System.Xml.XmlTextWriterTests
//
// Authors:
// Kral Ferch <kral_ferch@hotmail.com>
// Martin Willemoes Hansen <mwh@sysrq.dk>
//
// (C) 2002 Kral Ferch
// (C) 2003 Martin Willemoes Hansen
//
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using NUnit.Framework;
namespace MonoTests.System.Xml
{
[TestFixture]
public class XmlTextWriterTests
{
StringWriter sw;
XmlTextWriter xtw;
[SetUp]
public void GetReady ()
{
sw = new StringWriter ();
CreateXmlTextWriter ();
}
private void CreateXmlTextWriter ()
{
xtw = new XmlTextWriter (sw);
xtw.QuoteChar = '\'';
}
private string StringWriterText
{
get { return sw.GetStringBuilder ().ToString (); }
}
[Test]
public void AttributeNamespacesNonNamespaceAttributeBefore ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString("bar", "baz");
xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
Assert.AreEqual ("<foo bar='baz' xmlns:abc='http://abc.def'", StringWriterText);
}
[Test]
public void AttributeNamespacesNonNamespaceAttributeAfter ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("xmlns", "abc", null, "http://abc.def");
xtw.WriteAttributeString("bar", "baz");
Assert.AreEqual ("<foo xmlns:abc='http://abc.def' bar='baz'", StringWriterText);
}
[Test]
public void AttributeNamespacesThreeParamWithNullInNamespaceParam ()
{
xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText);
}
[Test]
public void WriteAttributeString_XmlNs_Valid ()
{
xtw.WriteAttributeString ("xmlns", null, "http://abc.def");
Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString ("xmlns", "http://www.w3.org/2000/xmlns/", "http://abc.def");
Assert.AreEqual ("xmlns='http://abc.def'", StringWriterText, "#2");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString (null, "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#3");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
Assert.AreEqual ("xmlns:test='http://abc.def'", StringWriterText, "#4");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("person");
xtw.WriteAttributeString ("", "test", "http://www.w3.org/2000/xmlns/", "http://abc.def");
xtw.WriteEndElement ();
Assert.AreEqual ("<person xmlns:test='http://abc.def' />", StringWriterText, "#5");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteAttributeString_XmlNs_Invalid1 ()
{
// The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
xtw.WriteAttributeString ("xmlns", "http://somenamespace.com", "http://abc.def");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteAttributeString_XmlNs_Invalid2 ()
{
// The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'
xtw.WriteAttributeString (null, "xmlns", "http://somenamespace.com", "http://abc.def");
}
[Test]
public void XmlSpace_Valid () // bug #77084
{
xtw.WriteAttributeString ("xml", "space", null, "preserve");
Assert.AreEqual ("xml:space='preserve'", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString ("xml", "space", "whatever", "default");
Assert.AreEqual ("xml:space='default'", StringWriterText, "#2");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("person");
xtw.WriteAttributeString ("xml", "space", "whatever", "default");
xtw.WriteEndElement ();
Assert.AreEqual ("<person xml:space='default' />", StringWriterText, "#3");
}
[Test]
public void WriteAttributeString_XmlPrefix_Valid ()
{
xtw.WriteAttributeString ("xml", "something", "whatever", "default");
Assert.AreEqual ("xml:something='default'", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString ("xml", "else", null, "whatever");
Assert.AreEqual ("xml:else='whatever'", StringWriterText, "#2");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("person");
xtw.WriteAttributeString ("xml", "something", "whatever", "default");
xtw.WriteAttributeString ("xml", "else", null, "whatever");
xtw.WriteEndElement ();
Assert.AreEqual ("<person xml:something='default' xml:else='whatever' />",
StringWriterText, "#3");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteAttributeString_XmlSpace_Invalid ()
{
// only preserve and default are valid values for xml:space
xtw.WriteAttributeString ("xml", "space", null, "something");
}
[Test]
public void AttributeNamespacesThreeParamWithTextInNamespaceParam ()
{
xtw.WriteAttributeString ("a", "http://somenamespace.com", "http://abc.def");
Assert.AreEqual ("d0p1:a='http://abc.def'", StringWriterText, "#1");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void AttributeNamespacesWithNullInNamespaceParam ()
{
// you cannot use prefix with an empty namespace
xtw.WriteAttributeString ("a", "abc", null, "http://abc.def");
}
[Test]
public void AttributeNamespacesWithTextInNamespaceParam ()
{
xtw.WriteAttributeString ("a", "abc", "http://somenamespace.com", "http://abc.def");
Assert.AreEqual ("a:abc='http://abc.def'", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString ("", "abc", "http://somenamespace.com", "http://abc.def");
Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#2");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
Assert.AreEqual ("d0p1:abc='http://abc.def'", StringWriterText, "#3");
}
[Test]
[Ignore ("Due to the (silly) dependency on bug #77088, this test will not be fixed. The test could be rewritten but it depends on the original test author.")]
public void AutoCreatePrefixes ()
{
xtw.WriteStartElement ("root");
xtw.WriteAttributeString (null, "abc", "http://somenamespace.com", "http://abc.def");
xtw.WriteAttributeString (null, "def", "http://somenamespace.com", "http://def.ghi");
xtw.WriteAttributeString (null, "ghi", "http://othernamespace.com", "http://ghi.jkl");
xtw.WriteEndElement ();
Assert.AreEqual ("<root d1p1:abc='http://abc.def' d1p1:def='http://def.ghi' d1p2:ghi='http://ghi.jkl' xmlns:d1p2='http://othernamespace.com' xmlns:d1p1='http://somenamespace.com' />", StringWriterText, "#1");
}
[Test]
[Ignore ("Due to the (silly) dependency on bug #77088, this test will not be fixed. The test could be rewritten but it depends on the original test author.")]
public void AutoCreatePrefixes2 ()
{
xtw.WriteStartElement ("person");
xtw.WriteAttributeString (null, "name", "http://somenamespace.com", "Driesen");
xtw.WriteAttributeString (null, "initials", "http://othernamespace.com", "GD");
xtw.WriteAttributeString (null, "firstName", "http://somenamespace.com", "Gert");
xtw.WriteStartElement ("address");
xtw.WriteAttributeString (null, "street", "http://somenamespace.com", "Campus");
xtw.WriteAttributeString (null, "number", "http://othernamespace.com", "1");
xtw.WriteAttributeString (null, "zip", "http://newnamespace.com", "3000");
xtw.WriteAttributeString (null, "box", "http://othernamespace.com", "a");
xtw.WriteEndElement ();
xtw.WriteEndElement ();
Assert.AreEqual (
"<person" +
" d1p1:name='Driesen'" +
" d1p2:initials='GD'" +
" d1p1:firstName='Gert'" +
" xmlns:d1p2='http://othernamespace.com'" +
" xmlns:d1p1='http://somenamespace.com'>" +
"<address" +
" d1p1:street='Campus'" +
" d1p2:number='1'" +
" d2p1:zip='3000'" +
" d1p2:box='a'" +
" xmlns:d2p1='http://newnamespace.com' />" +
"</person>", StringWriterText, "#2");
}
[Test]
public void AttributeNamespacesXmlnsXmlns ()
{
xtw.WriteStartElement ("foo");
// If XmlTextWriter conforms to "Namespaces in XML"
// when namespaceURI argument is null, then this
// is not allowed (http://www.w3.org/TR/REC-xml-names/
// Namespace Constraint: Prefix Declared), but seems
// like XmlTextWriter just ignores XML namespace
// constraints when namespaceURI argument is null.
xtw.WriteAttributeString ("xmlns", "xmlns", null, "http://abc.def");
//Assert.Fail ("A prefix must not start with \"xml\".");
}
[Test]
public void AttributeNamespacesXmlnsXmlns2 ()
{
// It is split from AttributeNamespacesXmlnsXmlns()
// because depending on XmlWriter it is likely to cause
// duplicate attribute error (XmlTextWriter is pretty
// hacky, so it does not raise such errors).
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("", "xmlns", null, "http://abc.def");
}
[Test]
public void WriteAttributeString_EmptyLocalName ()
{
xtw.WriteAttributeString ("", "something");
Assert.AreEqual ("='something'", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString ("", "", "something");
Assert.AreEqual ("='something'", StringWriterText, "#2");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString ("", "http://somenamespace.com", "something");
Assert.AreEqual ("d0p1:='something'", StringWriterText, "#3");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString ("x", "", "http://somenamespace.com", "something");
Assert.AreEqual ("x:='something'", StringWriterText, "#4");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString (null, "something");
Assert.AreEqual ("='something'", StringWriterText, "#5");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString (null, "", "something");
Assert.AreEqual ("='something'", StringWriterText, "#6");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString (null, "http://somenamespace.com", "something");
Assert.AreEqual ("d0p1:='something'", StringWriterText, "#7");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteAttributeString ("x", null, "http://somenamespace.com", "something");
Assert.AreEqual ("x:='something'", StringWriterText, "#8");
}
[Test]
public void WriteStartAttribute_EmptyLocalName ()
{
xtw.WriteStartAttribute ("", "");
Assert.AreEqual ("='", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartAttribute ("", "", "");
Assert.AreEqual ("='", StringWriterText, "#2");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartAttribute ("", "", "http://somenamespace.com");
Assert.AreEqual ("d0p1:='", StringWriterText, "#3");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartAttribute ("x", "", "http://somenamespace.com");
Assert.AreEqual ("x:='", StringWriterText, "#4");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartAttribute ("", null);
Assert.AreEqual ("='", StringWriterText, "#5");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartAttribute ("", null, "");
Assert.AreEqual ("='", StringWriterText, "#6");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartAttribute ("", null, "http://somenamespace.com");
Assert.AreEqual ("d0p1:='", StringWriterText, "#7");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartAttribute ("x", null, "http://somenamespace.com");
Assert.AreEqual ("x:='", StringWriterText, "#8");
}
[Test]
public void AttributeWriteAttributeString ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("foo", "bar");
Assert.AreEqual ("<foo foo='bar'", StringWriterText);
xtw.WriteAttributeString ("bar", "");
Assert.AreEqual ("<foo foo='bar' bar=''", StringWriterText);
xtw.WriteAttributeString ("baz", null);
Assert.AreEqual ("<foo foo='bar' bar='' baz=''", StringWriterText);
xtw.WriteAttributeString ("hoge", "a\nb");
Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a
b'", StringWriterText);
xtw.WriteAttributeString ("fuga", " a\t\r\nb\t");
Assert.AreEqual ("<foo foo='bar' bar='' baz='' hoge='a
b' fuga=' a\t
b\t'", StringWriterText);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void AttributeWriteAttributeStringNotInsideOpenStartElement ()
{
xtw.WriteStartElement ("foo");
xtw.WriteString ("bar");
xtw.WriteAttributeString ("baz", "quux");
}
[Test]
public void AttributeWriteAttributeStringWithoutParentElement ()
{
xtw.WriteAttributeString ("foo", "bar");
Assert.AreEqual ("foo='bar'", StringWriterText);
xtw.WriteAttributeString ("baz", "quux");
Assert.AreEqual ("foo='bar' baz='quux'", StringWriterText);
}
[Test]
public void WriteStartElement_EmptyLocalName ()
{
xtw.WriteStartElement ("", "");
Assert.AreEqual ("<", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("", "", "");
Assert.AreEqual ("<", StringWriterText, "#2");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("", "", "http://somenamespace.com");
Assert.AreEqual ("<", StringWriterText, "#3");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("x", "", "http://somenamespace.com");
Assert.AreEqual ("<x:", StringWriterText, "#4");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("", null);
Assert.AreEqual ("<", StringWriterText, "#5");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("", null, "");
Assert.AreEqual ("<", StringWriterText, "#6");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("", null, "http://somenamespace.com");
Assert.AreEqual ("<", StringWriterText, "#7");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("x", null, "http://somenamespace.com");
Assert.AreEqual ("<x:", StringWriterText, "#8");
}
[Test]
public void WriteElementString_EmptyLocalName ()
{
xtw.WriteElementString ("", "");
Assert.AreEqual ("< />", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteElementString ("", "", "");
Assert.AreEqual ("< />", StringWriterText, "#2");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteElementString ("", "http://somenamespace.com", "whatever");
Assert.AreEqual ("< xmlns='http://somenamespace.com'>whatever</>", StringWriterText, "#3");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteElementString ("", "http://somenamespace.com", "");
Assert.AreEqual ("< xmlns='http://somenamespace.com' />", StringWriterText, "#4");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteElementString (null, null);
Assert.AreEqual ("< />", StringWriterText, "#5");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteElementString (null, null, null);
Assert.AreEqual ("< />", StringWriterText, "#6");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteElementString (null, "http://somenamespace.com", "whatever");
Assert.AreEqual ("< xmlns='http://somenamespace.com'>whatever</>", StringWriterText, "#7");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteElementString (null, "http://somenamespace.com", null);
Assert.AreEqual ("< xmlns='http://somenamespace.com' />", StringWriterText, "#8");
}
[Test]
#if ONLY_1_1
[Category ("NotDotNet")] // MS.NET 1.1 does not allow zero-length namespace URI
#endif
public void WriteStartElement_Prefix_EmptyNamespace ()
{
xtw.WriteStartElement ("x", "whatever", "");
Assert.AreEqual ("<whatever", StringWriterText, "#1");
xtw.WriteEndElement ();
Assert.AreEqual ("<whatever />", StringWriterText, "#2");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteStartElement_Prefix_NullNamespace ()
{
xtw.WriteStartElement ("x", "whatever", null);
}
[Test]
public void WriteStartElement_XmlPrefix ()
{
xtw.WriteStartElement ("xml", "something", "http://www.w3.org/XML/1998/namespace");
Assert.AreEqual ("<xml:something", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("XmL", null, "http://www.w3.org/XML/1998/namespace");
Assert.AreEqual ("<XmL:", StringWriterText, "#2");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("xmlsomething", "name", "http://www.w3.org/XML/1998/namespace");
Assert.AreEqual ("<xmlsomething:name", StringWriterText, "#3");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteStartElement_XmlPrefix_Invalid1 ()
{
xtw.WriteStartElement ("xml", null, "http://somenamespace.com");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteStartElement_XmlPrefix_Invalid2 ()
{
xtw.WriteStartElement ("XmL", null, "http://somenamespace.com");
}
[Test]
public void WriteStartElement_XmlPrefix_Invalid3 ()
{
// from XML 1.0 (third edition) specification:
//
// [Definition: A Name is a token beginning with a letter or one of a
// few punctuation characters, and continuing with letters, digits,
// hyphens, underscores, colons, or full stops, together known as name
// characters.] Names beginning with the string "xml", or with any string
// which would match (('X'|'x') ('M'|'m') ('L'|'l')), are reserved for
// standardization in this or future versions of this specification.
//
// from the Namespaces in XML 1.0 specification:
//
// Prefixes beginning with the three-letter sequence x, m, l, in any case
// combination, are reserved for use by XML and XML-related specifications.
//
// should this prefix then not be considered invalid ?
//
// both Mono and MS.NET 1.x/2.0 accept it though
xtw.WriteStartElement ("xmlsomething", null, "http://somenamespace.com");
Assert.AreEqual ("<xmlsomething:", StringWriterText, "#1");
sw.GetStringBuilder ().Length = 0;
CreateXmlTextWriter ();
xtw.WriteStartElement ("XmLsomething", null, "http://somenamespace.com");
Assert.AreEqual ("<XmLsomething:", StringWriterText, "#2");
}
[Test]
public void CDataValid ()
{
xtw.WriteCData ("foo");
Assert.AreEqual ("<![CDATA[foo]]>", StringWriterText, "WriteCData had incorrect output.");
}
[Test]
public void CDataNull ()
{
xtw.WriteCData (null);
Assert.AreEqual ("<![CDATA[]]>", StringWriterText, "WriteCData had incorrect output.");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void CDataInvalid ()
{
xtw.WriteCData("foo]]>bar");
}
[Test]
public void CloseOpenElements ()
{
xtw.WriteStartElement("foo");
xtw.WriteStartElement("bar");
xtw.WriteStartElement("baz");
xtw.Close();
Assert.AreEqual ("<foo><bar><baz /></bar></foo>", StringWriterText,
"Close didn't write out end elements properly.");
}
[Test]
public void CloseWriteAfter ()
{
xtw.WriteElementString ("foo", "bar");
xtw.Close ();
// WriteEndElement and WriteStartDocument aren't tested here because
// they will always throw different exceptions besides 'The Writer is closed.'
// and there are already tests for those exceptions.
try {
xtw.WriteCData ("foo");
Assert.Fail ("WriteCData after Close Should have thrown an InvalidOperationException.");
} catch (InvalidOperationException) {
// Don't rely on English message assertion.
// It is enough to check an exception occurs.
// Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
}
try {
xtw.WriteComment ("foo");
Assert.Fail ("WriteComment after Close Should have thrown an InvalidOperationException.");
} catch (InvalidOperationException) {
// Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
}
try {
xtw.WriteProcessingInstruction ("foo", "bar");
Assert.Fail ("WriteProcessingInstruction after Close Should have thrown an InvalidOperationException.");
} catch (InvalidOperationException) {
// Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
}
try {
xtw.WriteStartElement ("foo", "bar", "baz");
Assert.Fail ("WriteStartElement after Close Should have thrown an InvalidOperationException.");
} catch (InvalidOperationException) {
// Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
}
try {
xtw.WriteAttributeString ("foo", "bar");
Assert.Fail ("WriteAttributeString after Close Should have thrown an InvalidOperationException.");
} catch (InvalidOperationException) {
// Assert.AreEqual ("Exception message is incorrect.", "The Writer is closed.", e.Message);
}
try {
xtw.WriteString ("foo");
Assert.Fail ("WriteString after Close Should have thrown an InvalidOperationException.");
} catch (InvalidOperationException) {
// Assert.AreEqual ("The Writer is closed.", e.Message, "Exception message is incorrect.");
}
}
[Test]
public void CommentValid ()
{
xtw.WriteComment ("foo");
Assert.AreEqual ("<!--foo-->", StringWriterText, "WriteComment had incorrect output.");
}
[Test]
public void CommentInvalid ()
{
try {
xtw.WriteComment("foo-");
Assert.Fail("Should have thrown an ArgumentException.");
} catch (ArgumentException) { }
try {
xtw.WriteComment("foo-->bar");
Assert.Fail("Should have thrown an ArgumentException.");
} catch (ArgumentException) { }
}
[Test]
public void ConstructorsAndBaseStream ()
{
Assert.IsTrue (Object.ReferenceEquals (null, this.xtw.BaseStream), "BaseStream property returned wrong value.");
MemoryStream ms;
StreamReader sr;
XmlTextWriter xtw;
ms = new MemoryStream ();
xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
xtw.WriteStartDocument ();
xtw.Flush ();
ms.Seek (0, SeekOrigin.Begin);
sr = new StreamReader (ms, Encoding.Unicode);
string expectedXmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
string actualXmlDeclaration = sr.ReadToEnd();
Assert.AreEqual (expectedXmlDeclaration, actualXmlDeclaration);
Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
ms = new MemoryStream ();
xtw = new XmlTextWriter (ms, new UnicodeEncoding ());
xtw.WriteStartDocument (true);
xtw.Flush ();
ms.Seek (0, SeekOrigin.Begin);
sr = new StreamReader (ms, Encoding.Unicode);
Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?>", sr.ReadToEnd ());
ms = new MemoryStream ();
xtw = new XmlTextWriter (ms, new UTF8Encoding ());
xtw.WriteStartDocument ();
xtw.Flush ();
ms.Seek (0, SeekOrigin.Begin);
sr = new StreamReader (ms, Encoding.UTF8);
Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-8\"?>", sr.ReadToEnd ());
ms = new MemoryStream ();
xtw = new XmlTextWriter (ms, null);
xtw.WriteStartDocument ();
xtw.Flush ();
ms.Seek (0, SeekOrigin.Begin);
sr = new StreamReader (ms, Encoding.UTF8);
Assert.AreEqual ("<?xml version=\"1.0\"?>", sr.ReadToEnd ());
ms = new MemoryStream ();
xtw = new XmlTextWriter (ms, null);
xtw.WriteStartDocument (true);
xtw.Flush ();
ms.Seek (0, SeekOrigin.Begin);
sr = new StreamReader (ms, Encoding.UTF8);
Assert.AreEqual ("<?xml version=\"1.0\" standalone=\"yes\"?>", sr.ReadToEnd ());
Assert.IsTrue (Object.ReferenceEquals (ms, xtw.BaseStream), "BaseStream property returned wrong value.");
}
[Test]
public void DocumentStart ()
{
xtw.WriteStartDocument ();
Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?>", StringWriterText,
"XmlDeclaration is incorrect.");
try {
xtw.WriteStartDocument ();
Assert.Fail("Should have thrown an InvalidOperationException.");
} catch (InvalidOperationException) {
// Don't rely on English message assertion.
// It is enough to check an exception occurs.
// Assert.AreEqual ("WriteStartDocument should be the first call.", e.Message, "Exception message is incorrect.");
}
xtw = new XmlTextWriter (sw = new StringWriter ());
xtw.QuoteChar = '\'';
xtw.WriteStartDocument (true);
Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='yes'?>", StringWriterText);
xtw = new XmlTextWriter (sw = new StringWriter ());
xtw.QuoteChar = '\'';
xtw.WriteStartDocument (false);
Assert.AreEqual ("<?xml version='1.0' encoding='utf-16' standalone='no'?>", StringWriterText);
}
[Test]
public void ElementAndAttributeSameXmlns ()
{
xtw.WriteStartElement ("ped", "foo", "urn:foo");
xtw.WriteStartAttribute ("ped", "foo", "urn:foo");
xtw.WriteEndElement ();
Assert.AreEqual ("<ped:foo ped:foo='' xmlns:ped='urn:foo' />", StringWriterText);
}
[Test]
[Category ("NotDotNet")]
public void ElementXmlnsNeedEscape ()
{
xtw.WriteStartElement ("test", "foo", "'");
xtw.WriteEndElement ();
// MS.NET output is : xmlns:test='''
Assert.AreEqual ("<test:foo xmlns:test=''' />", StringWriterText);
}
[Test]
public void ElementEmpty ()
{
xtw.WriteStartElement ("foo");
xtw.WriteEndElement ();
Assert.AreEqual ("<foo />", StringWriterText, "Incorrect output.");
}
[Test]
public void ElementWriteElementString ()
{
xtw.WriteElementString ("foo", "bar");
Assert.AreEqual ("<foo>bar</foo>", StringWriterText, "WriteElementString has incorrect output.");
xtw.WriteElementString ("baz", "");
Assert.AreEqual ("<foo>bar</foo><baz />", StringWriterText, "#2");
xtw.WriteElementString ("quux", null);
Assert.AreEqual ("<foo>bar</foo><baz /><quux />", StringWriterText, "#3");
xtw.WriteElementString ("", "quuux");
Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</>", StringWriterText, "#4");
xtw.WriteElementString (null, "quuuux");
Assert.AreEqual ("<foo>bar</foo><baz /><quux /><>quuux</><>quuuux</>", StringWriterText, "#5");
}
[Test]
public void FormattingTest ()
{
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument ();
xtw.WriteStartElement ("foo");
xtw.WriteElementString ("bar", "");
xtw.Close ();
Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0} <bar />{0}</foo>", Environment.NewLine), StringWriterText);
}
[Test]
public void FormattingInvalidXmlForFun ()
{
xtw.Formatting = Formatting.Indented;
xtw.IndentChar = 'x';
xtw.WriteStartDocument ();
xtw.WriteStartElement ("foo");
xtw.WriteStartElement ("bar");
xtw.WriteElementString ("baz", "");
xtw.Close ();
Assert.AreEqual (String.Format ("<?xml version='1.0' encoding='utf-16'?>{0}<foo>{0}xx<bar>{0}xxxx<baz />{0}xx</bar>{0}</foo>", Environment.NewLine), StringWriterText);
}
[Test]
public void FormattingFromRemarks ()
{
// Remarks section of on-line help for XmlTextWriter.Formatting suggests this test.
xtw.Formatting = Formatting.Indented;
xtw.WriteStartElement ("ol");
xtw.WriteStartElement ("li");
xtw.WriteString ("The big "); // This means "li" now has a mixed content model.
xtw.WriteElementString ("b", "E");
xtw.WriteElementString ("i", "lephant");
xtw.WriteString (" walks slowly.");
xtw.WriteEndElement ();
xtw.WriteEndElement ();
Assert.AreEqual (String.Format ("<ol>{0} <li>The big <b>E</b><i>lephant</i> walks slowly.</li>{0}</ol>", Environment.NewLine), StringWriterText);
}
[Test]
public void LookupPrefix ()
{
xtw.WriteStartElement ("root");
xtw.WriteStartElement ("one");
xtw.WriteAttributeString ("xmlns", "foo", null, "http://abc.def");
xtw.WriteAttributeString ("xmlns", "bar", null, "http://ghi.jkl");
Assert.AreEqual ("foo", xtw.LookupPrefix ("http://abc.def"), "#1");
Assert.AreEqual ("bar", xtw.LookupPrefix ("http://ghi.jkl"), "#2");
xtw.WriteEndElement ();
xtw.WriteStartElement ("two");
xtw.WriteAttributeString ("xmlns", "baz", null, "http://mno.pqr");
xtw.WriteString("quux");
Assert.AreEqual ("baz", xtw.LookupPrefix ("http://mno.pqr"), "#3");
Assert.IsNull (xtw.LookupPrefix ("http://abc.def"), "#4");
Assert.IsNull (xtw.LookupPrefix ("http://ghi.jkl"), "#5");
Assert.IsNull (xtw.LookupPrefix ("http://bogus"), "#6");
}
[Test]
public void NamespacesAttributesPassingInNamespaces ()
{
xtw.Namespaces = false;
xtw.WriteStartElement ("foo");
// These shouldn't throw any exceptions since they don't pass in
// a namespace.
xtw.WriteAttributeString ("bar", "baz");
xtw.WriteAttributeString ("", "a", "", "b");
xtw.WriteAttributeString (null, "c", "", "d");
xtw.WriteAttributeString ("", "e", null, "f");
xtw.WriteAttributeString (null, "g", null, "h");
Assert.AreEqual ("<foo bar='baz' a='b' c='d' e='f' g='h'", StringWriterText);
}
[Test]
public void NamespacesElementsPassingInNamespaces ()
{
xtw.Namespaces = false;
// These shouldn't throw any exceptions since they don't pass in
// a namespace.
xtw.WriteElementString ("foo", "bar");
xtw.WriteStartElement ("baz");
xtw.WriteStartElement ("quux", "");
xtw.WriteStartElement ("quuux", null);
xtw.WriteStartElement (null, "a", null);
xtw.WriteStartElement (null, "b", "");
xtw.WriteStartElement ("", "c", null);
xtw.WriteStartElement ("", "d", "");
Assert.AreEqual ("<foo>bar</foo><baz><quux><quuux><a><b><c><d", StringWriterText);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void NamespacesElementsPassingInNamespacesInvalid1 ()
{
// These should throw ArgumentException because they pass in a
// namespace when Namespaces = false.
xtw.Namespaces = false;
xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void NamespacesElementsPassingInNamespacesInvalid2 ()
{
xtw.Namespaces = false;
xtw.WriteStartElement ("foo", "http://netsack.com/");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void NamespacesElementsPassingInNamespacesInvalid3 ()
{
xtw.Namespaces = false;
xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void NamespacesElementsPassingInNamespacesInvalid4 ()
{
xtw.Namespaces = false;
xtw.WriteStartElement ("foo", "bar", null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void NamespacesElementsPassingInNamespacesInvalid5 ()
{
xtw.Namespaces = false;
xtw.WriteStartElement ("foo", "bar", "");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void NamespacesElementsPassingInNamespacesInvalid6 ()
{
xtw.Namespaces = false;
xtw.WriteStartElement ("foo", "", "");
}
[Test]
public void NamespacesNoNamespaceClearsDefaultNamespace ()
{
xtw.WriteStartElement(String.Empty, "foo", "http://netsack.com/");
xtw.WriteStartElement(String.Empty, "bar", String.Empty);
xtw.WriteElementString("baz", String.Empty, String.Empty);
xtw.WriteEndElement();
xtw.WriteEndElement();
Assert.AreEqual ("<foo xmlns='http://netsack.com/'><bar xmlns=''><baz /></bar></foo>",
StringWriterText, "XmlTextWriter is incorrectly outputting namespaces.");
}
[Test]
public void NamespacesPrefix ()
{
xtw.WriteStartElement ("foo", "bar", "http://netsack.com/");
xtw.WriteStartElement ("foo", "baz", "http://netsack.com/");
xtw.WriteElementString ("qux", "http://netsack.com/", String.Empty);
xtw.WriteEndElement ();
xtw.WriteEndElement ();
Assert.AreEqual ("<foo:bar xmlns:foo='http://netsack.com/'><foo:baz><foo:qux /></foo:baz></foo:bar>",
StringWriterText, "XmlTextWriter is incorrectly outputting prefixes.");
}
[Test]
#if ONLY_1_1
[Category ("NotDotNet")] // MS.NET 1.1 does not allow zero-length namespace URI
#endif
public void NamespacesPrefixWithEmptyAndNullNamespaceEmpty ()
{
xtw.WriteStartElement ("foo", "bar", "");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void NamespacesPrefixWithEmptyAndNullNamespaceNull ()
{
xtw.WriteStartElement ("foo", "bar", null);
}
[Test]
public void NamespacesSettingWhenWriteStateNotStart ()
{
xtw.WriteStartElement ("foo");
try {
xtw.Namespaces = false;
Assert.Fail ("Expected an InvalidOperationException.");
} catch (InvalidOperationException) {}
Assert.IsTrue (xtw.Namespaces);
}
[Test]
public void ProcessingInstructionValid ()
{
xtw.WriteProcessingInstruction("foo", "bar");
Assert.AreEqual ("<?foo bar?>", StringWriterText, "WriteProcessingInstruction had incorrect output.");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ProcessingInstructionInvalid1 ()
{
xtw.WriteProcessingInstruction("fo?>o", "bar");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ProcessingInstructionInvalid2 ()
{
xtw.WriteProcessingInstruction("foo", "ba?>r");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ProcessingInstructionInvalid3 ()
{
xtw.WriteProcessingInstruction("", "bar");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void ProcessingInstructionInvalid4 ()
{
xtw.WriteProcessingInstruction(null, "bar");
}
[Test]
public void QuoteCharDoubleQuote ()
{
xtw.QuoteChar = '"';
// version, encoding, standalone
xtw.WriteStartDocument (true);
// namespace declaration
xtw.WriteElementString ("foo", "http://netsack.com", "bar");
Assert.AreEqual ("<?xml version=\"1.0\" encoding=\"utf-16\" standalone=\"yes\"?><foo xmlns=\"http://netsack.com\">bar</foo>", StringWriterText);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void QuoteCharInvalid ()
{
xtw.QuoteChar = 'x';
}
[Test]
public void WriteBase64 ()
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] fooBar = encoding.GetBytes("foobar");
xtw.WriteBase64 (fooBar, 0, 6);
Assert.AreEqual ("Zm9vYmFy", StringWriterText);
try {
xtw.WriteBase64 (fooBar, 3, 6);
Assert.Fail ("Expected an Argument Exception to be thrown.");
} catch (ArgumentException) {}
try {
xtw.WriteBase64 (fooBar, -1, 6);
Assert.Fail ("Expected an Argument Exception to be thrown.");
} catch (ArgumentOutOfRangeException) {}
try {
xtw.WriteBase64 (fooBar, 3, -1);
Assert.Fail ("Expected an Argument Exception to be thrown.");
} catch (ArgumentOutOfRangeException) {}
try {
xtw.WriteBase64 (null, 0, 6);
Assert.Fail ("Expected an Argument Exception to be thrown.");
} catch (ArgumentNullException) {}
}
[Test]
public void WriteBinHex ()
{
byte [] bytes = new byte [] {4,14,34, 54,94,114, 134,194,255, 0,5};
xtw.WriteBinHex (bytes, 0, 11);
Assert.AreEqual ("040E22365E7286C2FF0005", StringWriterText);
}
[Test]
public void WriteCharEntity ()
{
xtw.WriteCharEntity ('a');
Assert.AreEqual ("a", StringWriterText);
xtw.WriteCharEntity ('A');
Assert.AreEqual ("aA", StringWriterText);
xtw.WriteCharEntity ('1');
Assert.AreEqual ("aA1", StringWriterText);
xtw.WriteCharEntity ('K');
Assert.AreEqual ("aA1K", StringWriterText);
try {
xtw.WriteCharEntity ((char)0xd800);
} catch (ArgumentException) {}
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void WriteEndAttribute ()
{
xtw.WriteEndAttribute ();
}
[Test]
public void WriteEndDocument ()
{
try {
xtw.WriteEndDocument ();
Assert.Fail ("Expected an Exception.");
// in .NET 2.0 it is InvalidOperationException.
// in .NET 1,1 it is ArgumentException.
} catch (Exception) {}
}
[Test]
public void WriteEndDocument2 ()
{
xtw.WriteStartDocument ();
try
{
xtw.WriteEndDocument ();
Assert.Fail ("Expected an Exception.");
// in .NET 2.0 it is InvalidOperationException.
// in .NET 1,1 it is ArgumentException.
} catch (Exception) {}
}
[Test]
public void WriteEndDocument3 ()
{
xtw.WriteStartDocument ();
xtw.WriteStartElement ("foo");
xtw.WriteStartAttribute ("bar", null);
Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='", StringWriterText);
xtw.WriteEndDocument ();
Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='' />", StringWriterText);
Assert.AreEqual (WriteState.Start, xtw.WriteState);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void WriteEndElement ()
{
// no matching StartElement
xtw.WriteEndElement ();
}
[Test]
public void WriteEndElement2 ()
{
xtw.WriteStartElement ("foo");
xtw.WriteEndElement ();
Assert.AreEqual ("<foo />", StringWriterText);
xtw.WriteStartElement ("bar");
xtw.WriteStartAttribute ("baz", null);
xtw.WriteEndElement ();
Assert.AreEqual ("<foo /><bar baz='' />", StringWriterText);
}
[Test]
public void FullEndElement ()
{
xtw.WriteStartElement ("foo");
xtw.WriteFullEndElement ();
Assert.AreEqual ("<foo></foo>", StringWriterText);
xtw.WriteStartElement ("bar");
xtw.WriteAttributeString ("foo", "bar");
xtw.WriteFullEndElement ();
Assert.AreEqual ("<foo></foo><bar foo='bar'></bar>", StringWriterText);
xtw.WriteStartElement ("baz");
xtw.WriteStartAttribute ("bar", null);
xtw.WriteFullEndElement ();
Assert.AreEqual ("<foo></foo><bar foo='bar'></bar><baz bar=''></baz>", StringWriterText);
}
[Test]
public void WriteQualifiedName ()
{
xtw.WriteStartElement (null, "test", null);
xtw.WriteAttributeString ("xmlns", "me", null, "http://localhost/");
xtw.WriteQualifiedName ("bob", "http://localhost/");
xtw.WriteEndElement ();
Assert.AreEqual ("<test xmlns:me='http://localhost/'>me:bob</test>", StringWriterText);
}
[Test]
public void WriteQualifiedNameNonDeclaredAttribute ()
{
xtw.WriteStartElement ("foo");
xtw.WriteStartAttribute ("a", "");
xtw.WriteQualifiedName ("attr", "urn:a");
xtw.WriteWhitespace (" ");
xtw.WriteQualifiedName ("attr", "urn:b");
xtw.WriteEndAttribute ();
xtw.WriteEndElement ();
string xml = sw.ToString ();
Assert.IsTrue (xml.IndexOf ("<foo ") >= 0, "foo");
Assert.IsTrue (xml.IndexOf ("a='d1p1:attr d1p2:attr'") > 0, "qnames");
Assert.IsTrue (xml.IndexOf (" xmlns:d1p1='urn:a'") > 0, "xmlns:a");
Assert.IsTrue (xml.IndexOf (" xmlns:d1p2='urn:b'") > 0, "xmlns:b");
}
[Test]
public void WriteQualifiedNameNonNamespacedName ()
{
xtw.WriteStartElement ("root");
xtw.WriteQualifiedName ("foo", "");
xtw.WriteEndElement ();
Assert.AreEqual ("<root>foo</root>", StringWriterText);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteQualifiedNameNonDeclaredContent ()
{
xtw.WriteStartElement ("foo");
xtw.WriteQualifiedName ("abc", "urn:abc");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteQualifiedNameNonNCName ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("xmlns", "urn:default");
xtw.WriteStartElement ("child");
xtw.WriteStartAttribute ("a", "");
xtw.WriteQualifiedName ("x:def", "urn:def");
}
[Test]
public void WriteRaw ()
{
xtw.WriteRaw("&<>\"'");
Assert.AreEqual ("&<>\"'", StringWriterText);
xtw.WriteRaw(null);
Assert.AreEqual ("&<>\"'", StringWriterText);
xtw.WriteRaw("");
Assert.AreEqual ("&<>\"'", StringWriterText);
// bug #77623
xtw.WriteRaw ("{0}{1}");
}
[Test]
public void WriteRawInvalidInAttribute ()
{
xtw.WriteStartElement ("foo");
xtw.WriteStartAttribute ("bar", null);
xtw.WriteRaw ("&<>\"'");
xtw.WriteEndAttribute ();
xtw.WriteEndElement ();
Assert.AreEqual ("<foo bar='&<>\"'' />", StringWriterText);
}
[Test]
public void WriteStateTest ()
{
Assert.AreEqual (WriteState.Start, xtw.WriteState);
xtw.WriteStartDocument ();
Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
xtw.WriteStartElement ("root");
Assert.AreEqual (WriteState.Element, xtw.WriteState);
xtw.WriteElementString ("foo", "bar");
Assert.AreEqual (WriteState.Content, xtw.WriteState);
xtw.Close ();
Assert.AreEqual (WriteState.Closed, xtw.WriteState);
}
[Test]
public void WriteString ()
{
xtw.WriteStartDocument ();
try {
xtw.WriteString("foo");
Assert.Fail ("should raise an error.");
} catch (InvalidOperationException) {}
}
[Test]
public void WriteString2 ()
{
xtw.WriteStartDocument ();
// Testing attribute values
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("bar", "&<>");
Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo bar='&<>'", StringWriterText);
}
[Test]
public void WriteAttributeStringSingleQuoteChar()
{
// When QuoteChar is single quote then replaces single quotes within attributes
// but not double quotes.
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("bar", "\"baz\"");
xtw.WriteAttributeString ("quux", "'baz'");
Assert.AreEqual ("<foo bar='\"baz\"' quux=''baz''", StringWriterText);
}
[Test]
public void WriteAttributeStringDoubleQuoteChar()
{
// When QuoteChar is double quote then replaces double quotes within attributes
// but not single quotes.
xtw.QuoteChar = '"';
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("bar", "\"baz\"");
xtw.WriteAttributeString ("quux", "'baz'");
Assert.AreEqual ("<foo bar=\""baz"\" quux=\"'baz'\"", StringWriterText);
}
[Test]
public void WriteStringWithEntities()
{
// Testing element values
xtw.QuoteChar = '\'';
xtw.WriteElementString ("foo", "&<>\"'");
Assert.AreEqual ("<foo>&<>\"'</foo>", StringWriterText);
}
[Test]
public void XmlLang ()
{
Assert.IsNull (xtw.XmlLang);
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("xml", "lang", null, "langfoo");
Assert.AreEqual ("langfoo", xtw.XmlLang);
Assert.AreEqual ("<foo xml:lang='langfoo'", StringWriterText);
xtw.WriteAttributeString ("boo", "yah");
Assert.AreEqual ("langfoo", xtw.XmlLang);
Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'", StringWriterText);
xtw.WriteElementString("bar", "baz");
Assert.AreEqual ("langfoo", xtw.XmlLang);
Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>", StringWriterText);
xtw.WriteString("baz");
Assert.AreEqual ("langfoo", xtw.XmlLang);
Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz", StringWriterText);
xtw.WriteStartElement ("quux");
xtw.WriteStartAttribute ("xml", "lang", null);
Assert.AreEqual ("langfoo", xtw.XmlLang);
Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
xtw.WriteString("langbar");
// Commented out there: it is implementation-dependent.
// and incompatible between .NET 1.0 and 1.1
// Assert.AreEqual ("langfoo", xtw.XmlLang);
// Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='", StringWriterText);
xtw.WriteEndAttribute ();
// Commented out there: it is implementation-dependent.
// and incompatible between .NET 1.0 and 1.1
// Assert.AreEqual ("langbar", xtw.XmlLang);
// Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'", StringWriterText);
// check if xml:lang repeats output even if same as current scope.
xtw.WriteStartElement ("joe");
xtw.WriteAttributeString ("xml", "lang", null, "langbar");
Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'", StringWriterText);
xtw.WriteElementString ("quuux", "squonk");
Assert.AreEqual ("langbar", xtw.XmlLang);
Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux>", StringWriterText);
xtw.WriteEndElement ();
xtw.WriteEndElement ();
Assert.AreEqual ("langfoo", xtw.XmlLang);
Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux>", StringWriterText);
xtw.WriteEndElement ();
Assert.IsNull (xtw.XmlLang);
Assert.AreEqual ("<foo xml:lang='langfoo' boo='yah'><bar>baz</bar>baz<quux xml:lang='langbar'><joe xml:lang='langbar'><quuux>squonk</quuux></joe></quux></foo>", StringWriterText);
xtw.Close ();
Assert.IsNull (xtw.XmlLang);
}
// TODO: test operational aspects
[Test]
public void XmlSpaceTest ()
{
xtw.WriteStartElement ("foo");
Assert.AreEqual (XmlSpace.None, xtw.XmlSpace, "#1");
xtw.WriteStartElement ("bar");
xtw.WriteAttributeString ("xml", "space", null, "preserve");
Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#2");
Assert.AreEqual ("<foo><bar xml:space='preserve'", StringWriterText, "#3");
xtw.WriteStartElement ("baz");
xtw.WriteAttributeString ("xml", "space", null, "preserve");
Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#4");
Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'", StringWriterText, "#5");
xtw.WriteStartElement ("quux");
xtw.WriteStartAttribute ("xml", "space", null);
Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#6");
Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText, "#7");
// Commented out there: it is implementation-dependent
// and incompatible between .NET 1.0 and 1.1
xtw.WriteString ("default");
// Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace);
// Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='", StringWriterText);
xtw.WriteEndAttribute ();
Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace, "#8");
Assert.AreEqual ("<foo><bar xml:space='preserve'><baz xml:space='preserve'><quux xml:space='default'", StringWriterText, "#9");
xtw.WriteEndElement ();
Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#10");
xtw.WriteEndElement ();
Assert.AreEqual (XmlSpace.Preserve, xtw.XmlSpace, "#11");
xtw.WriteEndElement ();
Assert.AreEqual (XmlSpace.None, xtw.XmlSpace, "#12");
xtw.WriteStartElement ("quux");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void XmlSpaceTestInvalidValue1 ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("xml", "space", null, "bubba");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void XmlSpaceTestInvalidValue2 ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("xml", "space", null, "PRESERVE");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void XmlSpaceTestInvalidValue3 ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("xml", "space", null, "Default");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void XmlSpaceTestInvalidValue4 ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("xml", "space", null, "bubba");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteWhitespaceNonWhitespace ()
{
xtw.WriteWhitespace ("x");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteWhitespace_Null ()
{
xtw.WriteWhitespace ((string) null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteWhitespace_Empty ()
{
xtw.WriteWhitespace (string.Empty);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteNmToken_Null ()
{
xtw.WriteNmToken ((string) null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteNmToken_Empty ()
{
xtw.WriteNmToken (string.Empty);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteNmToken_InvalidChars ()
{
xtw.WriteNmToken ("\uFFFF");
}
[Test]
public void WriteNmToken ()
{
xtw.WriteNmToken ("some:name");
Assert.AreEqual ("some:name", StringWriterText);
}
[Test]
public void XmlSpaceRaw ()
{
xtw.WriteStartElement ("foo");
xtw.WriteStartAttribute ("xml", "space", null);
Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
Assert.AreEqual ("<foo xml:space='", StringWriterText);
xtw.WriteString ("default");
// Commented out there: it is implementation-dependent
// and incompatible between .NET 1.0 and 1.1
// Assert.AreEqual (XmlSpace.None, xtw.XmlSpace);
// Assert.AreEqual ("<foo xml:space='", StringWriterText);
xtw.WriteEndAttribute ();
Assert.AreEqual (XmlSpace.Default, xtw.XmlSpace);
Assert.AreEqual ("<foo xml:space='default'", StringWriterText);
}
[Test]
public void WriteAttributes ()
{
XmlDocument doc = new XmlDocument();
StringWriter sw = new StringWriter();
XmlWriter wr = new XmlTextWriter(sw);
StringBuilder sb = sw.GetStringBuilder();
XmlParserContext ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
XmlTextReader xtr = new XmlTextReader("<?xml version='1.0' encoding='utf-8' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
xtr.Read(); // read XMLDecl
wr.WriteAttributes(xtr, false);
// This method don't always have to take this double-quoted style...
Assert.AreEqual ("version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"", sw.ToString ().Trim (),
"#WriteAttributes.XmlDecl.1");
sb.Remove(0, sb.Length); // init
ctx = new XmlParserContext(doc.NameTable, new XmlNamespaceManager(doc.NameTable), "", XmlSpace.Default);
xtr = new XmlTextReader("<?xml version='1.0' standalone='no'?><root a1='A' b2='B' c3='C'><foo><bar /></foo></root>", XmlNodeType.Document, ctx);
xtr.Read(); // read XMLDecl
Assert.AreEqual (XmlNodeType.XmlDeclaration, xtr.NodeType);
sw = new StringWriter ();
wr = new XmlTextWriter (sw);
// This block raises an error on MS.NET 1.0.
wr.WriteAttributes(xtr, false);
// This method don't always have to take this double-quoted style...
Assert.AreEqual ("version=\"1.0\" standalone=\"no\"", sw.ToString ().Trim (),
"#WriteAttributes.XmlDecl.2");
sw = new StringWriter ();
wr = new XmlTextWriter (sw);
sb.Remove(0, sb.Length); // init
xtr.Read(); // read root
Assert.AreEqual (XmlNodeType.Element, xtr.NodeType);
wr.WriteStartElement(xtr.LocalName, xtr.NamespaceURI);
wr.WriteAttributes(xtr, false);
wr.WriteEndElement();
wr.Close();
// This method don't always have to take this double-quoted style...
Assert.AreEqual ("<root a1=\"A\" b2=\"B\" c3=\"C\" />", sw.ToString ().Trim (),
"#WriteAttributes.Element");
xtr.Close ();
}
[Test]
public void WriteWhitespace ()
{
xtw.WriteStartElement ("a");
xtw.WriteWhitespace ("\n\t");
xtw.WriteStartElement ("b");
xtw.WriteWhitespace ("\n\t");
xtw.WriteEndElement ();
xtw.WriteWhitespace ("\n");
xtw.WriteEndElement ();
xtw.WriteWhitespace ("\n");
xtw.Flush ();
Assert.AreEqual ("<a>\n\t<b>\n\t</b>\n</a>\n", StringWriterText);
}
[Test]
public void FlushDoesntCloseTag ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("bar", "baz");
xtw.Flush ();
Assert.AreEqual ("<foo bar='baz'", StringWriterText);
}
[Test]
public void WriteWhitespaceClosesTag ()
{
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("bar", "baz");
xtw.WriteWhitespace (" ");
Assert.AreEqual ("<foo bar='baz'> ", StringWriterText);
}
[Test]
public void DontOutputMultipleXmlns ()
{
XmlDocument doc = new XmlDocument();
doc.LoadXml("<a xmlns:dt=\"b\" dt:dt=\"c\"/>");
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(doc.InnerXml);
Assert.AreEqual ("<a xmlns:dt=\"b\" dt:dt=\"c\" />",
doc2.OuterXml);
}
[Test]
public void DontOutputNonDeclaredXmlns ()
{
string xml = "<x:a foo='foo' xmlns:x='urn:foo'><b /></x:a>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(doc.InnerXml);
Assert.AreEqual (xml.Replace ('\'', '"'), doc2.OuterXml);
}
[Test]
public void DontOutputRemovalDefaultNSDeclaration ()
{
xtw.WriteStartDocument ();
xtw.WriteStartElement ("foo");
xtw.WriteAttributeString ("xmlns", "probe");
Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"), "#1");
xtw.WriteStartElement ("b");
Assert.AreEqual (String.Empty, xtw.LookupPrefix ("probe"), "#2");
xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
xtw.WriteEndElement (); // b2
xtw.WriteStartElement (null, "b2", ""); // *Do* output xmlns=""
xtw.WriteEndElement (); // b2
xtw.WriteEndElement (); // b
xtw.WriteEndElement (); // foo
xtw.WriteEndDocument ();
xtw.Close ();
Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 xmlns='' /></b></foo>", StringWriterText, "#3");
}
[Test]
public void DontOutputRemovalDefaultNSDeclaration2 ()
{
xtw.WriteStartDocument ();
// IMPORTANT DIFFERENCE!! ns = "", not null
xtw.WriteStartElement ("foo", "");
xtw.WriteAttributeString ("xmlns", "probe");
Assert.IsNull (xtw.LookupPrefix ("probe"), "#1");
xtw.WriteStartElement ("b");
Assert.IsNull (xtw.LookupPrefix ("probe"), "#2");
xtw.WriteStartElement (null, "b2", null); // *Don't* output xmlns=""
xtw.WriteEndElement (); // b2
xtw.WriteStartElement (null, "b2", ""); // *Don't* output xmlns=""
xtw.WriteEndElement (); // b2
xtw.WriteEndElement (); // b
xtw.WriteEndElement (); // foo
xtw.WriteEndDocument ();
xtw.Close ();
Assert.AreEqual ("<?xml version='1.0' encoding='utf-16'?><foo xmlns='probe'><b><b2 /><b2 /></b></foo>", StringWriterText, "#3");
}
[Test]
public void DoOutputRemovalDefaultNSDeclaration ()
{
xtw.WriteStartElement ("docelem", "a-namespace");
XmlDocument doc = new XmlDocument ();
doc.CreateElement ("hola").WriteTo (xtw);
// This means, WriteTo never passes null NamespaceURI argument to XmlWriter.
xtw.WriteEndElement ();
xtw.Close ();
Assert.AreEqual ("<docelem xmlns='a-namespace'><hola xmlns='' /></docelem>", StringWriterText);
}
[Test]
public void WriteAttributeTakePrecedenceOnXmlns ()
{
xtw.WriteStartElement ("root", "urn:foo");
xtw.WriteAttributeString ("xmlns", "urn:bar");
xtw.WriteEndElement ();
xtw.Close ();
Assert.AreEqual ("<root xmlns='urn:bar' />", StringWriterText);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void LookupPrefixNull ()
{
xtw.LookupPrefix (null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void LookupPrefixEmpty ()
{
xtw.LookupPrefix (String.Empty);
}
[Test]
public void LookupPrefixIgnoresXmlnsAttribute ()
{
Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
xtw.WriteStartElement ("root");
Assert.IsNull (xtw.LookupPrefix ("urn:foo"));
xtw.WriteAttributeString ("xmlns", "urn:foo");
// Surprisingly to say, it is ignored!!
Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
xtw.WriteStartElement ("hoge");
// (still after flushing previous start element.)
Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
xtw.WriteStartElement ("fuga", "urn:foo");
// Is this testing on the correct way? Yes, here it is.
Assert.AreEqual (String.Empty, xtw.LookupPrefix ("urn:foo"));
}
[Test]
public void WriteInvalidNames ()
{
xtw.WriteStartElement ("foo<>");
xtw.WriteAttributeString ("ho<>ge", "value");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void AttributeWriteStartAttributePrefixWithoutNS ()
{
xtw.WriteStartAttribute ("some", "foo", null);
}
[Test]
public void AttributeWriteStartAttributeXmlnsNullNS ()
{
xtw.WriteStartAttribute ("xmlns", "foo", null);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void AttributeWriteEndAttributeXmlnsNullNs ()
{
// This test checks if the specified namespace URI is
// incorrectly empty or not. Compare it with
// AttributeWriteStartAttributeXmlnsNullNS().
xtw.WriteStartAttribute ("xmlns", "foo", null);
xtw.WriteEndAttribute ();
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void AttributeWriteStartAttributePrefixXmlnsNonW3CNS ()
{
xtw.WriteStartAttribute ("xmlns", "foo", "urn:foo");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void AttributeWriteStartAttributeLocalXmlnsNonW3CNS ()
{
xtw.WriteStartAttribute ("", "xmlns", "urn:foo");
}
[Test]
public void WriteRawProceedToProlog ()
{
XmlTextWriter xtw = new XmlTextWriter (new StringWriter ());
xtw.WriteRaw ("");
Assert.AreEqual (WriteState.Prolog, xtw.WriteState);
}
[Test]
public void Indent ()
{
XmlDocument doc = new XmlDocument ();
doc.LoadXml ("<root><test>test<foo></foo>string</test><test>string</test></root>");
StringWriter sw = new StringWriter ();
sw.NewLine = "_";
XmlTextWriter xtw = new XmlTextWriter (sw);
xtw.Formatting = Formatting.Indented;
doc.WriteContentTo (xtw);
Assert.AreEqual (@"<root>_ <test>test<foo></foo>string</test>_ <test>string</test>_</root>", sw.ToString ());
}
[Test]
public void Indent2 ()
{
StringWriter sw = new StringWriter ();
XmlTextWriter xtw = new XmlTextWriter (sw);
xtw.Formatting = Formatting.Indented;
// sadly, this silly usage of this method is actually
// used in WriteNode() in MS.NET.
xtw.WriteProcessingInstruction ("xml",
"version=\"1.0\"");
xtw.WriteComment ("sample XML fragment");
Assert.AreEqual (string.Format(CultureInfo.InvariantCulture,
@"<?xml version=""1.0""?>{0}" +
"<!--sample XML fragment-->", Environment.NewLine),
sw.ToString ());
}
[Test]
public void Indent3 ()
{
XmlDocument doc = new XmlDocument ();
doc.PreserveWhitespace = true;
string s;
doc.LoadXml ("<root><element></element><!-- comment indented --><element>sample <!-- comment non-indented --></element></root>");
s = GetIndentedOutput (doc.DocumentElement);
Assert.AreEqual (s, String.Format ("<root>{0} <element>{0} </element>{0} <!-- comment indented -->{0} <element>sample <!-- comment non-indented --></element>{0}</root>", "\n"), "#1");
doc.LoadXml ("<root> \n<mid> \n<mid> \n<child attr='value'>sample <nested attr='value' /> string</child> <child2 attr='value'>sample string</child2> <empty attr='value'/>\n<a>test</a> \n</mid> <returnValue> <returnType>System.String</returnType> </returnValue> </mid> </root>");
s = GetIndentedOutput (doc.DocumentElement);
Assert.AreEqual (s, String.Format ("<root> {0}<mid> {0}<mid> {0}<child attr='value'>sample <nested attr='value' /> string</child> <child2 attr='value'>sample string</child2> <empty attr='value' />{0}<a>test</a> {0}</mid> <returnValue> <returnType>System.String</returnType> </returnValue> </mid> </root>", "\n"), "#2");
doc.LoadXml ("<!-- after /MemberType and after /returnValue --><root><MemberType>blah</MemberType>\n <returnValue><returnType>System.String</returnType></returnValue>\n <Docs><summary>text</summary><value>text<see cref='ttt' /></value><remarks/></Docs></root>");
s = GetIndentedOutput (doc.DocumentElement);
Assert.AreEqual (s, String.Format ("<root>{0} <MemberType>blah</MemberType>{0} <returnValue><returnType>System.String</returnType></returnValue>{0} <Docs><summary>text</summary><value>text<see cref='ttt' /></value><remarks /></Docs></root>", "\n"), "#3");
}
string GetIndentedOutput (XmlNode n)
{
StringWriter sw = new StringWriter ();
sw.NewLine = "\n";
XmlTextWriter xtw = new XmlTextWriter (sw);
xtw.QuoteChar = '\'';
xtw.Formatting = Formatting.Indented;
n.WriteTo (xtw);
return sw.ToString ();
}
[Test]
public void CloseTwice ()
{
StringWriter sw = new StringWriter ();
XmlTextWriter writer = new XmlTextWriter (sw);
writer.Close ();
// should not result in an exception
writer.Close ();
}
[Test]
public void WriteRawWriteString ()
{
// WriteRaw () -> WriteString ().
xtw.WriteRaw ("");
xtw.WriteString ("foo");
Assert.AreEqual (WriteState.Content, xtw.WriteState);
}
[Test]
public void LookupOverridenPrefix ()
{
xtw.WriteStartElement ("out");
xtw.WriteAttributeString ("xmlns", "baz", "http://www.w3.org/2000/xmlns/", "xyz");
xtw.WriteStartElement ("baz", "foo", "abc");
Assert.IsNull (xtw.LookupPrefix ("xyz"));
}
[Test]
public void DuplicatingNamespaceMappingInAttributes ()
{
xtw.WriteStartElement ("out");
xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
xtw.WriteAttributeString ("p", "bar", "urn:bar", "xyz");
xtw.WriteAttributeString ("p", "baz", "urn:baz", "xyz");
xtw.WriteStartElement ("out");
xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
xtw.WriteStartElement ("out");
xtw.WriteAttributeString ("p", "foo", "urn:foo", "xyz");
xtw.WriteEndElement ();
xtw.WriteEndElement ();
xtw.WriteEndElement ();
string xml = sw.ToString ();
Assert.IsTrue (xml.IndexOf ("p:foo='xyz'") > 0, "p:foo" + ". output is " + xml);
Assert.IsTrue (xml.IndexOf ("d1p1:bar='xyz'") > 0, "d1p1:bar" + ". output is " + xml);
Assert.IsTrue (xml.IndexOf ("d1p2:baz='xyz'") > 0, "d1p1:baz" + ". output is " + xml);
Assert.IsTrue (xml.IndexOf ("xmlns:d1p2='urn:baz'") > 0, "xmlns:d1p2" + ". output is " + xml);
Assert.IsTrue (xml.IndexOf ("xmlns:d1p1='urn:bar'") > 0, "xmlns:d1p1" + ". output is " + xml);
Assert.IsTrue (xml.IndexOf ("xmlns:p='urn:foo'") > 0, "xmlns:p" + ". output is " + xml);
Assert.IsTrue (xml.IndexOf ("<out p:foo='xyz'><out p:foo='xyz' /></out></out>") > 0, "remaining" + ". output is " + xml);
}
[Test]
public void WriteXmlSpaceIgnoresNS ()
{
xtw.WriteStartElement ("root");
xtw.WriteAttributeString ("xml", "space", "abc", "preserve");
xtw.WriteEndElement ();
Assert.AreEqual ("<root xml:space='preserve' />", sw.ToString ());
}
[Test] // bug #75546
public void WriteEmptyNSQNameInAttribute ()
{
XmlTextWriter xtw = new XmlTextWriter (TextWriter.Null);
xtw.WriteStartElement ("foo", "urn:goo");
xtw.WriteAttributeString ("xmlns:bar", "urn:bar");
xtw.WriteStartAttribute ("foo", "");
xtw.WriteQualifiedName ("n1", "urn:bar");
xtw.WriteEndAttribute ();
xtw.WriteStartAttribute ("foo", "");
xtw.WriteQualifiedName ("n2", "");
xtw.WriteEndAttribute ();
}
[Test]
[ExpectedException (typeof (ArgumentException))]
// cannot bind any prefix to "http://www.w3.org/2000/xmlns/".
public void WriteQualifiedNameXmlnsError ()
{
xtw.WriteStartElement ("foo");
xtw.WriteQualifiedName ("", "http://www.w3.org/2000/xmlns/");
}
[Test]
public void WriteDocType ()
{
// we have the following test matrix:
//
// | name | publicid | systemid | subset|
// |------------------------------------|
// | X | X | X | X | #01
// | X | E | X | X | #02
// | X | X | E | X | #03
// | X | X | X | E | #04
// | X | E | E | X | #05
// | X | X | E | E | #06
// | X | E | X | E | #07
// | X | E | E | E | #08
// | X | N | X | X | #09
// | X | X | N | X | #10
// | X | X | X | N | #11
// | X | N | N | X | #12
// | X | X | N | N | #13
// | X | N | X | N | #14
// | X | N | N | N | #15
//
// Legend:
// -------
// X = Has value
// E = Zero-length string
// N = Null
xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
Assert.AreEqual ("<!DOCTYPE test PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN'" +
" 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'[sub]>",
sw.ToString (), "#01");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", string.Empty,
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
sw.ToString (), "#02");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
string.Empty, "sub");
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
" \"\"[sub]>",
sw.ToString (), "#03");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
sw.ToString (), "#04");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", string.Empty, string.Empty, "sub");
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[sub]>",
sw.ToString (), "#05");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
string.Empty, string.Empty);
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
" \"\"[]>",
sw.ToString (), "#06");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", string.Empty,
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", string.Empty);
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\"" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[]>",
sw.ToString (), "#07");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", string.Empty, string.Empty, string.Empty);
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"\" \"\"[]>",
sw.ToString (), "#08");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", (string) null,
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"[sub]>",
sw.ToString (), "#09");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
(string) null, "sub");
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
" \"\"[sub]>",
sw.ToString (), "#10");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
sw.ToString (), "#11");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", (string) null, (string) null, "sub");
Assert.AreEqual ("<!DOCTYPE test[sub]>",
sw.ToString (), "#12");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", "-//W3C//DTD XHTML 1.0 Strict//EN",
(string) null, (string) null);
Assert.AreEqual ("<!DOCTYPE test PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"" +
" \"\">",
sw.ToString (), "#13");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", (string) null,
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", (string) null);
Assert.AreEqual ("<!DOCTYPE test SYSTEM" +
" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">",
sw.ToString (), "#14");
sw.GetStringBuilder ().Length = 0;
xtw = new XmlTextWriter (sw);
xtw.WriteDocType ("test", (string) null, (string) null, (string) null);
Assert.AreEqual ("<!DOCTYPE test>",
sw.ToString (), "#15");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteDocType_EmptyName ()
{
xtw.WriteDocType (string.Empty, "-//W3C//DTD XHTML 1.0 Strict//EN",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void WriteDocType_NullName ()
{
xtw.WriteDocType ((string) null, "-//W3C//DTD XHTML 1.0 Strict//EN",
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "sub");
}
[Test] // bug #76095
public void SurrogatePairsInWriteString ()
{
MemoryStream ms = new MemoryStream ();
XmlWriter writer = new XmlTextWriter(ms, null);
writer.WriteElementString("a", "\ud800\udf39");
writer.Close();
byte [] referent = new byte [] {0x3c, 0x61, 0x3e, 0xf0,
0x90, 0x8c, 0xb9, 0x3c, 0x2f, 0x61, 0x3e};
NUnit.Framework.Assert.AreEqual (referent, ms.ToArray ());
}
[Test]
public void InvalidCharIsWrittenAsSillyReferences ()
{
// I can't say how MS XmlTextWriter is silly.
// The expected output is *not* well-formed XML.
// Everyone have to make sure that he or she does
// not write invalid characters directly so that
// the output XML string can be fed by other XML
// processors.
// The funny thing is that XmlTextWriter spends
// significant performance on checking invalid
// characters, but results in nothing.
xtw.WriteElementString ("a", "\x0");
NUnit.Framework.Assert.AreEqual ("<a>�</a>",
StringWriterText);
}
[Test] // see also bug #77082
public void WriteDocTypeIndent ()
{
string expected = String.Format (@"<?xml version='1.0'?>{0}<!DOCTYPE root PUBLIC '' 'urn:foo'[]>{0}<root />", Environment.NewLine);
xtw.Formatting = Formatting.Indented;
xtw.WriteProcessingInstruction ("xml", "version='1.0'");
xtw.WriteDocType ("root", "", "urn:foo", "");
xtw.WriteStartElement ("root");
xtw.WriteEndElement ();
xtw.Close ();
Assert.AreEqual (expected, StringWriterText);
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void WriteDocTypeTwice ()
{
xtw.WriteDocType ("root", "", "urn:foo", "");
xtw.WriteDocType ("root", "", "urn:foo", "");
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void XmlDeclAfterDocType ()
{
xtw.WriteDocType ("root", "", "urn:foo", "");
xtw.WriteStartDocument ();
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void XmlDeclAfterWhitespace ()
{
xtw.WriteWhitespace (" ");
xtw.WriteStartDocument ();
}
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void XmlDeclAfterPI ()
{
xtw.WriteProcessingInstruction ("pi", "");
xtw.WriteStartDocument ();
}
[Test]
public void WriteRawEmptyCloseStartTag ()
{
xtw.WriteStartElement ("stream", "stream","http://etherx.jabber.org/streams");
xtw.WriteAttributeString ("version", "1.0");
xtw.WriteAttributeString ("to", "me@test.com");
xtw.WriteAttributeString ("from", "server");
xtw.WriteAttributeString ("xmlns", "jabber:client");
xtw.WriteRaw ("");// Ensure that the tag is closed
xtw.Flush ();
Assert.AreEqual ("<stream:stream version='1.0' to='me@test.com' from='server' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>", StringWriterText);
}
[Test] // bug #78148
public void UpdateFormattingOnTheFly ()
{
XmlTextWriter w = new XmlTextWriter (TextWriter.Null);
w.WriteStartElement ("test");
w.Formatting = Formatting.Indented;
}
[Test] // bug #78598
public void WriteGlobalAttributeInDefaultNS ()
{
StringWriter sw = new StringWriter ();
XmlTextWriter tw = new XmlTextWriter (sw);
string ns = "http://schemas.xmlsoap.org/soap/envelope/";
tw.WriteStartElement ("Envelope");
tw.WriteAttributeString ("xmlns", ns);
int start = sw.ToString ().Length;
tw.WriteStartElement ("UserInfo");
tw.WriteStartAttribute ("actor", ns);
tw.WriteEndAttribute ();
tw.WriteEndElement ();
tw.WriteEndElement ();
Assert.IsTrue (sw.ToString ().IndexOf (ns, start) > 0);
}
[Test]
public void WriteCommentPIAndIndent ()
{
StringWriter sw = new StringWriter ();
XmlTextWriter w = new XmlTextWriter (sw);
w.Formatting = Formatting.Indented;
w.WriteStartElement ("foo");
w.WriteComment ("test");
w.WriteProcessingInstruction ("PI", "");
w.WriteStartElement ("child");
w.WriteEndElement ();
w.WriteComment ("test");
w.WriteString ("STRING");
w.WriteEndElement ();
Assert.AreEqual (String.Format (@"<foo>{0} <!--test-->{0} <?PI ?>{0} <child />{0} <!--test-->STRING</foo>", Environment.NewLine), sw.ToString ());
}
[Test]
public void WriteBinHexAttribute () // for bug #79019
{
XmlWriter writer = new XmlTextWriter (TextWriter.Null);
writer.WriteStartElement ("test");
byte [] buffer1 = new byte [] {200, 155};
writer.WriteStartAttribute ("key", "");
writer.WriteBinHex (buffer1, 0, buffer1.Length);
writer.WriteEndAttribute ();
writer.WriteEndElement ();
}
[Test]
public void LookupNamespace ()
{
StringWriter sw = new StringWriter ();
XmlTextWriter xw = new XmlTextWriter (sw);
xw.Formatting = Formatting.Indented;
string q1 = "urn:test";
string q1prefix_first= "q1";
// Ensure we get a different reference for the string "q1"
string q1prefix_second = ("q1" + "a").Substring(0,2);
xw.WriteStartElement("document");
xw.WriteStartElement("item");
xw.WriteStartElement (q1prefix_first, "addMedia", q1);
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteStartElement("item");
xw.WriteStartElement (q1prefix_second, "addMedia", q1);
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndElement();
string xml = sw.ToString ();
int first = xml.IndexOf ("xmlns");
Assert.IsTrue (xml.IndexOf ("xmlns", first + 5) > 0);
}
[Test]
public void WriteAttributePrefixedNullNamespace ()
{
StringWriter sw = new StringWriter ();
XmlWriter xw = new XmlTextWriter (sw);
xw.WriteStartElement ("root");
xw.WriteAttributeString ("xmlns", "abc", null, "uri:abcnamespace");
xw.WriteAttributeString ("abc", "def", null, "value");
xw.WriteEndElement ();
Assert.AreEqual ("<root xmlns:abc=\"uri:abcnamespace\" abc:def=\"value\" />", sw.ToString ());
}
[Test]
public void WriteElementPrefixedNullNamespace ()
{
StringWriter sw = new StringWriter ();
XmlWriter xw = new XmlTextWriter (sw);
xw.WriteStartElement ("root");
xw.WriteAttributeString ("xmlns", "abc", null, "uri:abcnamespace");
xw.WriteStartElement ("abc", "def", null);
xw.WriteEndElement ();
xw.WriteEndElement ();
Assert.AreEqual ("<root xmlns:abc=\"uri:abcnamespace\"><abc:def /></root>", sw.ToString ());
}
#if NET_2_0
[Test]
[ExpectedException (typeof (InvalidOperationException))]
public void RejectWritingAtErrorState ()
{
try {
xtw.WriteEndElement ();
} catch (Exception) {
}
xtw.WriteStartElement ("foo");
}
#endif
}
}
|