XmlSchema.cs 22 KB

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