1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using OLEDB.Test.ModuleCore; 6 using System.IO; 7 using System.Text; 8 using XmlCoreTest.Common; 9 using Xunit; 10 11 namespace System.Xml.Tests 12 { 13 //[TestCase(Name = "XmlWriterSettings: NamespaceHandling")] 14 public partial class TCNamespaceHandling 15 { 16 private static NamespaceHandling[] s_nlHandlingMembers = { NamespaceHandling.Default, NamespaceHandling.OmitDuplicates }; 17 private StringWriter _strWriter = null; 18 CreateMemWriter(XmlWriterUtils utils, XmlWriterSettings settings)19 private XmlWriter CreateMemWriter(XmlWriterUtils utils, XmlWriterSettings settings) 20 { 21 XmlWriterSettings wSettings = settings.Clone(); 22 wSettings.CloseOutput = false; 23 wSettings.OmitXmlDeclaration = true; 24 wSettings.CheckCharacters = false; 25 XmlWriter w = null; 26 27 switch (utils.WriterType) 28 { 29 case WriterType.UTF8Writer: 30 wSettings.Encoding = Encoding.UTF8; 31 if (_strWriter != null) _strWriter.Dispose(); 32 _strWriter = new StringWriter(); 33 w = WriterHelper.Create(_strWriter, wSettings, overrideAsync: true, async: utils.Async); 34 break; 35 case WriterType.UnicodeWriter: 36 wSettings.Encoding = Encoding.Unicode; 37 if (_strWriter != null) _strWriter.Dispose(); 38 _strWriter = new StringWriter(); 39 w = WriterHelper.Create(_strWriter, wSettings, overrideAsync: true, async: utils.Async); 40 break; 41 case WriterType.WrappedWriter: 42 if (_strWriter != null) _strWriter.Dispose(); 43 _strWriter = new StringWriter(); 44 XmlWriter ww = WriterHelper.Create(_strWriter, wSettings, overrideAsync: true, async: utils.Async); 45 w = WriterHelper.Create(ww, wSettings, overrideAsync: true, async: utils.Async); 46 break; 47 case WriterType.CharCheckingWriter: 48 if (_strWriter != null) _strWriter.Dispose(); 49 _strWriter = new StringWriter(); 50 XmlWriter cw = WriterHelper.Create(_strWriter, wSettings, overrideAsync: true, async: utils.Async); 51 XmlWriterSettings cws = settings.Clone(); 52 cws.CheckCharacters = true; 53 w = WriterHelper.Create(cw, cws, overrideAsync: true, async: utils.Async); 54 break; 55 default: 56 throw new Exception("Unknown writer type"); 57 } 58 return w; 59 } 60 VerifyOutput(string expected)61 private void VerifyOutput(string expected) 62 { 63 string actual = _strWriter.ToString(); 64 65 if (actual != expected) 66 { 67 CError.WriteLineIgnore("Expected: " + expected); 68 CError.WriteLineIgnore("Actual: " + actual); 69 CError.Compare(false, "Expected and actual output differ!"); 70 } 71 } 72 73 [Theory] 74 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting)] NS_Handling_1(XmlWriterUtils utils)75 public void NS_Handling_1(XmlWriterUtils utils) 76 { 77 XmlWriterSettings wSettings = new XmlWriterSettings(); 78 CError.Compare(wSettings.NamespaceHandling, NamespaceHandling.Default, "Incorrect default value for XmlWriterSettings.NamespaceHandling"); 79 80 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 81 { 82 CError.Compare(w.Settings.NamespaceHandling, NamespaceHandling.Default, "Incorrect default value for XmlWriter.Settings.NamespaceHandling"); 83 } 84 return; 85 } 86 87 [Theory] 88 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 89 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_2(XmlWriterUtils utils, NamespaceHandling nsHandling)90 public void NS_Handling_2(XmlWriterUtils utils, NamespaceHandling nsHandling) 91 { 92 XmlWriterSettings wSettings = new XmlWriterSettings(); 93 wSettings.NamespaceHandling = nsHandling; 94 95 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 96 { 97 CError.Compare(w != null, "XmlWriter creation failed"); 98 CError.Compare(w.Settings.NamespaceHandling, nsHandling, "Invalid NamespaceHandling assignment"); 99 } 100 return; 101 } 102 103 [Theory] 104 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting)] NS_Handling_2a(XmlWriterUtils utils)105 public void NS_Handling_2a(XmlWriterUtils utils) 106 { 107 XmlWriterSettings wSettings = new XmlWriterSettings(); 108 wSettings.NamespaceHandling = NamespaceHandling.Default | NamespaceHandling.OmitDuplicates; 109 110 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 111 { 112 CError.Compare(w.Settings.NamespaceHandling, NamespaceHandling.OmitDuplicates, "Invalid NamespaceHandling assignment"); 113 } 114 return; 115 } 116 117 [Theory] 118 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 119 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_3(XmlWriterUtils utils, NamespaceHandling nsHandling)120 public void NS_Handling_3(XmlWriterUtils utils, NamespaceHandling nsHandling) 121 { 122 XmlWriterSettings wSettings = new XmlWriterSettings(); 123 wSettings.NamespaceHandling = NamespaceHandling.OmitDuplicates; 124 wSettings.NamespaceHandling = nsHandling; 125 126 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 127 { 128 wSettings.NamespaceHandling = NamespaceHandling.Default; 129 CError.Compare(w != null, "XmlWriter creation failed"); 130 CError.Compare(w.Settings.NamespaceHandling, nsHandling, "Invalid NamespaceHandling assignment"); 131 } 132 return; 133 } 134 135 [Theory] 136 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 137 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_3a(XmlWriterUtils utils, NamespaceHandling nsHandling)138 public void NS_Handling_3a(XmlWriterUtils utils, NamespaceHandling nsHandling) 139 { 140 XmlWriterSettings wSettings = new XmlWriterSettings(); 141 wSettings.NamespaceHandling = nsHandling; 142 try 143 { 144 wSettings.NamespaceHandling = (NamespaceHandling)(-1); 145 CError.Compare(false, "Failed"); 146 } 147 catch (ArgumentOutOfRangeException) 148 { 149 try 150 { 151 wSettings.NamespaceHandling = (NamespaceHandling)(999); 152 CError.Compare(false, "Failed2"); 153 } 154 catch (ArgumentOutOfRangeException) { } 155 } 156 157 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 158 { 159 CError.Compare(w != null, "XmlWriter creation failed"); 160 CError.Compare(w.Settings.NamespaceHandling, nsHandling, "Invalid NamespaceHandling assignment"); 161 } 162 return; 163 } 164 165 [Theory] 166 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<root><p:foo xmlns:p=\"uri\"><a xmlns:p=\"uri\" /></p:foo></root>", null)] 167 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<root><p:foo xmlns:p=\"uri\"><a xmlns:p=\"uri\" /></p:foo></root>", "<root><p:foo xmlns:p=\"uri\"><a /></p:foo></root>")] 168 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<root><foo xmlns=\"uri\"><a xmlns=\"uri\" /></foo></root>", null)] 169 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<root><foo xmlns=\"uri\"><a xmlns=\"uri\" /></foo></root>", "<root><foo xmlns=\"uri\"><a /></foo></root>")] 170 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<root><p:foo xmlns:p=\"uri\"><a xmlns:p=\"uriOther\" /></p:foo></root>", null)] 171 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<root><p:foo xmlns:p=\"uri\"><a xmlns:p=\"uriOther\" /></p:foo></root>", null)] 172 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<root><p:foo xmlns:p=\"uri\"><a xmlns:pOther=\"uri\" /></p:foo></root>", null)] 173 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<root><p:foo xmlns:p=\"uri\"><a xmlns:pOther=\"uri\" /></p:foo></root>", null)] 174 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<root xmlns:p=\"uri\"><p:foo><p:a xmlns:p=\"uri\" /></p:foo></root>", null)] 175 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<root xmlns:p=\"uri\"><p:foo><p:a xmlns:p=\"uri\" /></p:foo></root>", "<root xmlns:p=\"uri\"><p:foo><p:a /></p:foo></root>")] 176 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<root xmlns:p=\"uri\"><p:foo><a xmlns:p=\"uri\" /></p:foo></root>", null)] 177 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<root xmlns:p=\"uri\"><p:foo><a xmlns:p=\"uri\" /></p:foo></root>", "<root xmlns:p=\"uri\"><p:foo><a /></p:foo></root>")] 178 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<root><p xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" /></root>", null)] 179 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<root><p xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" /></root>", "<root><p /></root>")] 180 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<a xmlns=\"p1\"><b xmlns=\"p2\"><c xmlns=\"p1\" /></b><d xmlns=\"\"><e xmlns=\"p1\"><f xmlns=\"\" /></e></d></a>", null)] 181 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<a xmlns=\"p1\"><b xmlns=\"p2\"><c xmlns=\"p1\" /></b><d xmlns=\"\"><e xmlns=\"p1\"><f xmlns=\"\" /></e></d></a>", null)] 182 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<root> <elem1 xmlns=\"urn:namespace\" att1=\"foo\" /></root>", null)] 183 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<root> <elem1 xmlns=\"urn:namespace\" att1=\"foo\" /></root>", null)] 184 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<a xmlns:p=\"p1\"><b xmlns:a=\"p1\"><c xmlns:b=\"p1\" /></b></a>", null)] 185 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<a xmlns:p=\"p1\"><b xmlns:a=\"p1\"><c xmlns:b=\"p1\" /></b></a>", null)] 186 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<a xmlns:p=\"p1\"><b xmlns:p=\"p2\"><c xmlns:p=\"p3\" /></b></a>", null)] 187 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<a xmlns:p=\"p1\"><b xmlns:p=\"p2\"><c xmlns:p=\"p3\" /></b></a>", null)] 188 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "<a xmlns:p=\"p1\"><b xmlns:p=\"p2\"><c xmlns:p=\"p1\" /></b></a>", null)] 189 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "<a xmlns:p=\"p1\"><b xmlns:p=\"p2\"><c xmlns:p=\"p1\" /></b></a>", null)] NS_Handling_3b(XmlWriterUtils utils, NamespaceHandling nsHandling, string xml, string exp)190 public void NS_Handling_3b(XmlWriterUtils utils, NamespaceHandling nsHandling, string xml, string exp) 191 { 192 XmlWriterSettings wSettings = new XmlWriterSettings(); 193 wSettings.NamespaceHandling = nsHandling; 194 195 using (XmlReader r = ReaderHelper.Create(new StringReader(xml))) 196 { 197 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 198 { 199 w.WriteNode(r, false); 200 w.Dispose(); 201 VerifyOutput(exp == null ? xml : exp); 202 } 203 } 204 return; 205 } 206 207 [Theory] 208 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 209 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_4a(XmlWriterUtils utils, NamespaceHandling nsHandling)210 public void NS_Handling_4a(XmlWriterUtils utils, NamespaceHandling nsHandling) 211 { 212 XmlWriterSettings wSettings = new XmlWriterSettings(); 213 wSettings.NamespaceHandling = nsHandling; 214 using (XmlWriter ww = CreateMemWriter(utils, wSettings)) 215 { 216 XmlWriterSettings ws = wSettings.Clone(); 217 ws.NamespaceHandling = NamespaceHandling.Default; 218 ws.CheckCharacters = true; 219 using (XmlWriter w = WriterHelper.Create(ww, ws, overrideAsync: true, async: utils.Async)) 220 { 221 CError.Compare(w != null, "XmlWriter creation failed"); 222 CError.Compare(w.Settings.NamespaceHandling, nsHandling, "Invalid NamespaceHandling assignment"); 223 } 224 } 225 return; 226 } 227 228 [Theory] 229 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 230 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_4b(XmlWriterUtils utils, NamespaceHandling nsHandling)231 public void NS_Handling_4b(XmlWriterUtils utils, NamespaceHandling nsHandling) 232 { 233 XmlWriterSettings wSettings = new XmlWriterSettings(); 234 wSettings.NamespaceHandling = nsHandling; 235 using (XmlWriter ww = CreateMemWriter(utils, wSettings)) 236 { 237 XmlWriterSettings ws = wSettings.Clone(); 238 ws.NamespaceHandling = NamespaceHandling.OmitDuplicates; 239 ws.CheckCharacters = true; 240 using (XmlWriter w = WriterHelper.Create(ww, ws, overrideAsync: true, async: utils.Async)) 241 { 242 CError.Compare(w != null, "XmlWriter creation failed"); 243 CError.Compare(w.Settings.NamespaceHandling, nsHandling, "Invalid NamespaceHandling assignment"); 244 } 245 } 246 return; 247 } 248 249 [Theory] 250 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 251 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_5(XmlWriterUtils utils, NamespaceHandling nsHandling)252 public void NS_Handling_5(XmlWriterUtils utils, NamespaceHandling nsHandling) 253 { 254 XmlWriterSettings wSettings = new XmlWriterSettings(); 255 wSettings.NamespaceHandling = nsHandling; 256 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 257 { 258 w.WriteStartElement("root", "uri"); 259 w.WriteStartAttribute("xmlns", "p", "http://www.w3.org/2000/xmlns/"); 260 w.WriteString("uri"); 261 w.WriteEndElement(); 262 } 263 VerifyOutput("<root xmlns:p=\"uri\" xmlns=\"uri\" />"); 264 return; 265 } 266 267 [Theory] 268 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 269 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_6(XmlWriterUtils utils, NamespaceHandling nsHandling)270 public void NS_Handling_6(XmlWriterUtils utils, NamespaceHandling nsHandling) 271 { 272 XmlWriterSettings wSettings = new XmlWriterSettings(); 273 wSettings.NamespaceHandling = nsHandling; 274 275 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 276 { 277 w.WriteStartElement(null, "e", "ns"); 278 w.WriteAttributeString(null, "attr", "ns", "val"); 279 w.WriteElementString(null, "el", "ns", "val"); 280 } 281 VerifyOutput("<e p1:attr=\"val\" xmlns:p1=\"ns\" xmlns=\"ns\"><p1:el>val</p1:el></e>"); 282 return; 283 } 284 285 [Theory] 286 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 287 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_7(XmlWriterUtils utils, NamespaceHandling nsHandling)288 public void NS_Handling_7(XmlWriterUtils utils, NamespaceHandling nsHandling) 289 { 290 XmlWriterSettings wSettings = new XmlWriterSettings(); 291 wSettings.NamespaceHandling = nsHandling; 292 293 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 294 { 295 w.WriteStartElement(String.Empty, "e", "ns"); 296 w.WriteAttributeString(String.Empty, "attr", "ns", "val"); 297 w.WriteElementString(String.Empty, "el", "ns", "val"); 298 } 299 VerifyOutput("<e p1:attr=\"val\" xmlns:p1=\"ns\" xmlns=\"ns\"><el>val</el></e>"); 300 return; 301 } 302 303 [Theory] 304 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 305 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_8(XmlWriterUtils utils, NamespaceHandling nsHandling)306 public void NS_Handling_8(XmlWriterUtils utils, NamespaceHandling nsHandling) 307 { 308 XmlWriterSettings wSettings = new XmlWriterSettings(); 309 wSettings.NamespaceHandling = nsHandling; 310 311 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 312 { 313 w.WriteStartElement("a", "e", "ns"); 314 w.WriteAttributeString("a", "attr", "ns", "val"); 315 w.WriteElementString("a", "el", "ns", "val"); 316 } 317 VerifyOutput("<a:e a:attr=\"val\" xmlns:a=\"ns\"><a:el>val</a:el></a:e>"); 318 return; 319 } 320 321 [Theory] 322 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 323 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_9(XmlWriterUtils utils, NamespaceHandling nsHandling)324 public void NS_Handling_9(XmlWriterUtils utils, NamespaceHandling nsHandling) 325 { 326 XmlWriterSettings wSettings = new XmlWriterSettings(); 327 wSettings.NamespaceHandling = nsHandling; 328 329 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 330 { 331 w.WriteStartElement("e", "ns"); 332 w.WriteAttributeString("attr", "ns", "val"); 333 w.WriteElementString("el", "ns", "val"); 334 } 335 VerifyOutput("<e p1:attr=\"val\" xmlns:p1=\"ns\" xmlns=\"ns\"><p1:el>val</p1:el></e>"); 336 return; 337 } 338 339 [Theory] 340 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 341 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_10(XmlWriterUtils utils, NamespaceHandling nsHandling)342 public void NS_Handling_10(XmlWriterUtils utils, NamespaceHandling nsHandling) 343 { 344 XmlWriterSettings wSettings = new XmlWriterSettings(); 345 wSettings.NamespaceHandling = nsHandling; 346 347 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 348 { 349 w.WriteStartElement(null, "e", null); 350 w.WriteAttributeString(null, "attr", null, "val"); 351 w.WriteElementString(null, "el", null, "val"); 352 } 353 VerifyOutput("<e attr=\"val\"><el>val</el></e>"); 354 return; 355 } 356 357 [Theory] 358 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 359 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_11(XmlWriterUtils utils, NamespaceHandling nsHandling)360 public void NS_Handling_11(XmlWriterUtils utils, NamespaceHandling nsHandling) 361 { 362 XmlWriterSettings wSettings = new XmlWriterSettings(); 363 wSettings.NamespaceHandling = nsHandling; 364 365 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 366 { 367 w.WriteStartElement(String.Empty, "e", String.Empty); 368 w.WriteAttributeString(String.Empty, "attr", String.Empty, "val"); 369 w.WriteElementString(String.Empty, "el", String.Empty, "val"); 370 } 371 VerifyOutput("<e attr=\"val\"><el>val</el></e>"); 372 return; 373 } 374 375 [Theory] 376 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 377 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_12(XmlWriterUtils utils, NamespaceHandling nsHandling)378 public void NS_Handling_12(XmlWriterUtils utils, NamespaceHandling nsHandling) 379 { 380 XmlWriterSettings wSettings = new XmlWriterSettings(); 381 wSettings.NamespaceHandling = nsHandling; 382 383 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 384 { 385 w.WriteStartElement("e"); 386 w.WriteAttributeString("attr", "val"); 387 w.WriteElementString("el", "val"); 388 } 389 VerifyOutput("<e attr=\"val\"><el>val</el></e>"); 390 return; 391 } 392 393 [Theory] 394 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 395 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_16(XmlWriterUtils utils, NamespaceHandling nsHandling)396 public void NS_Handling_16(XmlWriterUtils utils, NamespaceHandling nsHandling) 397 { 398 XmlWriterSettings wSettings = new XmlWriterSettings(); 399 wSettings.NamespaceHandling = nsHandling; 400 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 401 { 402 w.WriteStartElement("a", "foo", "b"); 403 CError.Compare(w.LookupPrefix("foo"), null, "FailedEl"); 404 w.WriteAttributeString("a", "foo", "b"); 405 CError.Compare(w.LookupPrefix("foo"), "p1", "FailedAttr"); 406 w.WriteElementString("e", "foo", "b"); 407 CError.Compare(w.LookupPrefix("foo"), "p1", "FailedEl"); 408 } 409 VerifyOutput("<a:foo p1:a=\"b\" xmlns:p1=\"foo\" xmlns:a=\"b\"><p1:e>b</p1:e></a:foo>"); 410 return; 411 } 412 413 [Theory] 414 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 415 [XmlWriterInlineData(WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_17(XmlWriterUtils utils, NamespaceHandling nsHandling)416 public void NS_Handling_17(XmlWriterUtils utils, NamespaceHandling nsHandling) 417 { 418 XmlWriterSettings wSettings = new XmlWriterSettings(); 419 wSettings.NamespaceHandling = nsHandling; 420 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 421 { 422 w.WriteDocType("a", null, null, "<!ATTLIST Root a CDATA #IMPLIED>"); 423 w.WriteStartElement("Root"); 424 for (int i = 0; i < 1000; i++) 425 { 426 w.WriteAttributeString("a", "n" + i, "val"); 427 } 428 try 429 { 430 w.WriteAttributeString("a", "n" + 999, "val"); 431 CError.Compare(false, "Failed"); 432 } 433 catch (XmlException e) { CError.WriteLine(e); return; } 434 } 435 Assert.True(false); 436 } 437 438 [Theory] 439 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, true)] 440 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, true)] 441 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, false)] 442 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, false)] NS_Handling_17a(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr)443 public void NS_Handling_17a(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr) 444 { 445 XmlWriterSettings wSettings = new XmlWriterSettings(); 446 wSettings.NamespaceHandling = nsHandling; 447 448 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 449 { 450 w.WriteDocType("a", null, null, "<!ATTLIST Root a CDATA #IMPLIED>"); 451 w.WriteStartElement("Root"); 452 for (int i = 0; i < 10; i++) 453 { 454 if (isAttr) 455 w.WriteAttributeString("p", "a" + i, "n", "val"); 456 else 457 w.WriteElementString("p", "a" + i, "n", "val"); 458 } 459 } 460 string exp = isAttr ? 461 "<!DOCTYPE a [<!ATTLIST Root a CDATA #IMPLIED>]><Root p:a0=\"val\" p:a1=\"val\" p:a2=\"val\" p:a3=\"val\" p:a4=\"val\" p:a5=\"val\" p:a6=\"val\" p:a7=\"val\" p:a8=\"val\" p:a9=\"val\" xmlns:p=\"n\" />" : 462 "<!DOCTYPE a [<!ATTLIST Root a CDATA #IMPLIED>]><Root><p:a0 xmlns:p=\"n\">val</p:a0><p:a1 xmlns:p=\"n\">val</p:a1><p:a2 xmlns:p=\"n\">val</p:a2><p:a3 xmlns:p=\"n\">val</p:a3><p:a4 xmlns:p=\"n\">val</p:a4><p:a5 xmlns:p=\"n\">val</p:a5><p:a6 xmlns:p=\"n\">val</p:a6><p:a7 xmlns:p=\"n\">val</p:a7><p:a8 xmlns:p=\"n\">val</p:a8><p:a9 xmlns:p=\"n\">val</p:a9></Root>"; 463 VerifyOutput(exp); 464 return; 465 } 466 467 [Theory] 468 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, true)] 469 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, true)] 470 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, false)] 471 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, false)] NS_Handling_17b(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr)472 public void NS_Handling_17b(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr) 473 { 474 XmlWriterSettings wSettings = new XmlWriterSettings(); 475 wSettings.NamespaceHandling = nsHandling; 476 477 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 478 { 479 w.WriteStartElement("Root"); 480 for (int i = 0; i < 5; i++) 481 { 482 if (isAttr) 483 w.WriteAttributeString("p", "a" + i, "xmlns", "val"); 484 else 485 w.WriteElementString("p", "a" + i, "xmlns", "val"); 486 } 487 } 488 string exp = isAttr ? 489 "<Root p:a0=\"val\" p:a1=\"val\" p:a2=\"val\" p:a3=\"val\" p:a4=\"val\" xmlns:p=\"xmlns\" />" : 490 "<Root><p:a0 xmlns:p=\"xmlns\">val</p:a0><p:a1 xmlns:p=\"xmlns\">val</p:a1><p:a2 xmlns:p=\"xmlns\">val</p:a2><p:a3 xmlns:p=\"xmlns\">val</p:a3><p:a4 xmlns:p=\"xmlns\">val</p:a4></Root>"; 491 VerifyOutput(exp); 492 return; 493 } 494 495 [Theory] 496 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, true)] 497 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, true)] 498 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, false)] 499 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, false)] NS_Handling_17c(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr)500 public void NS_Handling_17c(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr) 501 { 502 XmlWriterSettings wSettings = new XmlWriterSettings(); 503 wSettings.NamespaceHandling = nsHandling; 504 505 XmlWriter w = CreateMemWriter(utils, wSettings); 506 w.WriteStartElement("Root"); 507 for (int i = 0; i < 5; i++) 508 { 509 if (isAttr) 510 w.WriteAttributeString("p" + i, "a", "n" + i, "val" + i); 511 else 512 w.WriteElementString("p" + i, "a", "n" + i, "val" + i); 513 } 514 try 515 { 516 if (isAttr) 517 { 518 w.WriteAttributeString("p", "a", "n" + 4, "val"); 519 CError.Compare(false, "Failed"); 520 } 521 else 522 w.WriteElementString("p", "a", "n" + 4, "val"); 523 } 524 catch (XmlException) { } 525 finally 526 { 527 w.Dispose(); 528 string exp = isAttr ? 529 "<Root p0:a=\"val0\" p1:a=\"val1\" p2:a=\"val2\" p3:a=\"val3\" p4:a=\"val4\"" : 530 "<Root><p0:a xmlns:p0=\"n0\">val0</p0:a><p1:a xmlns:p1=\"n1\">val1</p1:a><p2:a xmlns:p2=\"n2\">val2</p2:a><p3:a xmlns:p3=\"n3\">val3</p3:a><p4:a xmlns:p4=\"n4\">val4</p4:a><p:a xmlns:p=\"n4\">val</p:a></Root>"; 531 VerifyOutput(exp); 532 } 533 return; 534 } 535 536 [Theory] 537 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, true)] 538 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, true)] 539 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, false)] 540 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, false)] NS_Handling_17d(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr)541 public void NS_Handling_17d(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr) 542 { 543 XmlWriterSettings wSettings = new XmlWriterSettings(); 544 wSettings.NamespaceHandling = nsHandling; 545 546 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 547 { 548 w.WriteStartElement("Root"); 549 for (int i = 0; i < 5; i++) 550 { 551 if (isAttr) 552 { 553 w.WriteAttributeString("xml", "a" + i, "http://www.w3.org/XML/1998/namespace", "val"); 554 w.WriteAttributeString("xmlns", "a" + i, "http://www.w3.org/2000/xmlns/", "val"); 555 } 556 else 557 { 558 w.WriteElementString("xml", "a" + i, "http://www.w3.org/XML/1998/namespace", "val"); 559 } 560 } 561 } 562 string exp = isAttr ? 563 "<Root xml:a0=\"val\" xmlns:a0=\"val\" xml:a1=\"val\" xmlns:a1=\"val\" xml:a2=\"val\" xmlns:a2=\"val\" xml:a3=\"val\" xmlns:a3=\"val\" xml:a4=\"val\" xmlns:a4=\"val\" />" : 564 "<Root><xml:a0>val</xml:a0><xml:a1>val</xml:a1><xml:a2>val</xml:a2><xml:a3>val</xml:a3><xml:a4>val</xml:a4></Root>"; 565 VerifyOutput(exp); 566 return; 567 } 568 569 [Theory] 570 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, true)] 571 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, true)] 572 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, false)] 573 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, false)] NS_Handling_17e(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr)574 public void NS_Handling_17e(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr) 575 { 576 XmlWriterSettings wSettings = new XmlWriterSettings(); 577 wSettings.NamespaceHandling = nsHandling; 578 579 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 580 { 581 w.WriteStartElement("Root"); 582 for (int i = 0; i < 5; i++) 583 { 584 if (isAttr) 585 w.WriteAttributeString("a" + i, "http://www.w3.org/XML/1998/namespace", "val"); 586 else 587 w.WriteElementString("a" + i, "http://www.w3.org/XML/1998/namespace", "val"); 588 } 589 } 590 string exp = isAttr ? 591 "<Root xml:a0=\"val\" xml:a1=\"val\" xml:a2=\"val\" xml:a3=\"val\" xml:a4=\"val\" />" : 592 "<Root><xml:a0>val</xml:a0><xml:a1>val</xml:a1><xml:a2>val</xml:a2><xml:a3>val</xml:a3><xml:a4>val</xml:a4></Root>"; 593 VerifyOutput(exp); 594 return; 595 } 596 597 [Theory] 598 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 599 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_18(XmlWriterUtils utils, NamespaceHandling nsHandling)600 public void NS_Handling_18(XmlWriterUtils utils, NamespaceHandling nsHandling) 601 { 602 XmlWriterSettings wSettings = new XmlWriterSettings(); 603 wSettings.NamespaceHandling = nsHandling; 604 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 605 { 606 w.WriteStartElement("test"); 607 w.WriteAttributeString("p", "a1", "ns1", "v"); 608 w.WriteStartElement("base"); 609 w.WriteAttributeString("a2", "ns1", "v"); 610 w.WriteAttributeString("p", "a3", "ns2", "v"); 611 w.WriteElementString("p", "e", "ns2", "v"); 612 w.WriteEndElement(); 613 w.WriteEndElement(); 614 } 615 string exp = "<test p:a1=\"v\" xmlns:p=\"ns1\"><base p:a2=\"v\" p4:a3=\"v\" xmlns:p4=\"ns2\"><p:e xmlns:p=\"ns2\">v</p:e></base></test>"; 616 VerifyOutput(exp); 617 return; 618 } 619 620 [Theory] 621 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 622 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_19(XmlWriterUtils utils, NamespaceHandling nsHandling)623 public void NS_Handling_19(XmlWriterUtils utils, NamespaceHandling nsHandling) 624 { 625 XmlWriterSettings wSettings = new XmlWriterSettings(); 626 wSettings.NamespaceHandling = nsHandling; 627 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 628 { 629 w.WriteStartElement("Root"); 630 w.WriteAttributeString("xmlns", "xml", null, "http://www.w3.org/XML/1998/namespace"); 631 w.WriteAttributeString("xmlns", "space", null, "preserve"); 632 w.WriteAttributeString("xmlns", "lang", null, "chs"); 633 w.WriteElementString("xml", "lang", null, "jpn"); 634 w.WriteElementString("xml", "space", null, "default"); 635 w.WriteElementString("xml", "xml", null, "http://www.w3.org/XML/1998/namespace"); 636 w.WriteEndElement(); 637 } 638 string exp = (nsHandling == NamespaceHandling.OmitDuplicates) ? 639 "<Root xmlns:space=\"preserve\" xmlns:lang=\"chs\"><xml:lang>jpn</xml:lang><xml:space>default</xml:space><xml:xml>http://www.w3.org/XML/1998/namespace</xml:xml></Root>" : 640 "<Root xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:space=\"preserve\" xmlns:lang=\"chs\"><xml:lang>jpn</xml:lang><xml:space>default</xml:space><xml:xml>http://www.w3.org/XML/1998/namespace</xml:xml></Root>"; 641 VerifyOutput(exp); 642 return; 643 } 644 645 [Theory] 646 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "xmlns", "xml", true)] 647 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "xmlns", "xml", true)] 648 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "xmlns", "xml", false)] 649 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "xmlns", "xml", false)] 650 651 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "xml", "space", true)] 652 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "xml", "space", true)] 653 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "xmlns", "space", false)] 654 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "xmlns", "space", false)] 655 656 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "xmlns", "lang", true)] 657 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "xmlns", "lang", true)] 658 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, "xmlns", "lang", false)] 659 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, "xmlns", "lang", false)] NS_Handling_19a(XmlWriterUtils utils, NamespaceHandling nsHandling, string prefix, string name, bool isAttr)660 public void NS_Handling_19a(XmlWriterUtils utils, NamespaceHandling nsHandling, string prefix, string name, bool isAttr) 661 { 662 XmlWriterSettings wSettings = new XmlWriterSettings(); 663 wSettings.NamespaceHandling = nsHandling; 664 XmlWriter w = CreateMemWriter(utils, wSettings); 665 w.WriteStartElement("Root"); 666 try 667 { 668 if (isAttr) 669 w.WriteAttributeString(prefix, name, null, null); 670 else 671 w.WriteElementString(prefix, name, null, null); 672 CError.Compare(false, "error"); 673 } 674 catch (ArgumentException e) { CError.WriteLine(e); CError.Compare(w.WriteState, WriteState.Error, "state"); } 675 finally 676 { 677 w.Dispose(); 678 CError.Compare(w.WriteState, WriteState.Closed, "state"); 679 } 680 return; 681 } 682 683 [Theory] 684 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, true)] 685 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, true)] 686 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default, false)] 687 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates, false)] NS_Handling_19b(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr)688 public void NS_Handling_19b(XmlWriterUtils utils, NamespaceHandling nsHandling, bool isAttr) 689 { 690 XmlWriterSettings wSettings = new XmlWriterSettings(); 691 wSettings.NamespaceHandling = nsHandling; 692 693 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 694 { 695 w.WriteStartElement("Root"); 696 try 697 { 698 if (isAttr) 699 w.WriteAttributeString("xmlns", "xml", null, null); 700 else 701 w.WriteElementString("xmlns", "xml", null, null); 702 } 703 catch (ArgumentException e) { CError.WriteLine(e.Message); } 704 } 705 string exp = isAttr ? "<Root" : "<Root><xmlns:xml"; 706 VerifyOutput(exp); 707 return; 708 } 709 710 [Theory] 711 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 712 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_20(XmlWriterUtils utils, NamespaceHandling nsHandling)713 public void NS_Handling_20(XmlWriterUtils utils, NamespaceHandling nsHandling) 714 { 715 XmlWriterSettings wSettings = new XmlWriterSettings(); 716 wSettings.NamespaceHandling = nsHandling; 717 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 718 { 719 w.WriteStartElement("d", "Data", "http://example.org/data"); 720 w.WriteStartElement("g", "GoodStuff", "http://example.org/data/good"); 721 w.WriteAttributeString("hello", "world"); 722 w.WriteEndElement(); 723 w.WriteStartElement("BadStuff", "http://example.org/data/bad"); 724 w.WriteAttributeString("hello", "world"); 725 w.WriteEndElement(); 726 w.WriteEndElement(); 727 } 728 VerifyOutput("<d:Data xmlns:d=\"http://example.org/data\"><g:GoodStuff hello=\"world\" xmlns:g=\"http://example.org/data/good\" /><BadStuff hello=\"world\" xmlns=\"http://example.org/data/bad\" /></d:Data>"); 729 return; 730 } 731 732 [Theory] 733 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 734 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_21(XmlWriterUtils utils, NamespaceHandling nsHandling)735 public void NS_Handling_21(XmlWriterUtils utils, NamespaceHandling nsHandling) 736 { 737 XmlWriterSettings wSettings = new XmlWriterSettings(); 738 wSettings.NamespaceHandling = nsHandling; 739 string strraw = "abc"; 740 char[] buffer = strraw.ToCharArray(); 741 742 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 743 { 744 w.WriteStartElement("root"); 745 w.WriteStartAttribute("xml", "lang", null); 746 w.WriteRaw(buffer, 0, 0); 747 w.WriteRaw(buffer, 1, 1); 748 w.WriteRaw(buffer, 0, 2); 749 w.WriteEndElement(); 750 } 751 VerifyOutput("<root xml:lang=\"bab\" />"); 752 return; 753 } 754 755 [Theory] 756 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 757 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_22(XmlWriterUtils utils, NamespaceHandling nsHandling)758 public void NS_Handling_22(XmlWriterUtils utils, NamespaceHandling nsHandling) 759 { 760 XmlWriterSettings wSettings = new XmlWriterSettings(); 761 wSettings.NamespaceHandling = nsHandling; 762 byte[] buffer = new byte[] { (byte)'a', (byte)'b', (byte)'c' }; 763 764 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 765 { 766 w.WriteStartElement("root"); 767 w.WriteStartAttribute("xml", "lang", null); 768 w.WriteBinHex(buffer, 0, 0); 769 w.WriteBinHex(buffer, 1, 1); 770 w.WriteBinHex(buffer, 0, 2); 771 w.WriteEndElement(); 772 } 773 VerifyOutput("<root xml:lang=\"626162\" />"); 774 return; 775 } 776 777 [Theory] 778 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 779 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_23(XmlWriterUtils utils, NamespaceHandling nsHandling)780 public void NS_Handling_23(XmlWriterUtils utils, NamespaceHandling nsHandling) 781 { 782 XmlWriterSettings wSettings = new XmlWriterSettings(); 783 wSettings.NamespaceHandling = nsHandling; 784 byte[] buffer = new byte[] { (byte)'a', (byte)'b', (byte)'c' }; 785 786 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 787 { 788 w.WriteStartElement("root"); 789 w.WriteStartAttribute("a", "b", null); 790 w.WriteBase64(buffer, 0, 0); 791 w.WriteBase64(buffer, 1, 1); 792 w.WriteBase64(buffer, 0, 2); 793 w.WriteEndElement(); 794 } 795 VerifyOutput("<root b=\"YmFi\" />"); 796 return; 797 } 798 799 [Theory] 800 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 801 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_24(XmlWriterUtils utils, NamespaceHandling nsHandling)802 public void NS_Handling_24(XmlWriterUtils utils, NamespaceHandling nsHandling) 803 { 804 XmlWriterSettings wSettings = new XmlWriterSettings(); 805 wSettings.NamespaceHandling = nsHandling; 806 XmlWriter w = CreateMemWriter(utils, wSettings); 807 byte[] buffer = new byte[] { (byte)'a', (byte)'b', (byte)'c' }; 808 809 w.WriteStartElement("A"); 810 w.WriteAttributeString("xmlns", "p", null, "ns1"); 811 w.WriteStartElement("B"); 812 w.WriteAttributeString("xmlns", "p", null, "ns1"); // will be omitted 813 try 814 { 815 w.WriteAttributeString("xmlns", "p", null, "ns1"); 816 CError.Compare(false, "error"); 817 } 818 catch (XmlException e) { CError.WriteLine(e); } 819 finally 820 { 821 w.Dispose(); 822 VerifyOutput(nsHandling == NamespaceHandling.OmitDuplicates ? "<A xmlns:p=\"ns1\"><B" : "<A xmlns:p=\"ns1\"><B xmlns:p=\"ns1\""); 823 } 824 return; 825 } 826 827 [Theory] 828 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 829 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_25(XmlWriterUtils utils, NamespaceHandling nsHandling)830 public void NS_Handling_25(XmlWriterUtils utils, NamespaceHandling nsHandling) 831 { 832 string xml = "<employees xmlns:email=\"http://www.w3c.org/some-spec-3.2\">" + 833 "<employee><name>Bob Worker</name><address xmlns=\"http://postal.ie/spec-1.0\"><street>Nassau Street</street>" + 834 "<city>Dublin 3</city><country>Ireland</country></address><email:address>bob.worker@hisjob.ie</email:address>" + 835 "</employee></employees>"; 836 837 XmlWriterSettings wSettings = new XmlWriterSettings(); 838 wSettings.NamespaceHandling = nsHandling; 839 using (XmlReader r = ReaderHelper.Create(new StringReader(xml))) 840 { 841 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 842 { 843 w.WriteNode(r, false); 844 } 845 } 846 VerifyOutput(xml); 847 return; 848 } 849 850 [Theory] 851 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 852 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_25a(XmlWriterUtils utils, NamespaceHandling nsHandling)853 public void NS_Handling_25a(XmlWriterUtils utils, NamespaceHandling nsHandling) 854 { 855 string xml = "<root><elem1 xmlns=\"urn:URN1\" xmlns:ns1=\"urn:URN2\"><ns1:childElem1><grandChild1 /></ns1:childElem1><childElem2><grandChild2 /></childElem2></elem1></root>"; 856 857 XmlWriterSettings wSettings = new XmlWriterSettings(); 858 wSettings.NamespaceHandling = nsHandling; 859 using (XmlReader r = ReaderHelper.Create(new StringReader(xml))) 860 { 861 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 862 { 863 w.WriteNode(r, false); 864 } 865 } 866 VerifyOutput(xml); 867 return; 868 } 869 870 [Theory] 871 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 872 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_26(XmlWriterUtils utils, NamespaceHandling nsHandling)873 public void NS_Handling_26(XmlWriterUtils utils, NamespaceHandling nsHandling) 874 { 875 XmlWriterSettings wSettings = new XmlWriterSettings(); 876 wSettings.NamespaceHandling = nsHandling; 877 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 878 { 879 w.WriteStartElement("p", "e", "uri1"); 880 w.WriteAttributeString("p", "e", "uri1", "val"); 881 w.WriteAttributeString("p", "e", "uri2", "val"); 882 w.WriteElementString("p", "e", "uri1", "val"); 883 w.WriteElementString("p", "e", "uri2", "val"); 884 } 885 VerifyOutput("<p:e p:e=\"val\" p1:e=\"val\" xmlns:p1=\"uri2\" xmlns:p=\"uri1\"><p:e>val</p:e><p:e xmlns:p=\"uri2\">val</p:e></p:e>"); 886 return; 887 } 888 889 [Theory] 890 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 891 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_27(XmlWriterUtils utils, NamespaceHandling nsHandling)892 public void NS_Handling_27(XmlWriterUtils utils, NamespaceHandling nsHandling) 893 { 894 XmlWriterSettings wSettings = new XmlWriterSettings(); 895 wSettings.NamespaceHandling = nsHandling; 896 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 897 { 898 w.WriteStartElement("p1", "e", "uri"); 899 w.WriteAttributeString("p1", "e", "uri", "val"); 900 w.WriteAttributeString("p2", "e2", "uri", "val"); 901 w.WriteElementString("p1", "e", "uri", "val"); 902 w.WriteElementString("p2", "e", "uri", "val"); 903 } 904 VerifyOutput("<p1:e p1:e=\"val\" p2:e2=\"val\" xmlns:p2=\"uri\" xmlns:p1=\"uri\"><p1:e>val</p1:e><p2:e>val</p2:e></p1:e>"); 905 return; 906 } 907 908 [Theory] 909 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 910 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_29(XmlWriterUtils utils, NamespaceHandling nsHandling)911 public void NS_Handling_29(XmlWriterUtils utils, NamespaceHandling nsHandling) 912 { 913 string xml = "<!DOCTYPE root [ <!ELEMENT root ANY > <!ELEMENT ns1:elem1 ANY >" + 914 "<!ATTLIST ns1:elem1 xmlns CDATA #FIXED \"urn:URN2\"> <!ATTLIST ns1:elem1 xmlns:ns1 CDATA #FIXED \"urn:URN1\">" + 915 "<!ELEMENT childElem1 ANY > <!ATTLIST childElem1 childElem1Att1 CDATA #FIXED \"attributeValue\">]>" + 916 "<root> <ns1:elem1 xmlns:ns1=\"urn:URN1\" xmlns=\"urn:URN2\"> text node in elem1 <![CDATA[<doc> content </doc>]]>" + 917 "<childElem1 childElem1Att1=\"attributeValue\"> <?PI in childElem1 ?> </childElem1> <!-- Comment in elem1 --> & </ns1:elem1></root>"; 918 919 XmlWriterSettings wSettings = new XmlWriterSettings(); 920 wSettings.NamespaceHandling = nsHandling; 921 XmlReaderSettings rs = new XmlReaderSettings(); 922 rs.DtdProcessing = DtdProcessing.Parse; 923 using (XmlReader r = ReaderHelper.Create(new StringReader(xml), rs)) 924 { 925 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 926 { 927 w.WriteNode(r, false); 928 } 929 } 930 VerifyOutput(xml); 931 return; 932 } 933 934 [Theory] 935 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 936 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_30(XmlWriterUtils utils, NamespaceHandling nsHandling)937 public void NS_Handling_30(XmlWriterUtils utils, NamespaceHandling nsHandling) 938 { 939 string xml = "<!DOCTYPE doc " + 940 "[<!ELEMENT doc ANY>" + 941 "<!ELEMENT test1 (#PCDATA)>" + 942 "<!ELEMENT test2 ANY>" + 943 "<!ELEMENT test3 (#PCDATA)>" + 944 "<!ENTITY e1 \"&e2;\">" + 945 "<!ENTITY e2 \"xmlns:p='x'\">" + 946 "<!ATTLIST test3 a1 CDATA #IMPLIED>" + 947 "<!ATTLIST test3 a2 CDATA #IMPLIED>" + 948 "]>" + 949 "<doc xmlns:p='&e2;'>" + 950 " &e2;" + 951 " <test1 xmlns:p='&e2;'>AA&e2;AA</test1>" + 952 " <test2 xmlns:p='&e1;'>BB&e1;BB</test2>" + 953 " <test3 a1=\"&e2;\" a2=\"&e1;\">World</test3>" + 954 "</doc>"; 955 string exp = (nsHandling == NamespaceHandling.OmitDuplicates) ? 956 "<!DOCTYPE doc [<!ELEMENT doc ANY><!ELEMENT test1 (#PCDATA)><!ELEMENT test2 ANY><!ELEMENT test3 (#PCDATA)><!ENTITY e1 \"&e2;\"><!ENTITY e2 \"xmlns:p='x'\"><!ATTLIST test3 a1 CDATA #IMPLIED><!ATTLIST test3 a2 CDATA #IMPLIED>]><doc xmlns:p=\"xmlns:p='x'\"> xmlns:p='x' <test1>AAxmlns:p='x'AA</test1> <test2>BBxmlns:p='x'BB</test2> <test3 a1=\"xmlns:p='x'\" a2=\"xmlns:p='x'\">World</test3></doc>" : 957 "<!DOCTYPE doc [<!ELEMENT doc ANY><!ELEMENT test1 (#PCDATA)><!ELEMENT test2 ANY><!ELEMENT test3 (#PCDATA)><!ENTITY e1 \"&e2;\"><!ENTITY e2 \"xmlns:p='x'\"><!ATTLIST test3 a1 CDATA #IMPLIED><!ATTLIST test3 a2 CDATA #IMPLIED>]><doc xmlns:p=\"xmlns:p='x'\"> xmlns:p='x' <test1 xmlns:p=\"xmlns:p='x'\">AAxmlns:p='x'AA</test1> <test2 xmlns:p=\"xmlns:p='x'\">BBxmlns:p='x'BB</test2> <test3 a1=\"xmlns:p='x'\" a2=\"xmlns:p='x'\">World</test3></doc>"; 958 959 XmlWriterSettings wSettings = new XmlWriterSettings(); 960 wSettings.NamespaceHandling = nsHandling; 961 XmlReaderSettings rs = new XmlReaderSettings(); 962 rs.DtdProcessing = DtdProcessing.Parse; 963 using (XmlReader r = ReaderHelper.Create(new StringReader(xml), rs)) 964 { 965 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 966 { 967 w.WriteNode(r, false); 968 } 969 } 970 VerifyOutput(exp); 971 return; 972 } 973 974 [Theory] 975 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 976 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_30a(XmlWriterUtils utils, NamespaceHandling nsHandling)977 public void NS_Handling_30a(XmlWriterUtils utils, NamespaceHandling nsHandling) 978 { 979 string xml = "<!DOCTYPE doc " + 980 "[<!ELEMENT doc ANY>" + 981 "<!ELEMENT test1 (#PCDATA)>" + 982 "<!ELEMENT test2 ANY>" + 983 "<!ELEMENT test3 (#PCDATA)>" + 984 "<!ENTITY e1 \"&e2;\">" + 985 "<!ENTITY e2 \"xmlns='x'\">" + 986 "<!ATTLIST test3 a1 CDATA #IMPLIED>" + 987 "<!ATTLIST test3 a2 CDATA #IMPLIED>" + 988 "]>" + 989 "<doc xmlns:p='&e2;'>" + 990 " &e2;" + 991 " <test1 xmlns:p='&e2;'>AA&e2;AA</test1>" + 992 " <test2 xmlns:p='&e1;'>BB&e1;BB</test2>" + 993 " <test3 a1=\"&e2;\" a2=\"&e1;\">World</test3>" + 994 "</doc>"; 995 string exp = (nsHandling == NamespaceHandling.OmitDuplicates) ? 996 "<!DOCTYPE doc [<!ELEMENT doc ANY><!ELEMENT test1 (#PCDATA)><!ELEMENT test2 ANY><!ELEMENT test3 (#PCDATA)><!ENTITY e1 \"&e2;\"><!ENTITY e2 \"xmlns='x'\"><!ATTLIST test3 a1 CDATA #IMPLIED><!ATTLIST test3 a2 CDATA #IMPLIED>]><doc xmlns:p=\"xmlns='x'\"> xmlns='x' <test1>AAxmlns='x'AA</test1> <test2>BBxmlns='x'BB</test2> <test3 a1=\"xmlns='x'\" a2=\"xmlns='x'\">World</test3></doc>" : 997 "<!DOCTYPE doc [<!ELEMENT doc ANY><!ELEMENT test1 (#PCDATA)><!ELEMENT test2 ANY><!ELEMENT test3 (#PCDATA)><!ENTITY e1 \"&e2;\"><!ENTITY e2 \"xmlns='x'\"><!ATTLIST test3 a1 CDATA #IMPLIED><!ATTLIST test3 a2 CDATA #IMPLIED>]><doc xmlns:p=\"xmlns='x'\"> xmlns='x' <test1 xmlns:p=\"xmlns='x'\">AAxmlns='x'AA</test1> <test2 xmlns:p=\"xmlns='x'\">BBxmlns='x'BB</test2> <test3 a1=\"xmlns='x'\" a2=\"xmlns='x'\">World</test3></doc>"; 998 999 XmlWriterSettings wSettings = new XmlWriterSettings(); 1000 wSettings.NamespaceHandling = nsHandling; 1001 XmlReaderSettings rs = new XmlReaderSettings(); 1002 rs.DtdProcessing = DtdProcessing.Parse; 1003 using (XmlReader r = ReaderHelper.Create(new StringReader(xml), rs)) 1004 { 1005 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 1006 { 1007 w.WriteNode(r, false); 1008 } 1009 } 1010 VerifyOutput(exp); 1011 return; 1012 } 1013 1014 [Theory] 1015 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.Default)] 1016 [XmlWriterInlineData(~WriterType.Async & WriterType.AllButCustom & WriterType.AllButIndenting, NamespaceHandling.OmitDuplicates)] NS_Handling_31(XmlWriterUtils utils, NamespaceHandling nsHandling)1017 public void NS_Handling_31(XmlWriterUtils utils, NamespaceHandling nsHandling) 1018 { 1019 XmlWriterSettings wSettings = new XmlWriterSettings(); 1020 wSettings.NamespaceHandling = nsHandling; 1021 using (XmlWriter w = CreateMemWriter(utils, wSettings)) 1022 { 1023 w.WriteStartElement("test"); 1024 w.WriteAttributeString("p", "a1", "ns1", "v"); 1025 w.WriteStartElement("base"); 1026 w.WriteAttributeString("a2", "ns1", "v"); 1027 w.WriteAttributeString("p", "a3", "ns2", "v"); 1028 w.WriteEndElement(); 1029 w.WriteEndElement(); 1030 } 1031 VerifyOutput("<test p:a1=\"v\" xmlns:p=\"ns1\"><base p:a2=\"v\" p4:a3=\"v\" xmlns:p4=\"ns2\" /></test>"); 1032 return; 1033 } 1034 } 1035 } 1036 1037