XmlSchema.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. foreach(XmlSchemaAttribute attr in Attributes.Values)
  367. {
  368. attr.Validate(handler);
  369. }
  370. foreach(XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
  371. {
  372. attrgrp.Validate(handler);
  373. }
  374. foreach(XmlSchemaType type in SchemaTypes.Values)
  375. {
  376. if(type is XmlSchemaComplexType)
  377. {
  378. ((XmlSchemaComplexType)type).Validate(handler);
  379. }
  380. else
  381. ((XmlSchemaSimpleType)type).Validate(handler);
  382. }
  383. foreach(XmlSchemaElement elem in Elements.Values)
  384. {
  385. elem.Validate(handler);
  386. }
  387. foreach(XmlSchemaGroup grp in Groups.Values)
  388. {
  389. grp.Validate(handler);
  390. }
  391. foreach(XmlSchemaNotation ntn in Notations.Values)
  392. {
  393. ntn.Validate(handler);
  394. }
  395. }
  396. #region Read
  397. public static XmlSchema Read(TextReader reader, ValidationEventHandler validationEventHandler)
  398. {
  399. return Read(new XmlTextReader(reader),validationEventHandler);
  400. }
  401. public static XmlSchema Read(Stream stream, ValidationEventHandler validationEventHandler)
  402. {
  403. return Read(new XmlTextReader(stream),validationEventHandler);
  404. }
  405. public static XmlSchema Read(XmlReader rdr, ValidationEventHandler validationEventHandler)
  406. {
  407. //XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
  408. //return (XmlSchema) xser.Deserialize(reader);
  409. XmlSchemaReader reader = new XmlSchemaReader(rdr, validationEventHandler);
  410. while(reader.ReadNextElement())
  411. {
  412. switch(reader.NodeType)
  413. {
  414. case XmlNodeType.Element:
  415. if(reader.LocalName == "schema")
  416. {
  417. XmlSchema schema = new XmlSchema();
  418. schema.LineNumber = reader.LineNumber;
  419. schema.LinePosition = reader.LinePosition;
  420. schema.SourceUri = reader.BaseURI;
  421. ReadAttributes(schema, reader, validationEventHandler);
  422. //IsEmptyElement does not behave properly if reader is
  423. //positioned at an attribute.
  424. reader.MoveToElement();
  425. if(!reader.IsEmptyElement)
  426. {
  427. ReadContent(schema, reader, validationEventHandler);
  428. }
  429. return schema;
  430. }
  431. else
  432. {
  433. //Schema can't be generated. Throw an exception
  434. throw new XmlSchemaException("The root element must be schema", null);
  435. }
  436. default:
  437. error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
  438. break;
  439. }
  440. }
  441. throw new XmlSchemaException("The top level schema must have namespace "+XmlSchema.Namespace, null);
  442. }
  443. private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
  444. {
  445. Exception ex;
  446. reader.MoveToElement();
  447. while(reader.MoveToNextAttribute())
  448. {
  449. switch(reader.Name)
  450. {
  451. case "attributeFormDefault" :
  452. schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
  453. if(ex != null)
  454. error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
  455. break;
  456. case "blockDefault" :
  457. schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault");
  458. if(ex != null)
  459. warn(h, ex.Message, ex);
  460. break;
  461. case "elementFormDefault":
  462. schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
  463. if(ex != null)
  464. error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
  465. break;
  466. case "finalDefault":
  467. schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault");
  468. if(ex != null)
  469. warn(h, ex.Message , ex);
  470. break;
  471. case "id":
  472. schema.id = reader.Value;
  473. break;
  474. case "targetNamespace":
  475. schema.targetNamespace = reader.Value;
  476. break;
  477. case "version":
  478. schema.version = reader.Value;
  479. break;
  480. case "xml:lang":
  481. schema.language = reader.Value;
  482. break;
  483. default:
  484. if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
  485. error(h, reader.Name + " attribute is not allowed in schema element",null);
  486. else
  487. {
  488. XmlSchemaUtil.ReadUnhandledAttribute(reader,schema);
  489. }
  490. break;
  491. }
  492. }
  493. }
  494. private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
  495. {
  496. reader.MoveToElement();
  497. if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
  498. error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
  499. //(include | import | redefine | annotation)*,
  500. //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
  501. int level = 1;
  502. while(reader.ReadNextElement())
  503. {
  504. if(reader.NodeType == XmlNodeType.EndElement)
  505. {
  506. if(reader.LocalName != xmlname)
  507. error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
  508. break;
  509. }
  510. if(level <= 1)
  511. {
  512. if(reader.LocalName == "include")
  513. {
  514. XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
  515. if(include != null)
  516. schema.includes.Add(include);
  517. continue;
  518. }
  519. if(reader.LocalName == "import")
  520. {
  521. XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
  522. if(import != null)
  523. schema.includes.Add(import);
  524. continue;
  525. }
  526. if(reader.LocalName == "redefine")
  527. {
  528. XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
  529. if(redefine != null)
  530. schema.includes.Add(redefine);
  531. continue;
  532. }
  533. if(reader.LocalName == "annotation")
  534. {
  535. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  536. if(annotation != null)
  537. schema.items.Add(annotation);
  538. continue;
  539. }
  540. }
  541. if(level <=2)
  542. {
  543. level = 2;
  544. if(reader.LocalName == "simpleType")
  545. {
  546. XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
  547. if(stype != null)
  548. schema.items.Add(stype);
  549. continue;
  550. }
  551. if(reader.LocalName == "complexType")
  552. {
  553. XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
  554. if(ctype != null)
  555. schema.items.Add(ctype);
  556. continue;
  557. }
  558. if(reader.LocalName == "group")
  559. {
  560. XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
  561. if(group != null)
  562. schema.items.Add(group);
  563. continue;
  564. }
  565. if(reader.LocalName == "attributeGroup")
  566. {
  567. XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
  568. if(attributeGroup != null)
  569. schema.items.Add(attributeGroup);
  570. continue;
  571. }
  572. if(reader.LocalName == "element")
  573. {
  574. XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
  575. if(element != null)
  576. schema.items.Add(element);
  577. continue;
  578. }
  579. if(reader.LocalName == "attribute")
  580. {
  581. XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
  582. if(attr != null)
  583. schema.items.Add(attr);
  584. continue;
  585. }
  586. if(reader.LocalName == "notation")
  587. {
  588. XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
  589. if(notation != null)
  590. schema.items.Add(notation);
  591. continue;
  592. }
  593. if(reader.LocalName == "annotation")
  594. {
  595. XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
  596. if(annotation != null)
  597. schema.items.Add(annotation);
  598. continue;
  599. }
  600. }
  601. reader.RaiseInvalidElementError();
  602. }
  603. }
  604. #endregion
  605. #region write
  606. public void Write(System.IO.Stream stream)
  607. {
  608. Write(stream,null);
  609. }
  610. public void Write(System.IO.TextWriter writer)
  611. {
  612. Write(writer,null);
  613. }
  614. public void Write(System.Xml.XmlWriter writer)
  615. {
  616. Write(writer,null);
  617. }
  618. public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
  619. {
  620. Write(new XmlTextWriter(stream,null),namespaceManager);
  621. }
  622. public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
  623. {
  624. XmlTextWriter xwriter = new XmlTextWriter(writer);
  625. xwriter.Formatting = Formatting.Indented;
  626. Write(xwriter,namespaceManager);
  627. }
  628. public void Write(System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
  629. {
  630. if(Namespaces == null)
  631. {
  632. Namespaces = new XmlSerializerNamespaces();
  633. }
  634. if(namespaceManager != null)
  635. {
  636. foreach(string name in namespaceManager)
  637. {
  638. //xml and xmlns namespaced are added by default in namespaceManager.
  639. //So we should ignore them
  640. if(name!="xml" && name != "xmlns")
  641. Namespaces.Add(name,namespaceManager.LookupNamespace(name));
  642. }
  643. }
  644. XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
  645. xser.Serialize(writer,this,Namespaces);
  646. writer.Flush();
  647. }
  648. #endregion
  649. }
  650. }