2 // System.Xml.Schema.XmlSchema.cs
5 // Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 // Atsushi Enomoto ginga@kit.hi-ho.ne.jp
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
33 using System.Xml.Serialization;
34 using System.ComponentModel;
36 namespace System.Xml.Schema
39 /// Summary description for XmlSchema.
41 [XmlRoot ("schema",Namespace=XmlSchema.Namespace)]
42 public class XmlSchema : XmlSchemaObject
45 public const string Namespace = "http://www.w3.org/2001/XMLSchema";
46 public const string InstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
47 internal const string XdtNamespace = "http://www.w3.org/2003/11/xpath-datatypes";
50 private XmlSchemaForm attributeFormDefault ;
51 private XmlSchemaObjectTable attributeGroups ;
52 private XmlSchemaObjectTable attributes ;
53 private XmlSchemaDerivationMethod blockDefault ;
54 private XmlSchemaForm elementFormDefault ;
55 private XmlSchemaObjectTable elements ;
56 private XmlSchemaDerivationMethod finalDefault ;
57 private XmlSchemaObjectTable groups ;
59 private XmlSchemaObjectCollection includes ;
60 private XmlSchemaObjectCollection items ;
61 private XmlSchemaObjectTable notations ;
62 private XmlSchemaObjectTable schemaTypes ;
63 private string targetNamespace ;
64 private XmlAttribute[] unhandledAttributes ;
65 private string version;
66 private string language;
68 // other post schema compilation infoset
69 private Hashtable idCollection;
70 private XmlSchemaObjectTable namedIdentities;
71 private XmlSchemaSet schemas;
73 private XmlNameTable nameTable;
75 internal bool missedSubComponents;
77 // Only compilation-time use
78 private XmlSchemaObjectCollection compilationItems;
79 private Hashtable handledUris;
81 // Compiler specific things
82 const string xmlname = "schema";
86 attributeFormDefault= XmlSchemaForm.None;
87 blockDefault = XmlSchemaDerivationMethod.None;
88 elementFormDefault = XmlSchemaForm.None;
89 finalDefault = XmlSchemaDerivationMethod.None;
90 includes = new XmlSchemaObjectCollection();
92 items = new XmlSchemaObjectCollection();
93 attributeGroups = new XmlSchemaObjectTable();
94 attributes = new XmlSchemaObjectTable();
95 elements = new XmlSchemaObjectTable();
96 groups = new XmlSchemaObjectTable();
97 notations = new XmlSchemaObjectTable();
98 schemaTypes = new XmlSchemaObjectTable();
99 idCollection = new Hashtable ();
100 namedIdentities = new XmlSchemaObjectTable();
105 [DefaultValue (XmlSchemaForm.None)]
106 [System.Xml.Serialization.XmlAttribute ("attributeFormDefault")]
107 public XmlSchemaForm AttributeFormDefault
109 get{ return attributeFormDefault; }
110 set{ this.attributeFormDefault = value;}
113 [DefaultValue (XmlSchemaDerivationMethod.None)]
114 [System.Xml.Serialization.XmlAttribute ("blockDefault")]
115 public XmlSchemaDerivationMethod BlockDefault
117 get{ return blockDefault;}
118 set{ blockDefault = value;}
121 [DefaultValue (XmlSchemaDerivationMethod.None)]
122 [System.Xml.Serialization.XmlAttribute ("finalDefault")]
123 public XmlSchemaDerivationMethod FinalDefault
125 get{ return finalDefault; }
126 set{ finalDefault = value; }
129 [DefaultValue (XmlSchemaForm.None)]
130 [System.Xml.Serialization.XmlAttribute ("elementFormDefault")]
131 public XmlSchemaForm ElementFormDefault
133 get{ return elementFormDefault; }
134 set{ elementFormDefault = value; }
137 [System.Xml.Serialization.XmlAttribute ("targetNamespace")]
138 public string TargetNamespace
140 get{ return targetNamespace; }
141 set{ targetNamespace = value; }
144 [System.Xml.Serialization.XmlAttribute ("version")]
145 public string Version
147 get{ return version; }
148 set{ version = value; }
151 [XmlElement ("include",typeof(XmlSchemaInclude), Namespace="http://www.w3.org/2001/XMLSchema")]
152 [XmlElement ("import",typeof(XmlSchemaImport), Namespace="http://www.w3.org/2001/XMLSchema")]
153 [XmlElement ("redefine",typeof(XmlSchemaRedefine), Namespace="http://www.w3.org/2001/XMLSchema")]
154 public XmlSchemaObjectCollection Includes
156 get{ return includes;}
159 [XmlElement ("simpleType", typeof (XmlSchemaSimpleType), Namespace="http://www.w3.org/2001/XMLSchema")]
160 [XmlElement ("complexType", typeof (XmlSchemaComplexType), Namespace="http://www.w3.org/2001/XMLSchema")]
161 [XmlElement ("group", typeof (XmlSchemaGroup),Namespace="http://www.w3.org/2001/XMLSchema")]
162 //Only Schema's attributeGroup has type XmlSchemaAttributeGroup.
163 //Others (complextype, restrictions etc) must have XmlSchemaAttributeGroupRef
164 [XmlElement ("attributeGroup", typeof (XmlSchemaAttributeGroup), Namespace="http://www.w3.org/2001/XMLSchema")]
165 [XmlElement ("element", typeof (XmlSchemaElement), Namespace="http://www.w3.org/2001/XMLSchema")]
166 [XmlElement ("attribute", typeof (XmlSchemaAttribute), Namespace="http://www.w3.org/2001/XMLSchema")]
167 [XmlElement ("notation", typeof (XmlSchemaNotation), Namespace="http://www.w3.org/2001/XMLSchema")]
168 [XmlElement ("annotation", typeof (XmlSchemaAnnotation), Namespace="http://www.w3.org/2001/XMLSchema")]
169 public XmlSchemaObjectCollection Items
175 public bool IsCompiled
177 get{ return this.CompilationId != Guid.Empty; }
181 public XmlSchemaObjectTable Attributes
183 get{ return attributes; }
187 public XmlSchemaObjectTable AttributeGroups
189 get{ return attributeGroups; }
193 public XmlSchemaObjectTable SchemaTypes
195 get{ return schemaTypes; }
199 public XmlSchemaObjectTable Elements
201 get{ return elements; }
204 [System.Xml.Serialization.XmlAttribute ("id")]
212 public XmlAttribute [] UnhandledAttributes
215 if (unhandledAttributeList != null) {
216 unhandledAttributes = (XmlAttribute []) unhandledAttributeList.ToArray (typeof (XmlAttribute));
217 unhandledAttributeList = null;
219 return unhandledAttributes;
222 unhandledAttributes = value;
223 unhandledAttributeList = null;
228 public XmlSchemaObjectTable Groups
230 get{ return groups; }
234 public XmlSchemaObjectTable Notations
236 get{ return notations; }
239 internal Hashtable IDCollection
241 get { return idCollection; }
244 internal XmlSchemaObjectTable NamedIdentities
246 get { return namedIdentities; }
249 internal XmlSchemaSet Schemas
251 get { return schemas; }
259 /// This compile method does two things:
260 /// 1. It compiles and fills the PSVI dataset
261 /// 2. Validates the schema by calling Validate method.
262 /// Every XmlSchemaObject has a Compile Method which gets called.
265 /// 1. blockDefault must be one of #all | List of (extension | restriction | substitution)
266 /// 2. finalDefault must be one of (#all | List of (extension | restriction| union| list))
267 /// 3. id must be of type ID
268 /// 4. targetNamespace should be any uri
269 /// 5. version should be a normalizedString
270 /// 6. xml:lang should be a language
272 public void Compile (ValidationEventHandler handler)
274 Compile (handler, new XmlUrlResolver ());
278 public void Compile (ValidationEventHandler handler, XmlResolver resolver)
280 internal void Compile (ValidationEventHandler handler, XmlResolver resolver)
283 Compile (handler, new Stack (), this, null, resolver);
286 internal void Compile (ValidationEventHandler handler, XmlSchemaSet col, XmlResolver resolver)
288 Compile (handler, new Stack (), this, col, resolver);
291 private void Compile (ValidationEventHandler handler, Stack schemaLocationStack, XmlSchema rootSchema, XmlSchemaSet col, XmlResolver resolver)
293 if (rootSchema != this) {
294 CompilationId = rootSchema.CompilationId;
295 schemas = rootSchema.schemas;
299 if (schemas == null) {
300 schemas = new XmlSchemaSet ();
301 schemas.CompilationId = Guid.NewGuid ();
303 CompilationId = schemas.CompilationId;
304 this.idCollection.Clear ();
306 if (!schemas.Contains (this)) // e.g. xs:import
309 attributeGroups.Clear ();
314 schemaTypes.Clear ();
315 namedIdentities.Clear ();
317 //1. Union and List are not allowed in block default
318 if (BlockDefault != XmlSchemaDerivationMethod.All) {
319 if((BlockDefault & XmlSchemaDerivationMethod.List)!=0 )
320 error(handler, "list is not allowed in blockDefault attribute");
321 if((BlockDefault & XmlSchemaDerivationMethod.Union)!=0 )
322 error(handler, "union is not allowed in blockDefault attribute");
325 //2. Substitution is not allowed in finaldefault.
326 if (FinalDefault != XmlSchemaDerivationMethod.All) {
327 if((FinalDefault & XmlSchemaDerivationMethod.Substitution)!=0 )
328 error(handler, "substitution is not allowed in finalDefault attribute");
331 //3. id must be of type ID
332 XmlSchemaUtil.CompileID(Id, this, this.IDCollection, handler);
334 //4. targetNamespace should be of type anyURI or absent
335 if (TargetNamespace != null) {
336 if(!XmlSchemaUtil.CheckAnyUri (TargetNamespace))
337 error(handler, TargetNamespace+" is not a valid value for targetNamespace attribute of schema");
340 //5. version should be of type normalizedString
341 if (!XmlSchemaUtil.CheckNormalizedString(Version))
342 error(handler, Version + "is not a valid value for version attribute of schema");
344 //6. xml:lang must be a language (removed)
346 // Compile the content of this schema
348 compilationItems = new XmlSchemaObjectCollection ();
349 for (int i = 0; i < Items.Count; i++)
350 compilationItems.Add (Items [i]);
351 if (this == rootSchema)
352 handledUris = new Hashtable ();
354 // First, we run into inclusion schemas to collect
355 // compilation target items into compiledItems.
356 for (int i = 0; i < Includes.Count; i++) {
357 XmlSchemaExternal ext = Includes [i] as XmlSchemaExternal;
359 error (handler, String.Format ("Object of Type {0} is not valid in Includes Property of XmlSchema", Includes [i].GetType().Name));
363 if (ext.SchemaLocation == null)
366 Stream stream = null;
368 if (resolver != null) {
369 url = GetResolvedUri (resolver, ext.SchemaLocation);
370 if (schemaLocationStack.Contains (url)) {
371 error (handler, "Nested inclusion was found: " + url);
372 // must skip this inclusion
375 if (rootSchema.handledUris.Contains (url))
376 // This schema is already handled, so simply skip (otherwise, duplicate definition errrors occur.
378 rootSchema.handledUris.Add (url, url);
380 stream = resolver.GetEntity (new Uri (url), null, typeof (Stream)) as Stream;
381 } catch (Exception) {
382 // LAMESPEC: This is not good way to handle errors, but since we cannot know what kind of XmlResolver will come, so there are no mean to avoid this ugly catch.
383 warn (handler, "Could not resolve schema location URI: " + url);
388 // Process redefinition children in advance.
389 XmlSchemaRedefine redefine = Includes [i] as XmlSchemaRedefine;
390 if (redefine != null) {
391 for (int j = 0; j < redefine.Items.Count; j++) {
392 XmlSchemaObject redefinedObj = redefine.Items [j];
393 redefinedObj.isRedefinedComponent = true;
394 redefinedObj.isRedefineChild = true;
395 if (redefinedObj is XmlSchemaType ||
396 redefinedObj is XmlSchemaGroup ||
397 redefinedObj is XmlSchemaAttributeGroup)
398 compilationItems.Add (redefinedObj);
400 error (handler, "Redefinition is only allowed to simpleType, complexType, group and attributeGroup.");
404 XmlSchema includedSchema = null;
405 if (stream == null) {
406 // It is missing schema components.
407 missedSubComponents = true;
410 schemaLocationStack.Push (url);
411 XmlTextReader xtr = null;
413 xtr = new XmlTextReader (url, stream, nameTable);
414 includedSchema = XmlSchema.Read (xtr, handler);
419 includedSchema.schemas = schemas;
422 // Set - actual - target namespace for the included schema * before compilation*.
423 XmlSchemaImport import = ext as XmlSchemaImport;
424 if (import != null) {
425 if (TargetNamespace == includedSchema.TargetNamespace) {
426 error (handler, "Target namespace must be different from that of included schema.");
428 } else if (includedSchema.TargetNamespace != import.Namespace) {
429 error (handler, "Attribute namespace and its importing schema's target namespace must be the same.");
433 if (TargetNamespace == null &&
434 includedSchema.TargetNamespace != null) {
435 error (handler, "Target namespace is required to include a schema which has its own target namespace");
438 else if (TargetNamespace != null &&
439 includedSchema.TargetNamespace == null)
440 includedSchema.TargetNamespace = TargetNamespace;
443 // Compile included schema.
444 includedSchema.idCollection = this.IDCollection;
445 includedSchema.Compile (handler, schemaLocationStack, rootSchema, col, resolver);
446 schemaLocationStack.Pop ();
449 rootSchema.schemas.Add (includedSchema);
451 // Note that we use compiled items. Items
452 // may not exist in Items, since included
453 // schema also includes another schemas.
454 foreach (DictionaryEntry entry in includedSchema.Attributes)
455 compilationItems.Add ((XmlSchemaObject) entry.Value);
456 foreach (DictionaryEntry entry in includedSchema.Elements)
457 compilationItems.Add ((XmlSchemaObject) entry.Value);
458 foreach (DictionaryEntry entry in includedSchema.SchemaTypes)
459 compilationItems.Add ((XmlSchemaObject) entry.Value);
460 foreach (DictionaryEntry entry in includedSchema.AttributeGroups)
461 compilationItems.Add ((XmlSchemaObject) entry.Value);
462 foreach (DictionaryEntry entry in includedSchema.Groups)
463 compilationItems.Add ((XmlSchemaObject) entry.Value);
464 foreach (DictionaryEntry entry in includedSchema.Notations)
465 compilationItems.Add ((XmlSchemaObject) entry.Value);
468 // Compilation phase.
469 // At least each Compile() must gives unique (qualified) name for each component.
470 // It also checks self-resolvable properties correct.
471 // Post compilation schema information contribution is not required here.
472 // It should be done by Validate().
473 for (int i = 0; i < compilationItems.Count; i++) {
474 XmlSchemaObject obj = compilationItems [i];
475 if(obj is XmlSchemaAnnotation) {
476 int numerr = ((XmlSchemaAnnotation)obj).Compile (handler, this);
477 errorCount += numerr;
478 } else if (obj is XmlSchemaAttribute) {
479 XmlSchemaAttribute attr = (XmlSchemaAttribute) obj;
480 attr.ParentIsSchema = true;
481 int numerr = attr.Compile (handler, this);
482 errorCount += numerr;
485 XmlSchemaUtil.AddToTable (Attributes, attr, attr.QualifiedName, handler);
487 } else if (obj is XmlSchemaAttributeGroup) {
488 XmlSchemaAttributeGroup attrgrp = (XmlSchemaAttributeGroup) obj;
489 int numerr = attrgrp.Compile(handler, this);
490 errorCount += numerr;
492 XmlSchemaUtil.AddToTable (
495 attrgrp.QualifiedName,
497 } else if (obj is XmlSchemaComplexType) {
498 XmlSchemaComplexType ctype = (XmlSchemaComplexType) obj;
499 ctype.ParentIsSchema = true;
500 int numerr = ctype.Compile (handler, this);
501 errorCount += numerr;
503 XmlSchemaUtil.AddToTable (
508 } else if (obj is XmlSchemaSimpleType) {
509 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) obj;
510 stype.islocal = false; //This simple type is toplevel
511 int numerr = stype.Compile (handler, this);
512 errorCount += numerr;
514 XmlSchemaUtil.AddToTable (
519 } else if (obj is XmlSchemaElement) {
520 XmlSchemaElement elem = (XmlSchemaElement) obj;
521 elem.parentIsSchema = true;
522 int numerr = elem.Compile (handler, this);
523 errorCount += numerr;
525 XmlSchemaUtil.AddToTable (
530 } else if (obj is XmlSchemaGroup) {
531 XmlSchemaGroup grp = (XmlSchemaGroup) obj;
532 int numerr = grp.Compile (handler, this);
533 errorCount += numerr;
535 XmlSchemaUtil.AddToTable (
540 } else if (obj is XmlSchemaNotation) {
541 XmlSchemaNotation ntn = (XmlSchemaNotation) obj;
542 int numerr = ntn.Compile (handler, this);
543 errorCount += numerr;
545 XmlSchemaUtil.AddToTable (
551 ValidationHandler.RaiseValidationEvent (
554 String.Format ("Object of Type {0} is not valid in Item Property of Schema", obj.GetType ().Name),
558 XmlSeverityType.Error);
562 if (rootSchema == this)
570 private string GetResolvedUri (XmlResolver resolver, string relativeUri)
573 if (this.SourceUri != null && this.SourceUri != String.Empty)
574 baseUri = new Uri (this.SourceUri);
575 return resolver.ResolveUri (baseUri, relativeUri).ToString ();
578 internal bool IsNamespaceAbsent (string ns)
580 return !schemas.Contains (ns);
585 private void Validate (ValidationEventHandler handler)
587 ValidationId = CompilationId;
589 // Firstly Element needs to be filled their substitution group info
590 foreach (XmlSchemaElement elem in Elements.Values)
591 elem.FillSubstitutionElementInfo ();
594 foreach (XmlSchemaAttribute attr in Attributes.Values)
595 errorCount += attr.Validate (handler, this);
596 foreach (XmlSchemaAttributeGroup attrgrp in AttributeGroups.Values)
597 errorCount += attrgrp.Validate (handler, this);
598 foreach (XmlSchemaType type in SchemaTypes.Values)
599 errorCount += type.Validate (handler, this);
600 foreach (XmlSchemaElement elem in Elements.Values)
601 errorCount += elem.Validate (handler, this);
602 foreach (XmlSchemaGroup grp in Groups.Values)
603 errorCount += grp.Validate (handler, this);
604 foreach (XmlSchemaNotation ntn in Notations.Values)
605 errorCount += ntn.Validate (handler, this);
610 // We cannot use xml deserialization, since it does not provide line info, qname context, and so on.
611 public static XmlSchema Read (TextReader reader, ValidationEventHandler validationEventHandler)
613 return Read (new XmlTextReader (reader),validationEventHandler);
615 public static XmlSchema Read (Stream stream, ValidationEventHandler validationEventHandler)
617 return Read (new XmlTextReader (stream),validationEventHandler);
620 public static XmlSchema Read (XmlReader rdr, ValidationEventHandler validationEventHandler)
622 XmlSchemaReader reader = new XmlSchemaReader (rdr, validationEventHandler);
624 if (reader.ReadState == ReadState.Initial)
625 reader.ReadNextElement ();
627 int startDepth = reader.Depth;
631 switch(reader.NodeType)
633 case XmlNodeType.Element:
634 if(reader.LocalName == "schema")
636 XmlSchema schema = new XmlSchema ();
637 schema.nameTable = rdr.NameTable;
639 schema.LineNumber = reader.LineNumber;
640 schema.LinePosition = reader.LinePosition;
641 schema.SourceUri = reader.BaseURI;
643 ReadAttributes(schema, reader, validationEventHandler);
644 //IsEmptyElement does not behave properly if reader is
645 //positioned at an attribute.
646 reader.MoveToElement();
647 if(!reader.IsEmptyElement)
649 ReadContent(schema, reader, validationEventHandler);
654 if (rdr.NodeType == XmlNodeType.EndElement)
659 //Schema can't be generated. Throw an exception
660 error (validationEventHandler, "The root element must be schema", null);
663 error(validationEventHandler, "This should never happen. XmlSchema.Read 1 ",null);
666 } while(reader.Depth > startDepth && reader.ReadNextElement());
668 // This is thrown regardless of ValidationEventHandler existence.
669 throw new XmlSchemaException ("The top level schema must have namespace " + XmlSchema.Namespace, null);
672 private static void ReadAttributes(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
676 reader.MoveToElement();
677 while(reader.MoveToNextAttribute())
681 case "attributeFormDefault" :
682 schema.attributeFormDefault = XmlSchemaUtil.ReadFormAttribute(reader,out ex);
684 error(h, reader.Value + " is not a valid value for attributeFormDefault.", ex);
686 case "blockDefault" :
687 schema.blockDefault = XmlSchemaUtil.ReadDerivationAttribute(reader,out ex, "blockDefault",
688 XmlSchemaUtil.ElementBlockAllowed);
690 error (h, ex.Message, ex);
692 case "elementFormDefault":
693 schema.elementFormDefault = XmlSchemaUtil.ReadFormAttribute(reader, out ex);
695 error(h, reader.Value + " is not a valid value for elementFormDefault.", ex);
698 schema.finalDefault = XmlSchemaUtil.ReadDerivationAttribute(reader, out ex, "finalDefault",
699 XmlSchemaUtil.FinalAllowed);
701 error (h, ex.Message , ex);
704 schema.id = reader.Value;
706 case "targetNamespace":
707 schema.targetNamespace = reader.Value;
710 schema.version = reader.Value;
713 schema.language = reader.Value;
716 if((reader.NamespaceURI == "" && reader.Name != "xmlns") || reader.NamespaceURI == XmlSchema.Namespace)
717 error(h, reader.Name + " attribute is not allowed in schema element",null);
720 XmlSchemaUtil.ReadUnhandledAttribute(reader,schema);
727 private static void ReadContent(XmlSchema schema, XmlSchemaReader reader, ValidationEventHandler h)
729 reader.MoveToElement();
730 if(reader.LocalName != "schema" && reader.NamespaceURI != XmlSchema.Namespace && reader.NodeType != XmlNodeType.Element)
731 error(h, "UNREACHABLE CODE REACHED: Method: Schema.ReadContent, " + reader.LocalName + ", " + reader.NamespaceURI,null);
733 //(include | import | redefine | annotation)*,
734 //((simpleType | complexType | group | attributeGroup | element | attribute | notation | annotation)*
736 while(reader.ReadNextElement())
738 if(reader.NodeType == XmlNodeType.EndElement)
740 if(reader.LocalName != xmlname)
741 error(h,"Should not happen :2: XmlSchema.Read, name="+reader.Name,null);
746 if(reader.LocalName == "include")
748 XmlSchemaInclude include = XmlSchemaInclude.Read(reader,h);
750 schema.includes.Add(include);
753 if(reader.LocalName == "import")
755 XmlSchemaImport import = XmlSchemaImport.Read(reader,h);
757 schema.includes.Add(import);
760 if(reader.LocalName == "redefine")
762 XmlSchemaRedefine redefine = XmlSchemaRedefine.Read(reader,h);
764 schema.includes.Add(redefine);
767 if(reader.LocalName == "annotation")
769 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
770 if(annotation != null)
771 schema.items.Add(annotation);
778 if(reader.LocalName == "simpleType")
780 XmlSchemaSimpleType stype = XmlSchemaSimpleType.Read(reader,h);
782 schema.items.Add(stype);
785 if(reader.LocalName == "complexType")
787 XmlSchemaComplexType ctype = XmlSchemaComplexType.Read(reader,h);
789 schema.items.Add(ctype);
792 if(reader.LocalName == "group")
794 XmlSchemaGroup group = XmlSchemaGroup.Read(reader,h);
796 schema.items.Add(group);
799 if(reader.LocalName == "attributeGroup")
801 XmlSchemaAttributeGroup attributeGroup = XmlSchemaAttributeGroup.Read(reader,h);
802 if(attributeGroup != null)
803 schema.items.Add(attributeGroup);
806 if(reader.LocalName == "element")
808 XmlSchemaElement element = XmlSchemaElement.Read(reader,h);
810 schema.items.Add(element);
813 if(reader.LocalName == "attribute")
815 XmlSchemaAttribute attr = XmlSchemaAttribute.Read(reader,h);
817 schema.items.Add(attr);
820 if(reader.LocalName == "notation")
822 XmlSchemaNotation notation = XmlSchemaNotation.Read(reader,h);
824 schema.items.Add(notation);
827 if(reader.LocalName == "annotation")
829 XmlSchemaAnnotation annotation = XmlSchemaAnnotation.Read(reader,h);
830 if(annotation != null)
831 schema.items.Add(annotation);
835 reader.RaiseInvalidElementError();
842 public void Write(System.IO.Stream stream)
846 public void Write(System.IO.TextWriter writer)
850 public void Write(System.Xml.XmlWriter writer)
854 public void Write(System.IO.Stream stream, System.Xml.XmlNamespaceManager namespaceManager)
856 Write(new XmlTextWriter(stream,null),namespaceManager);
858 public void Write(System.IO.TextWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
860 XmlTextWriter xwriter = new XmlTextWriter(writer);
861 xwriter.Formatting = Formatting.Indented;
862 Write(xwriter,namespaceManager);
865 public void Write (System.Xml.XmlWriter writer, System.Xml.XmlNamespaceManager namespaceManager)
867 XmlSerializerNamespaces nss = new XmlSerializerNamespaces ();
869 if (namespaceManager != null) {
871 nss = new XmlSerializerNamespaces ();
872 foreach (string name in namespaceManager) {
873 //xml and xmlns namespaces are added by default in namespaceManager.
874 //So we should ignore them
875 if (name !="xml" && name != "xmlns")
876 nss.Add (name, namespaceManager.LookupNamespace (name));
880 if (Namespaces != null && Namespaces.Count > 0) {
881 nss.Add (String.Empty, XmlSchema.Namespace);
882 foreach (XmlQualifiedName qn in Namespaces.ToArray ()) {
883 nss.Add (qn.Name, qn.Namespace);
887 if (nss.Count == 0) {
888 // Add the xml schema namespace. (It is done
889 // only when no entry exists in Namespaces).
890 nss.Add ("xs", XmlSchema.Namespace);
891 if (TargetNamespace != null)
892 nss.Add ("tns", TargetNamespace);
895 XmlSchemaSerializer xser = new XmlSchemaSerializer ();
896 xser.Serialize (writer, this, nss);
902 class XmlSchemaSerializer : XmlSerializer
904 protected override void Serialize (object o, XmlSerializationWriter writer)
906 XmlSchemaSerializationWriter w = writer as XmlSchemaSerializationWriter;
907 w.WriteRoot_XmlSchema ((XmlSchema) o);
910 protected override XmlSerializationWriter CreateWriter ()
912 return new XmlSchemaSerializationWriter ();