XmlSchema.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. //
  2. // System.Xml.Schema.XmlSchema.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Atsushi Enomoto [email protected]
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Xml;
  11. using System.IO;
  12. using System.Xml.Serialization;
  13. using System.ComponentModel;
  14. namespace System.Xml.Schema
  15. {
  16. /// <summary>
  17. /// Summary description for XmlSchema.
  18. /// </summary>
  19. [XmlRoot("schema",Namespace=XmlSchema.Namespace)]
  20. public class XmlSchema : XmlSchemaObject
  21. {
  22. //public constants
  23. public const string Namespace = "http://www.w3.org/2001/XMLSchema";
  24. public const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
  25. //private fields
  26. private XmlSchemaForm attributeFormDefault ;
  27. private XmlSchemaObjectTable attributeGroups ;
  28. private XmlSchemaObjectTable attributes ;
  29. private XmlSchemaDerivationMethod blockDefault ;
  30. private XmlSchemaForm elementFormDefault ;
  31. private XmlSchemaObjectTable elements ;
  32. private XmlSchemaDerivationMethod finalDefault ;
  33. private XmlSchemaObjectTable groups ;
  34. private string id ;
  35. private XmlSchemaObjectCollection includes ;
  36. private XmlSchemaObjectCollection items ;
  37. private XmlSchemaObjectTable notations ;
  38. private XmlSchemaObjectTable schemaTypes ;
  39. private string targetNamespace ;
  40. private XmlAttribute[] unhandledAttributes ;
  41. private string version;
  42. private string language;
  43. // post schema compilation infoset
  44. private Hashtable idCollection;
  45. private Hashtable missingBaseSchemaTypeRefs;
  46. private Hashtable missingElementTypeRefs;
  47. // Compiler specific things
  48. private static string xmlname = "schema";
  49. public XmlSchema()
  50. {
  51. attributeFormDefault= XmlSchemaForm.None;
  52. blockDefault = XmlSchemaDerivationMethod.None;
  53. elementFormDefault = XmlSchemaForm.None;
  54. finalDefault = XmlSchemaDerivationMethod.None;
  55. includes = new XmlSchemaObjectCollection();
  56. isCompiled = false;
  57. items = new XmlSchemaObjectCollection();
  58. attributeGroups = new XmlSchemaObjectTable();
  59. attributes = new XmlSchemaObjectTable();
  60. elements = new XmlSchemaObjectTable();
  61. groups = new XmlSchemaObjectTable();
  62. notations = new XmlSchemaObjectTable();
  63. schemaTypes = new XmlSchemaObjectTable();
  64. idCollection = new Hashtable ();
  65. missingBaseSchemaTypeRefs = new Hashtable ();
  66. missingElementTypeRefs = new Hashtable ();
  67. }
  68. #region Properties
  69. [DefaultValue(XmlSchemaForm.None)]
  70. [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
  71. public XmlSchemaForm AttributeFormDefault
  72. {
  73. get{ return attributeFormDefault; }
  74. set{ this.attributeFormDefault = value;}
  75. }
  76. [DefaultValue(XmlSchemaDerivationMethod.None)]
  77. [System.Xml.Serialization.XmlAttribute("blockDefault")]
  78. public XmlSchemaDerivationMethod BlockDefault
  79. {
  80. get{ return blockDefault;}
  81. set{ blockDefault = value;}
  82. }
  83. [DefaultValue(XmlSchemaDerivationMethod.None)]
  84. [System.Xml.Serialization.XmlAttribute("finalDefault")]
  85. public XmlSchemaDerivationMethod FinalDefault
  86. {
  87. get{ return finalDefault;}
  88. set{ finalDefault = value;}
  89. }
  90. [DefaultValue(XmlSchemaForm.None)]
  91. [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
  92. public XmlSchemaForm ElementFormDefault
  93. {
  94. get{ return elementFormDefault;}
  95. set{ elementFormDefault = value;}
  96. }
  97. [System.Xml.Serialization.XmlAttribute("targetNamespace")]
  98. public string TargetNamespace
  99. {
  100. get{ return targetNamespace;}
  101. set{ targetNamespace = value;}
  102. }
  103. [System.Xml.Serialization.XmlAttribute("version")]
  104. public string Version
  105. {
  106. get{ return version;}
  107. set{ version = value;}
  108. }
  109. [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
  110. [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
  111. [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
  112. public XmlSchemaObjectCollection Includes
  113. {
  114. get{ return includes;}
  115. }
  116. [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
  117. [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
  118. [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
  119. //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
  120. //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
  121. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
  122. [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
  123. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
  124. [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]
  125. [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]
  126. public XmlSchemaObjectCollection Items
  127. {
  128. get{ return items;}
  129. }
  130. [XmlIgnore]
  131. public bool IsCompiled
  132. {
  133. get{ return isCompiled;}
  134. }
  135. [XmlIgnore]
  136. public XmlSchemaObjectTable Attributes
  137. {
  138. get{ return attributes;}
  139. }
  140. [XmlIgnore]
  141. public XmlSchemaObjectTable AttributeGroups
  142. {
  143. get{ return attributeGroups; }
  144. }
  145. [XmlIgnore]
  146. public XmlSchemaObjectTable SchemaTypes
  147. {
  148. get{ return schemaTypes; }
  149. }
  150. [XmlIgnore]
  151. public XmlSchemaObjectTable Elements
  152. {
  153. get{ return elements;}
  154. }
  155. [System.Xml.Serialization.XmlAttribute("id")]
  156. public string Id
  157. {
  158. get{ return id;}
  159. set{ id = value;}
  160. }
  161. [XmlAnyAttribute]
  162. public XmlAttribute[] UnhandledAttributes
  163. {
  164. get
  165. {
  166. if(unhandledAttributeList != null)
  167. {
  168. unhandledAttributes = (XmlAttribute[]) unhandledAttributeList.ToArray(typeof(XmlAttribute));
  169. unhandledAttributeList = null;
  170. }
  171. return unhandledAttributes;
  172. }
  173. set
  174. {
  175. unhandledAttributes = value;
  176. unhandledAttributeList = null;
  177. }
  178. }
  179. [XmlIgnore]
  180. public XmlSchemaObjectTable Groups
  181. {
  182. get{ return groups;}
  183. }
  184. [XmlIgnore]
  185. public XmlSchemaObjectTable Notations
  186. {
  187. get{ return notations;}
  188. }
  189. // New attribute defined in W3C schema element
  190. [System.Xml.Serialization.XmlAttribute("xml:lang")]
  191. public string Language
  192. {
  193. get{ return language; }
  194. set{ language = value; }
  195. }
  196. internal Hashtable IDCollection
  197. {
  198. get { return idCollection; }
  199. }
  200. internal Hashtable MissingBaseSchemaTypeRefs
  201. {
  202. get { return missingBaseSchemaTypeRefs; }
  203. }
  204. internal Hashtable MissingElementTypeRefs
  205. {
  206. get { return missingElementTypeRefs; }
  207. }
  208. #endregion
  209. #region Compile
  210. // Methods
  211. /// <summary>
  212. /// This compile method does two things:
  213. /// 1. It compiles and fills the PSVI dataset
  214. /// 2. Validates the schema by calling Validate method.
  215. /// Every XmlSchemaObject has a Compile Method which gets called.
  216. /// </summary>
  217. /// <remarks>
  218. /// 1. blockDefault must be one of #all | List of (extension | restriction | substitution)
  219. /// 2. finalDefault must be one of (#all | List of (extension | restriction| union| list))
  220. /// 3. id must be of type ID
  221. /// 4. targetNamespace should be any uri
  222. /// 5. version should be a normalizedString
  223. /// 6. xml:lang should be a language
  224. /// </remarks>
  225. [MonoTODO]
  226. public void Compile(ValidationEventHandler handler)
  227. {
  228. CompilationId = Guid.NewGuid ();
  229. //1. Union and List are not allowed in block default
  230. if(BlockDefault != XmlSchemaDerivationMethod.All)
  231. {
  232. if((BlockDefault & XmlSchemaDerivationMethod.List)!=0 )
  233. error(handler, "list is not allowed in blockDefault attribute");
  234. if((BlockDefault & XmlSchemaDerivationMethod.Union)!=0 )
  235. error(handler, "union is not allowed in blockDefault attribute");
  236. }
  237. //2. Substitution is not allowed in finaldefault.
  238. if(FinalDefault != XmlSchemaDerivationMethod.All)
  239. {
  240. if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
  241. error(handler, "substitution is not allowed in finalDefault attribute");
  242. }
  243. //3. id must be of type ID
  244. XmlSchemaUtil.CompileID(Id, this, this.IDCollection, handler);
  245. //4. targetNamespace should be of type anyURI or absent
  246. if(TargetNamespace != null)
  247. {
  248. if(!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
  249. error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
  250. }
  251. //5. version should be of type normalizedString
  252. if(!XmlSchemaUtil.CheckNormalizedString(Version))
  253. error(handler, Version + "is not a valid value for version attribute of schema");
  254. //6. xml:lang must be a language
  255. if(!XmlSchemaUtil.CheckLanguage(Language))
  256. error(handler, Language + " is not a valid language");
  257. // Compile the content of this schema
  258. foreach(XmlSchemaObject obj in Includes)
  259. {
  260. if(obj is XmlSchemaExternal)
  261. {
  262. //FIXME: Kuch to karo! (Do Something ;)
  263. }
  264. else
  265. {
  266. error(handler,"Object of Type "+obj.GetType().Name+" is not valid in Includes Property of XmlSchema");
  267. }
  268. }
  269. // It is hack, but types are required before element/attributes.
  270. foreach (XmlSchemaObject obj in Items)
  271. {
  272. if(obj is XmlSchemaComplexType)
  273. {
  274. XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
  275. ctype.istoplevel = true;
  276. int numerr = ctype.Compile(handler, this);
  277. errorCount += numerr;
  278. if(numerr == 0)
  279. {
  280. schemaTypes.Add(ctype.QualifiedName, ctype);
  281. }
  282. }
  283. else if(obj is XmlSchemaSimpleType)
  284. {
  285. XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
  286. stype.islocal = false; //This simple type is toplevel
  287. int numerr = stype.Compile(handler, this);
  288. errorCount += numerr;
  289. if(numerr == 0)
  290. {
  291. SchemaTypes.Add(stype.QualifiedName, stype);
  292. }
  293. }
  294. }
  295. // Add missing references.
  296. foreach (XmlSchemaComplexType cType in missingBaseSchemaTypeRefs.Keys)
  297. cType.BaseSchemaTypeInternal =
  298. SchemaTypes [missingBaseSchemaTypeRefs [cType] as XmlQualifiedName];
  299. foreach(XmlSchemaObject obj in Items)
  300. {
  301. if(obj is XmlSchemaAnnotation)
  302. {
  303. int numerr = ((XmlSchemaAnnotation)obj).Compile(handler, this);
  304. errorCount += numerr;
  305. if( numerr == 0)
  306. {
  307. //FIXME: What PSVI set do we add this to?
  308. }
  309. }
  310. else if(obj is XmlSchemaAttribute)
  311. {
  312. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  313. attr.parentIsSchema = true;
  314. int numerr = attr.Compile(handler, this);
  315. errorCount += numerr;
  316. if(numerr == 0)
  317. {
  318. Attributes.Add(attr.QualifiedName, attr);
  319. }
  320. }
  321. else if(obj is XmlSchemaAttributeGroup)
  322. {
  323. XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
  324. int numerr = attrgrp.Compile(handler, this);
  325. errorCount += numerr;
  326. if(numerr == 0)
  327. {
  328. AttributeGroups.Add(attrgrp.QualifiedName, attrgrp);
  329. }
  330. }
  331. else if(obj is XmlSchemaComplexType)
  332. {
  333. // Do nothing here.
  334. }
  335. else if(obj is XmlSchemaSimpleType)
  336. {
  337. // Do nothing here.
  338. }
  339. else if(obj is XmlSchemaElement)
  340. {
  341. XmlSchemaElement elem = (XmlSchemaElement) obj;
  342. elem.parentIsSchema = true;
  343. int numerr = elem.Compile(handler, this);
  344. errorCount += numerr;
  345. if(numerr == 0)
  346. {
  347. Elements.Add(elem.QualifiedName,elem);
  348. }
  349. }
  350. else if(obj is XmlSchemaGroup)
  351. {
  352. XmlSchemaGroup grp = (XmlSchemaGroup) obj;
  353. int numerr = grp.Compile(handler, this);
  354. errorCount += numerr;
  355. if(numerr == 0)
  356. {
  357. Groups.Add(grp.QualifiedName,grp);
  358. }
  359. }
  360. else if(obj is XmlSchemaNotation)
  361. {
  362. XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
  363. int numerr = ntn.Compile(handler, this);
  364. errorCount += numerr;
  365. if(numerr == 0)
  366. {
  367. Notations.Add(ntn.QualifiedName, ntn);
  368. }
  369. }
  370. else
  371. {
  372. ValidationHandler.RaiseValidationError(handler,this,
  373. "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
  374. }
  375. }
  376. foreach (XmlSchemaElement element in missingElementTypeRefs.Keys)
  377. {
  378. element.SetReferedElementInfo (
  379. FindElement (missingElementTypeRefs [element] as XmlQualifiedName));
  380. }
  381. Validate(handler);
  382. }
  383. #endregion
  384. [MonoTODO]
  385. private void Validate(ValidationEventHandler handler)
  386. {
  387. foreach(XmlSchemaAttribute attr in Attributes.Values)
  388. {
  389. attr.Validate(handler, this);
  390. }
  391. foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
  392. {
  393. attrgrp.Validate(handler);
  394. }
  395. foreach(XmlSchemaType type in SchemaTypes.Values)
  396. {
  397. if(type is XmlSchemaComplexType)
  398. {
  399. ((XmlSchemaComplexType)type).Validate(handler);
  400. }
  401. else
  402. ((XmlSchemaSimpleType)type).Validate(handler, this);
  403. }
  404. foreach(XmlSchemaElement elem in Elements.Values)
  405. {
  406. elem.Validate(handler);
  407. }
  408. foreach(XmlSchemaGroup grp in Groups.Values)
  409. {
  410. grp.Validate(handler);
  411. }
  412. foreach(XmlSchemaNotation ntn in Notations.Values)
  413. {
  414. ntn.Validate(handler);
  415. }
  416. }
  417. internal XmlSchemaElement FindElement (XmlQualifiedName name)
  418. {
  419. if (name.Namespace != TargetNamespace)
  420. return null;
  421. foreach (XmlSchemaObject obj in Items) {
  422. XmlSchemaElement elem = obj as XmlSchemaElement;
  423. if (obj != null && elem.Name == name.Name)
  424. return elem;
  425. }
  426. return null;
  427. }
  428. #region Read
  429. public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
  430. {
  431. return Read(new XmlTextReader(reader),validationEventHandler);
  432. }
  433. public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
  434. {
  435. return Read(new XmlTextReader(stream),validationEventHandler);
  436. }
  437. [MonoTODO ("Use ValidationEventHandler")]
  438. public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)
  439. {
  440. XmlSerializer xser = new XmlSerializer (typeof (XmlSchema));
  441. return (XmlSchema) xser.Deserialize (rdr);
  442. /*
  443. XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);
  444. while(reader.ReadNextElement())
  445. {
  446. switch(reader.NodeType)
  447. {
  448. case XmlNodeType.Element:
  449. if(reader.LocalName == "schema")
  450. {
  451. XmlSchema schema = new XmlSchema();
  452. schema.LineNumber = reader.LineNumber;
  453. schema.LinePosition = reader.LinePosition;
  454. schema.SourceUri = reader.BaseURI;
  455. ReadAttributes(schema, reader, validationEventHandler);
  456. //IsEmptyElement does not behave properly if reader is
  457. //positioned at an attribute.
  458. reader.MoveToElement();
  459. if(!reader.IsEmptyElement)
  460. {
  461. ReadContent(schema, reader, validationEventHandler);
  462. }
  463. return schema;
  464. }
  465. else
  466. {
  467. //Schema can't be generated. Throw an exception
  468. throw new XmlSchemaException("The root element must be schema", null);
  469. }
  470. default:
  471. error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
  472. break;
  473. }
  474. }
  475. throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);
  476. */
  477. }
  478. private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
  479. {
  480. Exception ex;
  481. reader.MoveToElement();
  482. while(reader.MoveToNextAttribute())
  483. {
  484. switch(reader.Name)
  485. {
  486. case "attributeFormDefault" :
  487. schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
  488. if(ex != null)
  489. error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
  490. break;
  491. case "blockDefault" :
  492. schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");
  493. if(ex != null)
  494. warn(h, ex.Message, ex);
  495. break;
  496. case "elementFormDefault":
  497. schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
  498. if(ex != null)
  499. error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
  500. break;
  501. case "finalDefault":
  502. schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");
  503. if(ex != null)
  504. warn(h, ex.Message , ex);
  505. break;
  506. case "id":
  507. schema.id = reader.Value;
  508. break;
  509. case "targetNamespace":
  510. schema.targetNamespace = reader.Value;
  511. break;
  512. case "version":
  513. schema.version = reader.Value;
  514. break;
  515. case "xml:lang":
  516. schema.language = reader.Value;
  517. break;
  518. default:
  519. if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  520. error(h, reader.Name + " attribute is not allowed in schema element",null);
  521. else
  522. {
  523. XmlSchemaUtil.ReadUnhandledAttribute(reader,schema);
  524. }
  525. break;
  526. }
  527. }
  528. }
  529. private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
  530. {
  531. reader.MoveToElement();
  532. if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
  533. error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
  534. //(include | import | redefine | annotation)*,
  535. //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
  536. int level = 1;
  537. while(reader.ReadNextElement())
  538. {
  539. if(reader.NodeType == XmlNodeType.EndElement)
  540. {
  541. if(reader.LocalName != xmlname)
  542. error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
  543. break;
  544. }
  545. if(level <= 1)
  546. {
  547. if(reader.LocalName == "include")
  548. {
  549. XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
  550. if(include != null)
  551. schema.includes.Add(include);
  552. continue;
  553. }
  554. if(reader.LocalName == "import")
  555. {
  556. XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
  557. if(import != null)
  558. schema.includes.Add(import);
  559. continue;
  560. }
  561. if(reader.LocalName == "redefine")
  562. {
  563. XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
  564. if(redefine != null)
  565. schema.includes.Add(redefine);
  566. continue;
  567. }
  568. if(reader.LocalName == "annotation")
  569. {
  570. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  571. if(annotation != null)
  572. schema.items.Add(annotation);
  573. continue;
  574. }
  575. }
  576. if(level <=2)
  577. {
  578. level = 2;
  579. if(reader.LocalName == "simpleType")
  580. {
  581. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  582. if(stype != null)
  583. schema.items.Add(stype);
  584. continue;
  585. }
  586. if(reader.LocalName == "complexType")
  587. {
  588. XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
  589. if(ctype != null)
  590. schema.items.Add(ctype);
  591. continue;
  592. }
  593. if(reader.LocalName == "group")
  594. {
  595. XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
  596. if(group != null)
  597. schema.items.Add(group);
  598. continue;
  599. }
  600. if(reader.LocalName == "attributeGroup")
  601. {
  602. XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
  603. if(attributeGroup != null)
  604. schema.items.Add(attributeGroup);
  605. continue;
  606. }
  607. if(reader.LocalName == "element")
  608. {
  609. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  610. if(element != null)
  611. schema.items.Add(element);
  612. continue;
  613. }
  614. if(reader.LocalName == "attribute")
  615. {
  616. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  617. if(attr != null)
  618. schema.items.Add(attr);
  619. continue;
  620. }
  621. if(reader.LocalName == "notation")
  622. {
  623. XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
  624. if(notation != null)
  625. schema.items.Add(notation);
  626. continue;
  627. }
  628. if(reader.LocalName == "annotation")
  629. {
  630. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  631. if(annotation != null)
  632. schema.items.Add(annotation);
  633. continue;
  634. }
  635. }
  636. reader.RaiseInvalidElementError();
  637. }
  638. }
  639. #endregion
  640. #region write
  641. public void Write(System.IO.Stream stream)
  642. {
  643. Write(stream,null);
  644. }
  645. public void Write(System.IO.TextWriter writer)
  646. {
  647. Write(writer,null);
  648. }
  649. public void Write(System.Xml.XmlWriter writer)
  650. {
  651. Write(writer,null);
  652. }
  653. public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
  654. {
  655. Write(new XmlTextWriter(stream,null),namespaceManager);
  656. }
  657. public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
  658. {
  659. XmlTextWriter xwriter = new XmlTextWriter(writer);
  660. xwriter.Formatting = Formatting.Indented;
  661. Write(xwriter,namespaceManager);
  662. }
  663. public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
  664. {
  665. if(Namespaces == null)
  666. {
  667. Namespaces = new XmlSerializerNamespaces();
  668. }
  669. //Add the xml schema namespace.
  670. if(Namespaces.Count == 0)
  671. {
  672. Namespaces.Add("xs", XmlSchema.Namespace);
  673. if (TargetNamespace != null && TargetNamespace != String.Empty)
  674. Namespaces.Add("tns", TargetNamespace);
  675. }
  676. if(namespaceManager != null)
  677. {
  678. foreach(string name in namespaceManager)
  679. {
  680. //xml and xmlns namespaced are added by default in namespaceManager.
  681. //So we should ignore them
  682. if(name!="xml" && name != "xmlns")
  683. Namespaces.Add(name,namespaceManager.LookupNamespace(name));
  684. }
  685. }
  686. XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
  687. xser.Serialize(writer,this,Namespaces);
  688. writer.Flush();
  689. }
  690. #endregion
  691. }
  692. }