XmlSchema.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 XmlSchemaObjectCollection items ;
  32. private XmlSchemaObjectTable notations ;
  33. private XmlSchemaObjectTable schemaTypes ;
  34. private string targetNamespace ;
  35. private XmlAttribute[] unhandledAttributes ;
  36. private string version;
  37. private string language;
  38. // Compiler specific things
  39. private XmlSchemaInfo info;
  40. private static string xmlname = "schema";
  41. public XmlSchema()
  42. {
  43. attributeFormDefault= XmlSchemaForm.None;
  44. blockDefault = XmlSchemaDerivationMethod.None;
  45. elementFormDefault = XmlSchemaForm.None;
  46. finalDefault = XmlSchemaDerivationMethod.None;
  47. includes = new XmlSchemaObjectCollection();
  48. isCompiled = false;
  49. items = new XmlSchemaObjectCollection();
  50. attributeGroups = new XmlSchemaObjectTable();
  51. attributes = new XmlSchemaObjectTable();
  52. elements = new XmlSchemaObjectTable();
  53. groups = new XmlSchemaObjectTable();
  54. notations = new XmlSchemaObjectTable();
  55. schemaTypes = new XmlSchemaObjectTable();
  56. }
  57. #region Properties
  58. [DefaultValue(XmlSchemaForm.None)]
  59. [System.Xml.Serialization.XmlAttribute("attributeFormDefault")]
  60. public XmlSchemaForm AttributeFormDefault
  61. {
  62. get{ return attributeFormDefault; }
  63. set{ this.attributeFormDefault = value;}
  64. }
  65. [DefaultValue(XmlSchemaDerivationMethod.None)]
  66. [System.Xml.Serialization.XmlAttribute("blockDefault")]
  67. public XmlSchemaDerivationMethod BlockDefault
  68. {
  69. get{ return blockDefault;}
  70. set{ blockDefault = value;}
  71. }
  72. [DefaultValue(XmlSchemaDerivationMethod.None)]
  73. [System.Xml.Serialization.XmlAttribute("finalDefault")]
  74. public XmlSchemaDerivationMethod FinalDefault
  75. {
  76. get{ return finalDefault;}
  77. set{ finalDefault = value;}
  78. }
  79. [DefaultValue(XmlSchemaForm.None)]
  80. [System.Xml.Serialization.XmlAttribute("elementFormDefault")]
  81. public XmlSchemaForm ElementFormDefault
  82. {
  83. get{ return elementFormDefault;}
  84. set{ elementFormDefault = value;}
  85. }
  86. [System.Xml.Serialization.XmlAttribute("targetNamespace")]
  87. public string TargetNamespace
  88. {
  89. get{ return targetNamespace;}
  90. set{ targetNamespace = value;}
  91. }
  92. [System.Xml.Serialization.XmlAttribute("version")]
  93. public string Version
  94. {
  95. get{ return version;}
  96. set{ version = value;}
  97. }
  98. [XmlElement("include",typeof(XmlSchemaInclude),Namespace="http://www.w3.org/2001/XMLSchema")]
  99. [XmlElement("import",typeof(XmlSchemaImport),Namespace="http://www.w3.org/2001/XMLSchema")]
  100. [XmlElement("redefine",typeof(XmlSchemaRedefine),Namespace="http://www.w3.org/2001/XMLSchema")]
  101. public XmlSchemaObjectCollection Includes
  102. {
  103. get{ return includes;}
  104. }
  105. [XmlElement("simpleType",typeof(XmlSchemaSimpleType),Namespace="http://www.w3.org/2001/XMLSchema")]
  106. [XmlElement("complexType",typeof(XmlSchemaComplexType),Namespace="http://www.w3.org/2001/XMLSchema")]
  107. [XmlElement("group",typeof(XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
  108. //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
  109. //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
  110. [XmlElement("attributeGroup",typeof(XmlSchemaAttributeGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
  111. [XmlElement("element",typeof(XmlSchemaElement),Namespace="http://www.w3.org/2001/XMLSchema")]
  112. [XmlElement("attribute",typeof(XmlSchemaAttribute),Namespace="http://www.w3.org/2001/XMLSchema")]
  113. [XmlElement("notation",typeof(XmlSchemaNotation),Namespace="http://www.w3.org/2001/XMLSchema")]
  114. [XmlElement("annotation",typeof(XmlSchemaAnnotation),Namespace="http://www.w3.org/2001/XMLSchema")]
  115. public XmlSchemaObjectCollection Items
  116. {
  117. get{ return items;}
  118. }
  119. [XmlIgnore]
  120. public bool IsCompiled
  121. {
  122. get{ return isCompiled;}
  123. }
  124. [XmlIgnore]
  125. public XmlSchemaObjectTable Attributes
  126. {
  127. get{ return attributes;}
  128. }
  129. [XmlIgnore]
  130. public XmlSchemaObjectTable AttributeGroups
  131. {
  132. get{ return attributeGroups; }
  133. }
  134. [XmlIgnore]
  135. public XmlSchemaObjectTable SchemaTypes
  136. {
  137. get{ return schemaTypes; }
  138. }
  139. [XmlIgnore]
  140. public XmlSchemaObjectTable Elements
  141. {
  142. get{ return elements;}
  143. }
  144. [System.Xml.Serialization.XmlAttribute("id")]
  145. public string Id
  146. {
  147. get{ return id;}
  148. set{ id = value;}
  149. }
  150. [XmlAnyAttribute]
  151. public XmlAttribute[] UnhandledAttributes
  152. {
  153. get
  154. {
  155. if(unhandledAttributeList != null)
  156. {
  157. unhandledAttributes = (XmlAttribute[]) unhandledAttributeList.ToArray(typeof(XmlAttribute));
  158. unhandledAttributeList = null;
  159. }
  160. return unhandledAttributes;
  161. }
  162. set
  163. {
  164. unhandledAttributes = value;
  165. unhandledAttributeList = null;
  166. }
  167. }
  168. [XmlIgnore]
  169. public XmlSchemaObjectTable Groups
  170. {
  171. get{ return groups;}
  172. }
  173. [XmlIgnore]
  174. public XmlSchemaObjectTable Notations
  175. {
  176. get{ return notations;}
  177. }
  178. // New attribute defined in W3C schema element
  179. [System.Xml.Serialization.XmlAttribute("xml:lang")]
  180. public string Language
  181. {
  182. get{ return language; }
  183. set{ language = value; }
  184. }
  185. #endregion
  186. #region Compile
  187. // Methods
  188. /// <summary>
  189. /// This compile method does two things:
  190. /// 1. It compiles and fills the PSVI dataset
  191. /// 2. Validates the schema by calling Validate method.
  192. /// Every XmlSchemaObject has a Compile Method which gets called.
  193. /// </summary>
  194. /// <remarks>
  195. /// 1. blockDefault must be one of #all | List of (extension | restriction | substitution)
  196. /// 2. finalDefault must be one of (#all | List of (extension | restriction| union| list))
  197. /// 3. id must be of type ID
  198. /// 4. targetNamespace should be any uri
  199. /// 5. version should be a normalizedString
  200. /// 6. xml:lang should be a language
  201. /// </remarks>
  202. [MonoTODO]
  203. public void Compile(ValidationEventHandler handler)
  204. {
  205. // Create the xmlschemainfo object which we use to pass variables like targetnamespace;
  206. info = new XmlSchemaInfo();
  207. //1. Union and List are not allowed in block default
  208. if(BlockDefault != XmlSchemaDerivationMethod.All)
  209. {
  210. if((BlockDefault & XmlSchemaDerivationMethod.List)!=0 )
  211. error(handler, "list is not allowed in blockDefault attribute");
  212. if((BlockDefault & XmlSchemaDerivationMethod.Union)!=0 )
  213. error(handler, "union is not allowed in blockDefault attribute");
  214. }
  215. //2. Substitution is not allowed in finaldefault.
  216. if(FinalDefault != XmlSchemaDerivationMethod.All)
  217. {
  218. if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
  219. error(handler, "substitution is not allowed in finalDefault attribute");
  220. }
  221. //3. id must be of type ID
  222. XmlSchemaUtil.CompileID(Id, this, info.IDCollection, handler);
  223. //4. targetNamespace should be of type anyURI or absent
  224. if(TargetNamespace != null)
  225. {
  226. if(!XmlSchemaUtil.CheckAnyUri(TargetNamespace))
  227. error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
  228. else
  229. info.TargetNamespace = TargetNamespace;
  230. }
  231. //5. version should be of type normalizedString
  232. if(!XmlSchemaUtil.CheckNormalizedString(Version))
  233. error(handler, Version + "is not a valid value for version attribute of schema");
  234. //6. xml:lang must be a language
  235. if(!XmlSchemaUtil.CheckLanguage(Language))
  236. error(handler, Language + " is not a valid language");
  237. // elementFormDefault defaults to UnQualified
  238. if(ElementFormDefault != XmlSchemaForm.Qualified)
  239. info.ElementFormDefault = XmlSchemaForm.Unqualified;
  240. else
  241. info.ElementFormDefault = XmlSchemaForm.Qualified;
  242. // attributeFormDefault defaults to UnQualified
  243. if(AttributeFormDefault != XmlSchemaForm.Qualified)
  244. info.AttributeFormDefault = XmlSchemaForm.Unqualified;
  245. else
  246. info.AttributeFormDefault = XmlSchemaForm.Qualified;
  247. if(FinalDefault == XmlSchemaDerivationMethod.All)
  248. info.FinalDefault = XmlSchemaDerivationMethod.All;
  249. else // If finalDefault is None, info's finalDefault is set to empty
  250. info.FinalDefault = (FinalDefault & (XmlSchemaDerivationMethod.Extension | XmlSchemaDerivationMethod.Restriction));
  251. if(BlockDefault == XmlSchemaDerivationMethod.All)
  252. info.BlockDefault = XmlSchemaDerivationMethod.All;
  253. else // If finalDefault is None, info's blockDefault is set to empty
  254. info.BlockDefault = (blockDefault & (XmlSchemaDerivationMethod.Extension |
  255. XmlSchemaDerivationMethod.Restriction | XmlSchemaDerivationMethod.Substitution));
  256. // Compile the content of this schema
  257. foreach(XmlSchemaObject obj in Includes)
  258. {
  259. if(obj is XmlSchemaExternal)
  260. {
  261. //FIXME: Kuch to karo! (Do Something ;)
  262. }
  263. else
  264. {
  265. error(handler,"Object of Type "+obj.GetType().Name+" is not valid in Includes Property of XmlSchema");
  266. }
  267. }
  268. foreach(XmlSchemaObject obj in Items)
  269. {
  270. if(obj is XmlSchemaAnnotation)
  271. {
  272. int numerr = ((XmlSchemaAnnotation)obj).Compile(handler,info);
  273. errorCount += numerr;
  274. if( numerr == 0)
  275. {
  276. //FIXME: What PSVI set do we add this to?
  277. }
  278. }
  279. else if(obj is XmlSchemaAttribute)
  280. {
  281. XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
  282. attr.parentIsSchema = true;
  283. int numerr = attr.Compile(handler,info);
  284. errorCount += numerr;
  285. if(numerr == 0)
  286. {
  287. Attributes.Add(attr.QualifiedName, attr);
  288. }
  289. }
  290. else if(obj is XmlSchemaAttributeGroup)
  291. {
  292. XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
  293. int numerr = attrgrp.Compile(handler,info);
  294. errorCount += numerr;
  295. if(numerr == 0)
  296. {
  297. AttributeGroups.Add(attrgrp.QualifiedName, attrgrp);
  298. }
  299. }
  300. else if(obj is XmlSchemaComplexType)
  301. {
  302. XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
  303. ctype.istoplevel = true;
  304. int numerr = ctype.Compile(handler,info);
  305. errorCount += numerr;
  306. if(numerr == 0)
  307. {
  308. schemaTypes.Add(ctype.QualifiedName, ctype);
  309. }
  310. }
  311. else if(obj is XmlSchemaSimpleType)
  312. {
  313. XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
  314. stype.islocal = false; //This simple type is toplevel
  315. int numerr = stype.Compile(handler,info);
  316. errorCount += numerr;
  317. if(numerr == 0)
  318. {
  319. SchemaTypes.Add(stype.QualifiedName, stype);
  320. }
  321. }
  322. else if(obj is XmlSchemaElement)
  323. {
  324. XmlSchemaElement elem = (XmlSchemaElement) obj;
  325. elem.parentIsSchema = true;
  326. int numerr = elem.Compile(handler,info);
  327. errorCount += numerr;
  328. if(numerr == 0)
  329. {
  330. Elements.Add(elem.QualifiedName,elem);
  331. }
  332. }
  333. else if(obj is XmlSchemaGroup)
  334. {
  335. XmlSchemaGroup grp = (XmlSchemaGroup) obj;
  336. int numerr = grp.Compile(handler,info);
  337. errorCount += numerr;
  338. if(numerr == 0)
  339. {
  340. Groups.Add(grp.QualifiedName,grp);
  341. }
  342. }
  343. else if(obj is XmlSchemaNotation)
  344. {
  345. XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
  346. int numerr = ntn.Compile(handler,info);
  347. errorCount += numerr;
  348. if(numerr == 0)
  349. {
  350. Notations.Add(ntn.QualifiedName, ntn);
  351. }
  352. }
  353. else
  354. {
  355. ValidationHandler.RaiseValidationError(handler,this,
  356. "Object of Type "+obj.GetType().Name+" is not valid in Item Property of Schema");
  357. }
  358. }
  359. Validate(handler);
  360. }
  361. #endregion
  362. [MonoTODO]
  363. private void Validate(ValidationEventHandler handler)
  364. {
  365. info.SchemaTypes = SchemaTypes;
  366. foreach(XmlSchemaAttribute attr in Attributes.Values)
  367. {
  368. attr.Validate(handler, info);
  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, info);
  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. //Add the xml schema namespace.
  635. if(Namespaces.Count == 0)
  636. {
  637. Namespaces.Add("xs", XmlSchema.Namespace);
  638. if (TargetNamespace != null && TargetNamespace != String.Empty)
  639. Namespaces.Add("tns", TargetNamespace);
  640. }
  641. if(namespaceManager != null)
  642. {
  643. foreach(string name in namespaceManager)
  644. {
  645. //xml and xmlns namespaced are added by default in namespaceManager.
  646. //So we should ignore them
  647. if(name!="xml" && name != "xmlns")
  648. Namespaces.Add(name,namespaceManager.LookupNamespace(name));
  649. }
  650. }
  651. XmlSerializer xser = new XmlSerializer(typeof(XmlSchema));
  652. xser.Serialize(writer,this,Namespaces);
  653. writer.Flush();
  654. }
  655. #endregion
  656. }
  657. }