XmlSchemaExporterTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //
  2. // System.Xml.Serialization.XmlSchemaExporterTests
  3. //
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (C) 2005 Novell
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using System.Xml.Serialization;
  16. using NUnit.Framework;
  17. using MonoTests.System.Xml.TestClasses;
  18. namespace MonoTests.System.XmlSerialization
  19. {
  20. [TestFixture]
  21. public class XmlSchemaExporterTests
  22. {
  23. [Test]
  24. [Category ("NotWorking")] // on Mono, element is output before type
  25. public void ExportStruct ()
  26. {
  27. XmlReflectionImporter ri = new XmlReflectionImporter ("NSTimeSpan");
  28. XmlSchemas schemas = new XmlSchemas ();
  29. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  30. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (TimeSpan));
  31. sx.ExportTypeMapping (tm);
  32. Assert.AreEqual (1, schemas.Count, "#1");
  33. StringWriter sw = new StringWriter ();
  34. schemas[0].Write (sw);
  35. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  36. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  37. "<xs:schema xmlns:tns=\"NSTimeSpan\" elementFormDefault=\"qualified\" targetNamespace=\"NSTimeSpan\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  38. " <xs:element name=\"TimeSpan\" type=\"tns:TimeSpan\" />{0}" +
  39. " <xs:complexType name=\"TimeSpan\" />{0}" +
  40. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  41. }
  42. [Test]
  43. [Category ("NotWorking")] // on Mono, element is output before type
  44. public void ExportStruct_Array ()
  45. {
  46. XmlReflectionImporter ri = new XmlReflectionImporter ("NSTimeSpanArray");
  47. XmlSchemas schemas = new XmlSchemas ();
  48. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  49. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (TimeSpan[]));
  50. sx.ExportTypeMapping (tm);
  51. Assert.AreEqual (1, schemas.Count, "#1");
  52. StringWriter sw = new StringWriter ();
  53. schemas[0].Write (sw);
  54. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  55. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  56. "<xs:schema xmlns:tns=\"NSTimeSpanArray\" elementFormDefault=\"qualified\" targetNamespace=\"NSTimeSpanArray\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  57. " <xs:element name=\"ArrayOfTimeSpan\" nillable=\"true\" type=\"tns:ArrayOfTimeSpan\" />{0}" +
  58. " <xs:complexType name=\"ArrayOfTimeSpan\">{0}" +
  59. " <xs:sequence>{0}" +
  60. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"TimeSpan\" type=\"tns:TimeSpan\" />{0}" +
  61. " </xs:sequence>{0}" +
  62. " </xs:complexType>{0}" +
  63. " <xs:complexType name=\"TimeSpan\" />{0}" +
  64. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  65. }
  66. [Test]
  67. [Category ("NotWorking")] // on Mono, element is output before type
  68. public void ExportClass ()
  69. {
  70. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  71. XmlAttributes attr = new XmlAttributes ();
  72. XmlElementAttribute element = new XmlElementAttribute ();
  73. element.ElementName = "saying";
  74. element.IsNullable = true;
  75. attr.XmlElements.Add (element);
  76. overrides.Add (typeof (SimpleClass), "something", attr);
  77. XmlReflectionImporter ri = new XmlReflectionImporter (overrides, "NS1");
  78. XmlSchemas schemas = new XmlSchemas ();
  79. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  80. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (SimpleClass));
  81. sx.ExportTypeMapping (tm);
  82. Assert.AreEqual (1, schemas.Count, "#1");
  83. StringWriter sw = new StringWriter ();
  84. schemas[0].Write (sw);
  85. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  86. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  87. "<xs:schema xmlns:tns=\"NS1\" elementFormDefault=\"qualified\" targetNamespace=\"NS1\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  88. " <xs:element name=\"SimpleClass\" nillable=\"true\" type=\"tns:SimpleClass\" />{0}" +
  89. " <xs:complexType name=\"SimpleClass\">{0}" +
  90. " <xs:sequence>{0}" +
  91. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"saying\" nillable=\"true\" type=\"xs:string\" />{0}" +
  92. " </xs:sequence>{0}" +
  93. " </xs:complexType>{0}" +
  94. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  95. }
  96. [Test]
  97. [Category ("NotWorking")] // on Mono, element is output before type
  98. public void ExportClass_Array ()
  99. {
  100. XmlAttributeOverrides overrides = new XmlAttributeOverrides ();
  101. XmlAttributes attr = new XmlAttributes ();
  102. XmlElementAttribute element = new XmlElementAttribute ();
  103. element.ElementName = "saying";
  104. element.IsNullable = true;
  105. attr.XmlElements.Add (element);
  106. overrides.Add (typeof (SimpleClass), "something", attr);
  107. XmlReflectionImporter ri = new XmlReflectionImporter (overrides, "NSSimpleClassArray");
  108. XmlSchemas schemas = new XmlSchemas ();
  109. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  110. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (SimpleClass[]));
  111. sx.ExportTypeMapping (tm);
  112. Assert.AreEqual (1, schemas.Count, "#1");
  113. StringWriter sw = new StringWriter ();
  114. schemas[0].Write (sw);
  115. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  116. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  117. "<xs:schema xmlns:tns=\"NSSimpleClassArray\" elementFormDefault=\"qualified\" targetNamespace=\"NSSimpleClassArray\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  118. " <xs:element name=\"ArrayOfSimpleClass\" nillable=\"true\" type=\"tns:ArrayOfSimpleClass\" />{0}" +
  119. " <xs:complexType name=\"ArrayOfSimpleClass\">{0}" +
  120. " <xs:sequence>{0}" +
  121. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"SimpleClass\" nillable=\"true\" type=\"tns:SimpleClass\" />{0}" +
  122. " </xs:sequence>{0}" +
  123. " </xs:complexType>{0}" +
  124. " <xs:complexType name=\"SimpleClass\">{0}" +
  125. " <xs:sequence>{0}" +
  126. " <xs:element minOccurs=\"1\" maxOccurs=\"1\" name=\"saying\" nillable=\"true\" type=\"xs:string\" />{0}" +
  127. " </xs:sequence>{0}" +
  128. " </xs:complexType>{0}" +
  129. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  130. }
  131. [Test]
  132. [Category ("NotWorking")] // on Mono, element is output before type
  133. public void ExportEnum ()
  134. {
  135. XmlReflectionImporter ri = new XmlReflectionImporter ("NS2");
  136. XmlSchemas schemas = new XmlSchemas ();
  137. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  138. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (EnumDefaultValue));
  139. sx.ExportTypeMapping (tm);
  140. Assert.AreEqual (1, schemas.Count, "#1");
  141. StringWriter sw = new StringWriter ();
  142. schemas[0].Write (sw);
  143. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  144. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  145. "<xs:schema xmlns:tns=\"NS2\" elementFormDefault=\"qualified\" targetNamespace=\"NS2\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  146. " <xs:element name=\"EnumDefaultValue\" type=\"tns:EnumDefaultValue\" />{0}" +
  147. " <xs:simpleType name=\"EnumDefaultValue\">{0}" +
  148. " <xs:list>{0}" +
  149. " <xs:simpleType>{0}" +
  150. " <xs:restriction base=\"xs:string\">{0}" +
  151. " <xs:enumeration value=\"e1\" />{0}" +
  152. " <xs:enumeration value=\"e2\" />{0}" +
  153. " <xs:enumeration value=\"e3\" />{0}" +
  154. " </xs:restriction>{0}" +
  155. " </xs:simpleType>{0}" +
  156. " </xs:list>{0}" +
  157. " </xs:simpleType>{0}" +
  158. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  159. }
  160. [Test]
  161. [Category ("NotWorking")] // on Mono, element is output before type
  162. public void ExportXmlSerializable ()
  163. {
  164. XmlReflectionImporter ri = new XmlReflectionImporter ("NS3");
  165. XmlSchemas schemas = new XmlSchemas ();
  166. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  167. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (Employee));
  168. sx.ExportTypeMapping (tm);
  169. Assert.AreEqual (1, schemas.Count, "#1");
  170. StringWriter sw = new StringWriter ();
  171. schemas[0].Write (sw);
  172. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  173. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  174. "<xs:schema xmlns:tns=\"NS3\" elementFormDefault=\"qualified\" targetNamespace=\"NS3\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  175. " <xs:import namespace=\"http://www.w3.org/2001/XMLSchema\" />{0}" +
  176. " <xs:element name=\"Employee\" nillable=\"true\">{0}" +
  177. " <xs:complexType>{0}" +
  178. " <xs:sequence>{0}" +
  179. " <xs:element ref=\"xs:schema\" />{0}" +
  180. " <xs:any />{0}" +
  181. " </xs:sequence>{0}" +
  182. " </xs:complexType>{0}" +
  183. " </xs:element>{0}" +
  184. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  185. ri = new XmlReflectionImporter ("NS4");
  186. schemas = new XmlSchemas ();
  187. sx = new XmlSchemaExporter (schemas);
  188. tm = ri.ImportTypeMapping (typeof (EmployeeSchema));
  189. sx.ExportTypeMapping (tm);
  190. Assert.AreEqual (1, schemas.Count, "#3");
  191. sw = new StringWriter ();
  192. schemas[0].Write (sw);
  193. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  194. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  195. "<xs:schema xmlns:tns=\"NS4\" elementFormDefault=\"qualified\" targetNamespace=\"NS4\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  196. #if NET_2_0
  197. " <xs:import namespace=\"urn:types-devx-com\" />{0}" +
  198. " <xs:element name=\"employeeRoot\" nillable=\"true\" xmlns:q1=\"urn:types-devx-com\" type=\"q1:employeeRoot\" />{0}" +
  199. #else
  200. " <xs:import namespace=\"http://www.w3.org/2001/XMLSchema\" />{0}" +
  201. " <xs:element name=\"EmployeeSchema\" nillable=\"true\">{0}" +
  202. " <xs:complexType>{0}" +
  203. " <xs:sequence>{0}" +
  204. " <xs:element ref=\"xs:schema\" />{0}" +
  205. " <xs:any />{0}" +
  206. " </xs:sequence>{0}" +
  207. " </xs:complexType>{0}" +
  208. " </xs:element>{0}" +
  209. #endif
  210. "</xs:schema>", Environment.NewLine), sw.ToString (), "#4");
  211. }
  212. [Test]
  213. [Category ("NotWorking")] // bug #77117
  214. public void ExportXsdPrimitive ()
  215. {
  216. ArrayList types = new ArrayList ();
  217. types.Add (new TypeDescription (typeof (byte), true, "unsignedByte", "Byte"));
  218. types.Add (new TypeDescription (typeof (sbyte), true, "byte", "Byte"));
  219. types.Add (new TypeDescription (typeof (bool), true, "boolean", "Boolean"));
  220. types.Add (new TypeDescription (typeof (short), true, "short", "Short"));
  221. types.Add (new TypeDescription (typeof (int), true, "int", "Int"));
  222. types.Add (new TypeDescription (typeof (long), true, "long", "Long"));
  223. types.Add (new TypeDescription (typeof (float), true, "float", "Float"));
  224. types.Add (new TypeDescription (typeof (double), true, "double", "Double"));
  225. types.Add (new TypeDescription (typeof (decimal), true, "decimal", "Decimal"));
  226. types.Add (new TypeDescription (typeof (ushort), true, "unsignedShort", "UnsignedShort"));
  227. types.Add (new TypeDescription (typeof (uint), true, "unsignedInt", "UnsignedInt"));
  228. types.Add (new TypeDescription (typeof (ulong), true, "unsignedLong", "UnsignedLong"));
  229. types.Add (new TypeDescription (typeof (DateTime), true, "dateTime", "DateTime"));
  230. #if NET_2_0
  231. types.Add (new TypeDescription (typeof (XmlQualifiedName), true, "QName", "QName", true));
  232. #else
  233. types.Add (new TypeDescription (typeof (XmlQualifiedName), true, "QName", "QName"));
  234. #endif
  235. types.Add (new TypeDescription (typeof (string), true, "string", "String", true));
  236. foreach (TypeDescription typeDesc in types) {
  237. XmlReflectionImporter ri = new XmlReflectionImporter (typeDesc.Type.Name);
  238. XmlSchemas schemas = new XmlSchemas ();
  239. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  240. XmlTypeMapping tm = ri.ImportTypeMapping (typeDesc.Type);
  241. sx.ExportTypeMapping (tm);
  242. Assert.AreEqual (1, schemas.Count, typeDesc.Type.FullName + "#1");
  243. StringWriter sw = new StringWriter ();
  244. schemas[0].Write (sw);
  245. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  246. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  247. "<xs:schema xmlns:tns=\"{1}\" elementFormDefault=\"qualified\" targetNamespace=\"{1}\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  248. " <xs:element name=\"{2}\" {3}type=\"xs:{2}\" />{0}" +
  249. "</xs:schema>", Environment.NewLine, typeDesc.Type.Name, typeDesc.XmlType, typeDesc.IsNillable ? "nillable=\"true\" " : ""),
  250. sw.ToString (), typeDesc.Type.FullName + "#2");
  251. }
  252. }
  253. [Test]
  254. [Category ("NotWorking")] // bug #77117
  255. public void ExportXsdPrimitive_ByteArray ()
  256. {
  257. XmlReflectionImporter ri = new XmlReflectionImporter ("ByteArray");
  258. XmlSchemas schemas = new XmlSchemas ();
  259. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  260. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (byte[]));
  261. sx.ExportTypeMapping (tm);
  262. Assert.AreEqual (1, schemas.Count, "#1");
  263. StringWriter sw = new StringWriter ();
  264. schemas[0].Write (sw);
  265. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  266. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  267. "<xs:schema xmlns:tns=\"ByteArray\" elementFormDefault=\"qualified\" targetNamespace=\"ByteArray\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  268. " <xs:element name=\"base64Binary\" nillable=\"true\" type=\"xs:base64Binary\" />{0}" +
  269. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  270. }
  271. [Test]
  272. [Category ("NotWorking")] // bug #77117
  273. public void ExportXsdPrimitive_Arrays ()
  274. {
  275. ArrayList types = new ArrayList ();
  276. types.Add (new TypeDescription (typeof (sbyte[]), true, "byte", "Byte"));
  277. types.Add (new TypeDescription (typeof (bool[]), true, "boolean", "Boolean"));
  278. types.Add (new TypeDescription (typeof (short[]), true, "short", "Short"));
  279. types.Add (new TypeDescription (typeof (int[]), true, "int", "Int"));
  280. types.Add (new TypeDescription (typeof (long[]), true, "long", "Long"));
  281. types.Add (new TypeDescription (typeof (float[]), true, "float", "Float"));
  282. types.Add (new TypeDescription (typeof (double[]), true, "double", "Double"));
  283. types.Add (new TypeDescription (typeof (decimal[]), true, "decimal", "Decimal"));
  284. types.Add (new TypeDescription (typeof (ushort[]), true, "unsignedShort", "UnsignedShort"));
  285. types.Add (new TypeDescription (typeof (uint[]), true, "unsignedInt", "UnsignedInt"));
  286. types.Add (new TypeDescription (typeof (ulong[]), true, "unsignedLong", "UnsignedLong"));
  287. types.Add (new TypeDescription (typeof (DateTime[]), true, "dateTime", "DateTime"));
  288. #if NET_2_0
  289. types.Add (new TypeDescription (typeof (XmlQualifiedName[]), true, "QName", "QName", true));
  290. #else
  291. types.Add (new TypeDescription (typeof (XmlQualifiedName[]), true, "QName", "QName"));
  292. #endif
  293. types.Add (new TypeDescription (typeof (string[]), true, "string", "String", true));
  294. foreach (TypeDescription typeDesc in types) {
  295. XmlReflectionImporter ri = new XmlReflectionImporter (typeDesc.Type.Name);
  296. XmlSchemas schemas = new XmlSchemas ();
  297. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  298. XmlTypeMapping tm = ri.ImportTypeMapping (typeDesc.Type);
  299. sx.ExportTypeMapping (tm);
  300. Assert.AreEqual (1, schemas.Count, typeDesc.Type.FullName + "#1");
  301. StringWriter sw = new StringWriter ();
  302. schemas[0].Write (sw);
  303. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  304. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  305. "<xs:schema xmlns:tns=\"{1}\" elementFormDefault=\"qualified\" targetNamespace=\"{1}\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  306. " <xs:element name=\"ArrayOf{2}\" nillable=\"true\" type=\"tns:ArrayOf{2}\" />{0}" +
  307. " <xs:complexType name=\"ArrayOf{2}\">{0}" +
  308. " <xs:sequence>{0}" +
  309. " <xs:element minOccurs=\"0\" maxOccurs=\"unbounded\" name=\"{3}\" {5}type=\"{4}:{3}\" />{0}" +
  310. " </xs:sequence>{0}" +
  311. " </xs:complexType>{0}" +
  312. "</xs:schema>", Environment.NewLine, typeDesc.Type.Name, typeDesc.ArrayType, typeDesc.XmlType,
  313. typeDesc.XsdType ? "xs" : "tns", typeDesc.IsNillable ? "nillable=\"true\" " : ""),
  314. sw.ToString (), typeDesc.Type.FullName + "#2");
  315. }
  316. }
  317. [Test]
  318. [Category ("NotWorking")] // bug #77117
  319. public void ExportNonXsdPrimitive_Guid ()
  320. {
  321. XmlReflectionImporter ri = new XmlReflectionImporter ("NSPrimGuid");
  322. XmlSchemas schemas = new XmlSchemas ();
  323. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  324. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (Guid));
  325. sx.ExportTypeMapping (tm);
  326. Assert.AreEqual (2, schemas.Count, "#1");
  327. StringWriter sw = new StringWriter ();
  328. schemas[0].Write (sw);
  329. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  330. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  331. "<xs:schema xmlns:tns=\"NSPrimGuid\" elementFormDefault=\"qualified\" targetNamespace=\"NSPrimGuid\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  332. " <xs:import namespace=\"http://microsoft.com/wsdl/types/\" />{0}" +
  333. " <xs:element name=\"guid\" xmlns:q1=\"http://microsoft.com/wsdl/types/\" type=\"q1:guid\" />{0}" +
  334. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  335. sw = new StringWriter ();
  336. schemas[1].Write (sw);
  337. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  338. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  339. "<xs:schema xmlns:tns=\"http://microsoft.com/wsdl/types/\" elementFormDefault=\"qualified\" targetNamespace=\"http://microsoft.com/wsdl/types/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  340. " <xs:simpleType name=\"guid\">{0}" +
  341. " <xs:restriction base=\"xs:string\">{0}" +
  342. " <xs:pattern value=\"[0-9a-fA-F]{{8}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{4}}-[0-9a-fA-F]{{12}}\" />{0}" +
  343. " </xs:restriction>{0}" +
  344. " </xs:simpleType>{0}" +
  345. "</xs:schema>", Environment.NewLine), sw.ToString (), "#3");
  346. }
  347. [Test]
  348. [Category ("NotWorking")] // bug #77117
  349. public void ExportNonXsdPrimitive_Char ()
  350. {
  351. XmlReflectionImporter ri = new XmlReflectionImporter ("NSPrimChar");
  352. XmlSchemas schemas = new XmlSchemas ();
  353. XmlSchemaExporter sx = new XmlSchemaExporter (schemas);
  354. XmlTypeMapping tm = ri.ImportTypeMapping (typeof (Char));
  355. sx.ExportTypeMapping (tm);
  356. Assert.AreEqual (2, schemas.Count, "#1");
  357. StringWriter sw = new StringWriter ();
  358. schemas[0].Write (sw);
  359. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  360. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  361. "<xs:schema xmlns:tns=\"NSPrimChar\" elementFormDefault=\"qualified\" targetNamespace=\"NSPrimChar\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  362. " <xs:import namespace=\"http://microsoft.com/wsdl/types/\" />{0}" +
  363. " <xs:element name=\"char\" xmlns:q1=\"http://microsoft.com/wsdl/types/\" type=\"q1:char\" />{0}" +
  364. "</xs:schema>", Environment.NewLine), sw.ToString (), "#2");
  365. sw = new StringWriter ();
  366. schemas[1].Write (sw);
  367. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  368. "<?xml version=\"1.0\" encoding=\"utf-16\"?>{0}" +
  369. "<xs:schema xmlns:tns=\"http://microsoft.com/wsdl/types/\" elementFormDefault=\"qualified\" targetNamespace=\"http://microsoft.com/wsdl/types/\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">{0}" +
  370. " <xs:simpleType name=\"char\">{0}" +
  371. " <xs:restriction base=\"xs:unsignedShort\" />{0}" +
  372. " </xs:simpleType>{0}" +
  373. "</xs:schema>", Environment.NewLine), sw.ToString (), "#3");
  374. }
  375. public class Employee : IXmlSerializable
  376. {
  377. private string _firstName;
  378. private string _lastName;
  379. private string _address;
  380. public XmlSchema GetSchema ()
  381. {
  382. return null;
  383. }
  384. public void WriteXml (XmlWriter writer)
  385. {
  386. writer.WriteStartElement ("employee", "urn:devx-com");
  387. writer.WriteAttributeString ("firstName", _firstName);
  388. writer.WriteAttributeString ("lastName", _lastName);
  389. writer.WriteAttributeString ("address", _address);
  390. writer.WriteEndElement ();
  391. }
  392. public void ReadXml (XmlReader reader)
  393. {
  394. XmlNodeType type = reader.MoveToContent ();
  395. if (type == XmlNodeType.Element && reader.LocalName == "employee") {
  396. _firstName = reader["firstName"];
  397. _lastName = reader["lastName"];
  398. _address = reader["address"];
  399. }
  400. }
  401. }
  402. #if NET_2_0
  403. [XmlSchemaProvider ("CreateEmployeeSchema")]
  404. #endif
  405. public class EmployeeSchema : Employee
  406. {
  407. #if NET_2_0
  408. public static XmlQualifiedName CreateEmployeeSchema (XmlSchemaSet schemaSet)
  409. {
  410. XmlSchema schema = new XmlSchema();
  411. schema.Id = "EmployeeSchema";
  412. schema.TargetNamespace = "urn:types-devx-com";
  413. XmlSchemaComplexType type = new XmlSchemaComplexType();
  414. type.Name = "employeeRoot";
  415. XmlSchemaAttribute firstNameAttr = new XmlSchemaAttribute();
  416. firstNameAttr.Name = "firstName";
  417. type.Attributes.Add(firstNameAttr);
  418. XmlSchemaAttribute lastNameAttr = new XmlSchemaAttribute();
  419. lastNameAttr.Name = "lastName";
  420. type.Attributes.Add(lastNameAttr);
  421. XmlSchemaAttribute addressAttr = new XmlSchemaAttribute();
  422. addressAttr.Name = "address";
  423. type.Attributes.Add(addressAttr);
  424. XmlSchemaElement employeeElement = new XmlSchemaElement();
  425. employeeElement.Name = "employee";
  426. XmlQualifiedName name = new XmlQualifiedName("employeeRoot", "urn:types-devx-com");
  427. employeeElement.SchemaTypeName = name;
  428. schema.Items.Add(type);
  429. schema.Items.Add(employeeElement);
  430. schemaSet.Add(schema);
  431. return name;
  432. }
  433. #endif
  434. }
  435. private class TypeDescription
  436. {
  437. public TypeDescription (Type type, bool xsdType, string xmlType, string arrayType) : this (type, xsdType, xmlType, arrayType, false)
  438. {
  439. }
  440. public TypeDescription (Type type, bool xsdType, string xmlType, string arrayType, bool isNillable)
  441. {
  442. _type = type;
  443. _xsdType = xsdType;
  444. _xmlType = xmlType;
  445. _arrayType = arrayType;
  446. _isNillable = isNillable;
  447. }
  448. public Type Type {
  449. get { return _type; }
  450. }
  451. public string XmlType {
  452. get { return _xmlType; }
  453. }
  454. public string ArrayType {
  455. get { return _arrayType; }
  456. }
  457. public bool XsdType {
  458. get { return _xsdType; }
  459. }
  460. public bool IsNillable {
  461. get { return _isNillable; }
  462. }
  463. private Type _type;
  464. private bool _xsdType;
  465. private string _xmlType;
  466. private string _arrayType;
  467. private bool _isNillable;
  468. }
  469. }
  470. }