XmlSchemaTests.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. //
  2. // System.Xml.XmlSchemaTests.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // (C) 2002 Atsushi Enomoto
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Net;
  12. using System.Xml;
  13. using System.Xml.Schema;
  14. using System.Xml.Serialization;
  15. using NUnit.Framework;
  16. namespace MonoTests.System.Xml
  17. {
  18. [TestFixture]
  19. public class XmlSchemaTests : XmlSchemaAssertion
  20. {
  21. static readonly bool StrictMsCompliant = Environment.GetEnvironmentVariable ("MONO_STRICT_MS_COMPLIANT") == "yes";
  22. [Test]
  23. public void TestRead ()
  24. {
  25. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
  26. Assert.AreEqual (6, schema.Items.Count);
  27. bool fooValidated = false;
  28. bool barValidated = false;
  29. string ns = "urn:bar";
  30. foreach (XmlSchemaObject obj in schema.Items) {
  31. XmlSchemaElement element = obj as XmlSchemaElement;
  32. if (element == null)
  33. continue;
  34. if (element.Name == "Foo") {
  35. AssertElement (element, "Foo",
  36. XmlQualifiedName.Empty, null,
  37. QName ("string", XmlSchema.Namespace), null);
  38. fooValidated = true;
  39. }
  40. if (element.Name == "Bar") {
  41. AssertElement (element, "Bar",
  42. XmlQualifiedName.Empty, null, QName ("FugaType", ns), null);
  43. barValidated = true;
  44. }
  45. }
  46. Assert.IsTrue (fooValidated);
  47. Assert.IsTrue (barValidated);
  48. }
  49. [Test]
  50. public void TestReadFlags ()
  51. {
  52. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
  53. schema.Compile (null);
  54. XmlSchemaElement el = schema.Items [0] as XmlSchemaElement;
  55. Assert.IsNotNull (el);
  56. Assert.AreEqual (XmlSchemaDerivationMethod.Extension, el.Block);
  57. el = schema.Items [1] as XmlSchemaElement;
  58. Assert.IsNotNull (el);
  59. Assert.AreEqual (XmlSchemaDerivationMethod.Extension |
  60. XmlSchemaDerivationMethod.Restriction, el.Block);
  61. }
  62. [Test]
  63. public void TestWriteFlags ()
  64. {
  65. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/2.xsd");
  66. StringWriter sw = new StringWriter ();
  67. XmlTextWriter xtw = new XmlTextWriter (sw);
  68. schema.Write (xtw);
  69. }
  70. [Test]
  71. public void TestCompile ()
  72. {
  73. XmlQualifiedName qname;
  74. XmlSchemaComplexContentExtension xccx;
  75. XmlSchemaComplexType cType;
  76. XmlSchemaSequence seq;
  77. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/1.xsd");
  78. // Assert.IsTrue (!schema.IsCompiled);
  79. schema.Compile (null);
  80. Assert.IsTrue (schema.IsCompiled);
  81. string ns = "urn:bar";
  82. XmlSchemaElement foo = (XmlSchemaElement) schema.Elements [QName ("Foo", ns)];
  83. Assert.IsNotNull (foo);
  84. XmlSchemaDatatype stringDatatype = foo.ElementType as XmlSchemaDatatype;
  85. Assert.IsNotNull (stringDatatype);
  86. // HogeType
  87. qname = QName ("HogeType", ns);
  88. cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
  89. Assert.IsNotNull (cType);
  90. Assert.IsNull (cType.ContentModel);
  91. AssertCompiledComplexType (cType, qname, 0, 0,
  92. false, null, true, XmlSchemaContentType.ElementOnly);
  93. seq = cType.ContentTypeParticle as XmlSchemaSequence;
  94. Assert.IsNotNull (seq);
  95. Assert.AreEqual (2, seq.Items.Count);
  96. XmlSchemaElement refFoo = seq.Items [0] as XmlSchemaElement;
  97. AssertCompiledElement (refFoo, QName ("Foo", ns), stringDatatype);
  98. // FugaType
  99. qname = QName ("FugaType", ns);
  100. cType = schema.SchemaTypes [qname] as XmlSchemaComplexType;
  101. Assert.IsNotNull (cType);
  102. xccx = cType.ContentModel.Content as XmlSchemaComplexContentExtension;
  103. AssertCompiledComplexContentExtension (
  104. xccx, 0, false, QName ("HogeType", ns));
  105. AssertCompiledComplexType (cType, qname, 0, 0,
  106. false, typeof (XmlSchemaComplexContent),
  107. true, XmlSchemaContentType.ElementOnly);
  108. Assert.IsNotNull (cType.BaseSchemaType);
  109. seq = xccx.Particle as XmlSchemaSequence;
  110. Assert.IsNotNull (seq);
  111. Assert.AreEqual (1, seq.Items.Count);
  112. XmlSchemaElement refBaz = seq.Items [0] as XmlSchemaElement;
  113. Assert.IsNotNull (refBaz);
  114. AssertCompiledElement (refBaz, QName ("Baz", ""), stringDatatype);
  115. qname = QName ("Bar", ns);
  116. XmlSchemaElement element = schema.Elements [qname] as XmlSchemaElement;
  117. AssertCompiledElement (element, qname, cType);
  118. }
  119. [Test]
  120. [ExpectedException (typeof (XmlSchemaException))]
  121. public void TestCompile_ZeroLength_TargetNamespace ()
  122. {
  123. XmlSchema schema = new XmlSchema ();
  124. schema.TargetNamespace = string.Empty;
  125. Assert.IsTrue (!schema.IsCompiled);
  126. // MS.NET 1.x: The Namespace '' is an invalid URI.
  127. // MS.NET 2.0: The targetNamespace attribute cannot have empty string as its value.
  128. schema.Compile (null);
  129. }
  130. [Test]
  131. [ExpectedException (typeof (XmlSchemaException))]
  132. public void TestCompileNonSchema ()
  133. {
  134. XmlTextReader xtr = new XmlTextReader ("<root/>", XmlNodeType.Document, null);
  135. XmlSchema schema = XmlSchema.Read (xtr, null);
  136. xtr.Close ();
  137. }
  138. [Test]
  139. public void TestSimpleImport ()
  140. {
  141. XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/3.xsd"), null);
  142. Assert.AreEqual ("urn:foo", schema.TargetNamespace);
  143. XmlSchemaImport import = schema.Includes [0] as XmlSchemaImport;
  144. Assert.IsNotNull (import);
  145. schema.Compile (null);
  146. Assert.AreEqual (4, schema.Elements.Count);
  147. Assert.IsNotNull (schema.Elements [QName ("Foo", "urn:foo")]);
  148. Assert.IsNotNull (schema.Elements [QName ("Bar", "urn:foo")]);
  149. Assert.IsNotNull (schema.Elements [QName ("Foo", "urn:bar")]);
  150. Assert.IsNotNull (schema.Elements [QName ("Bar", "urn:bar")]);
  151. }
  152. [Test]
  153. public void TestSimpleMutualImport ()
  154. {
  155. XmlReader r = new XmlTextReader ("Test/XmlFiles/xsd/inter-inc-1.xsd");
  156. try {
  157. XmlSchema.Read (r, null).Compile (null);
  158. } finally {
  159. r.Close ();
  160. }
  161. }
  162. [Test]
  163. public void TestQualification ()
  164. {
  165. XmlSchema schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/5.xsd"), null);
  166. schema.Compile (null);
  167. XmlSchemaElement el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  168. Assert.IsNotNull (el);
  169. XmlSchemaComplexType ct = el.ElementType as XmlSchemaComplexType;
  170. XmlSchemaSequence seq = ct.ContentTypeParticle as XmlSchemaSequence;
  171. XmlSchemaElement elp = seq.Items [0] as XmlSchemaElement;
  172. Assert.AreEqual (QName ("Bar", ""), elp.QualifiedName);
  173. schema = XmlSchema.Read (new XmlTextReader ("Test/XmlFiles/xsd/6.xsd"), null);
  174. schema.Compile (null);
  175. el = schema.Elements [QName ("Foo", "urn:bar")] as XmlSchemaElement;
  176. Assert.IsNotNull (el);
  177. ct = el.ElementType as XmlSchemaComplexType;
  178. seq = ct.ContentTypeParticle as XmlSchemaSequence;
  179. elp = seq.Items [0] as XmlSchemaElement;
  180. Assert.AreEqual (QName ("Bar", "urn:bar"), elp.QualifiedName);
  181. }
  182. [Test]
  183. public void TestWriteNamespaces ()
  184. {
  185. XmlDocument doc = new XmlDocument ();
  186. XmlSchema xs;
  187. StringWriter sw;
  188. XmlTextWriter xw;
  189. // empty
  190. xs = new XmlSchema ();
  191. sw = new StringWriter ();
  192. xw = new XmlTextWriter (sw);
  193. xs.Write (xw);
  194. doc.LoadXml (sw.ToString ());
  195. Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#1");
  196. // TargetNamespace
  197. xs = new XmlSchema ();
  198. sw = new StringWriter ();
  199. xw = new XmlTextWriter (sw);
  200. xs.TargetNamespace = "urn:foo";
  201. xs.Write (xw);
  202. doc.LoadXml (sw.ToString ());
  203. Assert.AreEqual ("<xs:schema xmlns:tns=\"urn:foo\" targetNamespace=\"urn:foo\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2");
  204. // Zero-length TargetNamespace
  205. xs = new XmlSchema ();
  206. sw = new StringWriter ();
  207. xw = new XmlTextWriter (sw);
  208. xs.TargetNamespace = string.Empty;
  209. xs.Write (xw);
  210. doc.LoadXml (sw.ToString ());
  211. Assert.AreEqual ("<xs:schema targetNamespace=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#2b");
  212. // XmlSerializerNamespaces
  213. xs = new XmlSchema ();
  214. sw = new StringWriter ();
  215. xw = new XmlTextWriter (sw);
  216. xs.Namespaces.Add ("hoge", "urn:hoge");
  217. xs.Write (xw);
  218. doc.LoadXml (sw.ToString ());
  219. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  220. // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#3");
  221. // TargetNamespace + XmlSerializerNamespaces
  222. xs = new XmlSchema ();
  223. sw = new StringWriter ();
  224. xw = new XmlTextWriter (sw);
  225. xs.TargetNamespace = "urn:foo";
  226. xs.Namespaces.Add ("hoge", "urn:hoge");
  227. xs.Write (xw);
  228. doc.LoadXml (sw.ToString ());
  229. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  230. // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" targetNamespace=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#4");
  231. // Add XmlSchema.Namespace to XmlSerializerNamespaces
  232. xs = new XmlSchema ();
  233. sw = new StringWriter ();
  234. xw = new XmlTextWriter (sw);
  235. xs.Namespaces.Add ("a", XmlSchema.Namespace);
  236. xs.Write (xw);
  237. doc.LoadXml (sw.ToString ());
  238. Assert.AreEqual ("<a:schema xmlns:a=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#5");
  239. // UnhandledAttributes + XmlSerializerNamespaces
  240. xs = new XmlSchema ();
  241. sw = new StringWriter ();
  242. xw = new XmlTextWriter (sw);
  243. XmlAttribute attr = doc.CreateAttribute ("hoge");
  244. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  245. xs.Namespaces.Add ("hoge", "urn:hoge");
  246. xs.Write (xw);
  247. doc.LoadXml (sw.ToString ());
  248. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  249. // Assert.AreEqual ("<schema xmlns:hoge=\"urn:hoge\" hoge=\"\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#6");
  250. // Adding xmlns to UnhandledAttributes -> no output
  251. xs = new XmlSchema ();
  252. sw = new StringWriter ();
  253. xw = new XmlTextWriter (sw);
  254. attr = doc.CreateAttribute ("xmlns");
  255. attr.Value = "urn:foo";
  256. xs.UnhandledAttributes = new XmlAttribute [] {attr};
  257. xs.Write (xw);
  258. doc.LoadXml (sw.ToString ());
  259. Assert.AreEqual ("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", doc.DocumentElement.OuterXml, "#7");
  260. }
  261. [Category ("NotWorking")]
  262. [Test]
  263. public void TestWriteNamespaces2 ()
  264. {
  265. string xmldecl = "<?xml version=\"1.0\" encoding=\"utf-16\"?>";
  266. XmlSchema xs = new XmlSchema ();
  267. XmlSerializerNamespaces nss =
  268. new XmlSerializerNamespaces ();
  269. StringWriter sw;
  270. sw = new StringWriter ();
  271. xs.Write (new XmlTextWriter (sw));
  272. Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#1");
  273. xs.Namespaces = nss;
  274. sw = new StringWriter ();
  275. xs.Write (new XmlTextWriter (sw));
  276. Assert.AreEqual (xmldecl + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#2");
  277. nss.Add ("foo", "urn:foo");
  278. sw = new StringWriter ();
  279. xs.Write (new XmlTextWriter (sw));
  280. // commenting out. .NET 2.0 outputs xs:schema instead of schema, that also makes sense.
  281. // Assert.AreEqual (xmldecl + "<schema xmlns:foo=\"urn:foo\" xmlns=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#3");
  282. nss.Add ("", "urn:foo");
  283. sw = new StringWriter ();
  284. xs.Write (new XmlTextWriter (sw));
  285. // commenting out. .NET 2.0 outputs xs:schema instead of q1:schema, that also makes sense.
  286. // Assert.AreEqual (xmldecl + "<q1:schema xmlns:foo=\"urn:foo\" xmlns=\"urn:foo\" xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString (), "#4");
  287. nss.Add ("q1", "urn:q1");
  288. sw = new StringWriter ();
  289. xs.Write (new XmlTextWriter (sw));
  290. //Not sure if testing for exact order of these name spaces is
  291. // relevent, so using less strict test that passes on MS.NET
  292. //Assert.AreEqual (xmldecl + "<q2:schema xmlns:foo=\"urn:foo\" xmlns:q1=\"urn:q1\" xmlns=\"urn:foo\" xmlns:q2=\"http://www.w3.org/2001/XMLSchema\" />", sw.ToString ());
  293. Assert.IsTrue (sw.ToString ().IndexOf ("xmlns:q1=\"urn:q1\"") != -1, "q1");
  294. }
  295. [Test]
  296. public void ReaderPositionAfterRead ()
  297. {
  298. string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'> <xs:element name='test' type='xs:integer'/></xs:schema>";
  299. XmlTextReader xtr = new XmlTextReader (xsd, XmlNodeType.Document, null);
  300. xtr.Read ();
  301. XmlSchema xs = XmlSchema.Read (xtr, null);
  302. Assert.AreEqual (XmlNodeType.EndElement, xtr.NodeType);
  303. }
  304. [Test]
  305. // bug #76865
  306. public void AmbiguityDetectionOnChameleonAnyOther ()
  307. {
  308. string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  309. <xs:complexType name='TestType'>
  310. <xs:sequence>
  311. <xs:any namespace='##other' minOccurs='0' />
  312. <xs:element name='Item' />
  313. <xs:any namespace='##other' minOccurs='0' />
  314. </xs:sequence>
  315. </xs:complexType>
  316. </xs:schema>";
  317. XmlSchema.Read (new XmlTextReader (xsd, XmlNodeType.Document, null), null);
  318. }
  319. [Test]
  320. // bug #77685
  321. public void ReadDoesNotIgnoreDocumentationEmptyElement ()
  322. {
  323. string schemaxml = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
  324. <xs:element name='choice'>
  325. <xs:annotation><xs:documentation /></xs:annotation>
  326. </xs:element>
  327. </xs:schema>";
  328. XmlTextReader tr = new XmlTextReader (
  329. schemaxml, XmlNodeType.Document, null);
  330. XmlSchema schema = XmlSchema.Read (tr, null);
  331. XmlSchemaElement element =
  332. schema.Items [0] as XmlSchemaElement;
  333. XmlSchemaAnnotation annotation = element.Annotation;
  334. XmlSchemaDocumentation doc =
  335. annotation.Items [0] as XmlSchemaDocumentation;
  336. Assert.AreEqual (0, doc.Markup.Length);
  337. }
  338. [Test]
  339. // bug #77687
  340. public void CompileFillsSchemaPropertyInExternal ()
  341. {
  342. string schemaFileName = "Test/XmlFiles/xsd/77687.xsd";
  343. XmlTextReader tr = new XmlTextReader (schemaFileName);
  344. XmlSchema schema = XmlSchema.Read (tr, null);
  345. XmlSchemaInclude inc = (XmlSchemaInclude) schema.Includes [0];
  346. Assert.IsNull (inc.Schema);
  347. schema.Compile (null);
  348. tr.Close ();
  349. Assert.IsNotNull (inc.Schema);
  350. }
  351. [Test]
  352. // bug #78985 (contains two identical field path "@key" in
  353. // two different keys where one is in scope within another)
  354. public void DuplicateKeyFieldAttributePath ()
  355. {
  356. string schemaFileName = "Test/XmlFiles/xsd/78985.xsd";
  357. string xmlFileName = "Test/XmlFiles/xsd/78985.xml";
  358. XmlTextReader tr = new XmlTextReader (schemaFileName);
  359. XmlValidatingReader vr = new XmlValidatingReader (
  360. new XmlTextReader (xmlFileName));
  361. vr.Schemas.Add (XmlSchema.Read (tr, null));
  362. while (!vr.EOF)
  363. vr.Read ();
  364. }
  365. [Test]
  366. public void ThreeLevelNestedInclusion ()
  367. {
  368. XmlTextReader r = new XmlTextReader ("Test/XmlFiles/xsd/361818.xsd");
  369. try {
  370. XmlSchema xs = XmlSchema.Read (r, null);
  371. xs.Compile (null);
  372. } finally {
  373. r.Close ();
  374. }
  375. }
  376. [Test] // bug #502115
  377. public void ExtensionRedefineAttribute1 ()
  378. {
  379. const string xml = "<Bar xmlns='foo'/>";
  380. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-1.xsd");
  381. #if NET_2_0
  382. XmlSchemaSet xss = new XmlSchemaSet ();
  383. xss.Add (schema);
  384. if (StrictMsCompliant) {
  385. xss.Compile ();
  386. } else {
  387. try {
  388. xss.Compile ();
  389. Assert.Fail ();
  390. } catch (XmlSchemaException) {
  391. }
  392. return;
  393. }
  394. StringReader sr = new StringReader (xml);
  395. XmlReaderSettings settings = new XmlReaderSettings ();
  396. settings.ValidationType = ValidationType.Schema;
  397. settings.Schemas = xss;
  398. XmlReader vr = XmlReader.Create (sr, settings);
  399. #else
  400. if (StrictMsCompliant) {
  401. schema.Compile (null);
  402. } else {
  403. try {
  404. schema.Compile (null);
  405. Assert.Fail ();
  406. } catch (XmlSchemaException) {
  407. }
  408. return;
  409. }
  410. XmlValidatingReader vr = new XmlValidatingReader (xml,
  411. XmlNodeType.Document, null);
  412. vr.Schemas.Add (schema);
  413. vr.ValidationType = ValidationType.Schema;
  414. #endif
  415. try {
  416. vr.Read ();
  417. Assert.Fail ();
  418. } catch (XmlSchemaException) {
  419. }
  420. }
  421. [Test] // bug #502115
  422. public void ExtensionRedefineAttribute2 ()
  423. {
  424. const string xml = "<Bar xmlns='foo'/>";
  425. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-2.xsd");
  426. #if NET_2_0
  427. XmlSchemaSet xss = new XmlSchemaSet ();
  428. xss.Add (schema);
  429. xss.Compile ();
  430. StringReader sr = new StringReader (xml);
  431. XmlReaderSettings settings = new XmlReaderSettings ();
  432. settings.ValidationType = ValidationType.Schema;
  433. settings.Schemas = xss;
  434. XmlReader vr = XmlReader.Create (sr, settings);
  435. #else
  436. schema.Compile (null);
  437. XmlValidatingReader vr = new XmlValidatingReader (xml,
  438. XmlNodeType.Document, null);
  439. vr.Schemas.Add (schema);
  440. vr.ValidationType = ValidationType.Schema;
  441. #endif
  442. while (vr.Read ()) ;
  443. }
  444. [Test] // bug #502115
  445. public void ExtensionRedefineAttribute3 ()
  446. {
  447. const string xml = "<Bar xmlns='foo'/>";
  448. XmlSchema schema = GetSchema ("Test/XmlFiles/xsd/extension-attr-redefine-3.xsd");
  449. #if NET_2_0
  450. XmlSchemaSet xss = new XmlSchemaSet ();
  451. xss.Add (schema);
  452. if (StrictMsCompliant) {
  453. xss.Compile ();
  454. } else {
  455. try {
  456. xss.Compile ();
  457. Assert.Fail ();
  458. } catch (XmlSchemaException) {
  459. }
  460. return;
  461. }
  462. StringReader sr = new StringReader ("<Bar xmlns='foo'/>");
  463. XmlReaderSettings settings = new XmlReaderSettings ();
  464. settings.ValidationType = ValidationType.Schema;
  465. settings.Schemas = xss;
  466. XmlReader vr = XmlReader.Create (sr, settings);
  467. #else
  468. if (StrictMsCompliant) {
  469. schema.Compile (null);
  470. } else {
  471. try {
  472. schema.Compile (null);
  473. Assert.Fail ();
  474. } catch (XmlSchemaException) {
  475. }
  476. return;
  477. }
  478. XmlValidatingReader vr = new XmlValidatingReader (xml,
  479. XmlNodeType.Document, null);
  480. vr.Schemas.Add (schema);
  481. vr.ValidationType = ValidationType.Schema;
  482. #endif
  483. while (vr.Read ()) ;
  484. }
  485. #if NET_2_0
  486. internal class XmlTestResolver : XmlResolver
  487. {
  488. Uri receivedUri;
  489. public override ICredentials Credentials
  490. {
  491. set { throw new NotSupportedException (); }
  492. }
  493. public override Uri ResolveUri (Uri baseUri, string relativeUri)
  494. {
  495. return new Uri (relativeUri);
  496. }
  497. public Uri ReceivedUri
  498. {
  499. get { return receivedUri; }
  500. }
  501. public override object GetEntity (Uri absoluteUri, string role, Type ofObjectToReturn)
  502. {
  503. receivedUri = absoluteUri;
  504. return null;
  505. }
  506. }
  507. [Test]
  508. public void TestResolveUri ()
  509. {
  510. XmlSchemaSet schemaSet = new XmlSchemaSet ();
  511. FileStream stream = new FileStream ("Test/XmlFiles/xsd/resolveUriSchema.xsd", FileMode.Open);
  512. schemaSet.Add ("http://tempuri.org/resolveUriSchema.xsd", new XmlTextReader (stream));
  513. XmlTestResolver resolver = new XmlTestResolver ();
  514. XmlReaderSettings settings = new XmlReaderSettings ();
  515. settings.Schemas.XmlResolver = resolver;
  516. settings.Schemas.Add (schemaSet);
  517. settings.ValidationType = ValidationType.Schema;
  518. settings.ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema | XmlSchemaValidationFlags.ProcessSchemaLocation;
  519. XmlReader reader = XmlReader.Create (stream, settings);
  520. try
  521. {
  522. reader.Read ();
  523. }
  524. catch (XmlException)
  525. {
  526. // do nothing - we are expecting this exception because the test xmlresolver returns null from its
  527. // GetEntity method.
  528. }
  529. Assert.AreEqual ("assembly://MyAssembly.Name/MyProjectNameSpace/objects.xsd", resolver.ReceivedUri.OriginalString);
  530. }
  531. [Test]
  532. public void TestImportNoSchemaLocation()
  533. {
  534. XmlSchemaSet schemaSet = new XmlSchemaSet ();
  535. schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importNamespaceTest.xsd"));
  536. schemaSet.Add (GetSchema ("Test/XmlFiles/xsd/importedNamespace.xsd"));
  537. XmlReaderSettings settings = new XmlReaderSettings ();
  538. settings.Schemas.Add (schemaSet);
  539. settings.ValidationType = ValidationType.Schema;
  540. XmlReader reader = XmlReader.Create ("Test/XmlFiles/xsd/xsdimporttest.xml", settings);
  541. // Parse the file.
  542. while (reader.Read()) {}
  543. }
  544. #endif
  545. }
  546. }