I need to add some tags to an existing XML schema and write it to file. However, I need to add tags to every of the existing schema. What is the best way to accomplish it in C#?
Thanks!
I have the following code that parses the schema:
/// <summary> /// Iterates over all elements in a XmlSchema and prints them out /// </summary> /// <param name='schema'></param> private void IterateOverSchemaElements(XmlSchema schema) { XmlSchemaComplexType complex; foreach (XmlSchemaObject schemaObj in schema.Items) { complex = schemaObj as XmlSchemaComplexType; if (complex != null) { if (OutputTextDelegate != null) { OutputTextDelegate(string.Format('ComplexType: {0}', complex.Name)); } complex.Annotation = new XmlSchemaAnnotation(); //Get sequence of the complextype: XmlSchemaSequence sequence = complex.ContentTypeParticle as XmlSchemaSequence; if (sequence != null) { // Iterate over each XmlSchemaElement in the Items collection. foreach (XmlSchemaElement childElement in sequence.Items) { if (OutputTextDelegate != null) { OutputTextDelegate(string.Format('--Element: {0}', childElement.Name)); } } } } } }
You may be better off manipulating the schema as an XML document, not as a schema. For instance, this code creates an annotation under every element definition that’s part of a sequence in a complex type: