Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / C#

PropertyGrid

Rate me:
Please Sign up or sign in to vote.
3.22/5 (3 votes)
3 Nov 20069 min read 52.2K   1.3K   30  
A control which provides a convenient way to display data in a property grid.
using System;

namespace Puma.Xml
{
	public class XmlXsdDocument : XmlXsdNodeBuilder
	{
        public XmlXsdDocument(string XsdFileName, ConstructParams ConstructParams, NodeExtensible NodeExtensible)
        {
            if (ConstructParams != null)
            {
                this.ConstructParams = ConstructParams;
            }

            IniXmlXsdDocument(XsdFileName, NodeExtensible);

            if (ConstructParams != null)
            {
                //!!!Set default params!!!
                this.ConstructParams = new ConstructParams();
            }
        }

		public XmlXsdDocument(string XsdFileName, NodeExtensible NodeExtensible) 
		{	
			IniXmlXsdDocument(XsdFileName, NodeExtensible);
		}

		public XmlXsdDocument(string XsdFileName) 
		{	
			IniXmlXsdDocument(XsdFileName, null);
		}

		public XmlXsdDocument(string XmlFileName, string XsdFileName, NodeExtensible NodeExtensible) 
		{
            if (XmlFileName != null)
			    IniXmlXsdDocument(XmlFileName, XsdFileName, NodeExtensible);
            else
                IniXmlXsdDocument(XsdFileName, NodeExtensible);
		}

		public XmlXsdDocument(string XmlFileName, string XsdFileName) 
		{
            if (XmlFileName != null)
			    IniXmlXsdDocument(XmlFileName, XsdFileName, null);
            else
                IniXmlXsdDocument(XsdFileName, null);
		}

        public NodeExtensible NodeExtensible { get { return _nodeExtensible; } }

        private NodeExtensible _nodeExtensible;

        private Puma.Xml.XmlDocument _xmlDocument;
        private Puma.Xml.XsdDocument _xsdDocument;

        internal ConstructParams ConstructParams = new ConstructParams();

		private void IniXmlXsdDocument(string XsdFileName, NodeExtensible NodeExtensible) 
		{
			try
			{
				_nodeExtensible = NodeExtensible;

				System.Xml.XmlTextReader xmlTextReader = new System.Xml.XmlTextReader(new System.IO.StreamReader(XsdFileName));
				
				System.Xml.Schema.XmlSchema sysXmlSchema = System.Xml.Schema.XmlSchema.Read(xmlTextReader, new System.Xml.Schema.ValidationEventHandler(xmlValidatingReader_ValidationEventHandler));			

				System.Diagnostics.Debug.Assert(sysXmlSchema != null && sysXmlSchema.Items.Count != 0);

				sysXmlSchema.Compile(new System.Xml.Schema.ValidationEventHandler(xmlValidatingReader_ValidationEventHandler));

				_xsdDocument  = new XsdDocument(sysXmlSchema);

				base.SetChildSchemaElements(sysXmlSchema.Elements.Values);

				_xmlDocument = new Puma.Xml.XmlDocument();

				XmlXsdNodeConstructor xmlXsdNodeConstructor = new XmlXsdNodeConstructor((System.Xml.Schema.XmlSchemaElement)sysXmlSchema.Items[0], this);
				
                base.Childs.AddXmlXsdNode(xmlXsdNodeConstructor);

				SetExtensiblity();
			}
            catch (System.Xml.Schema.XmlSchemaException e)
			{
                throw new Puma.Xml.XmlExceptions.ReadTimeXmlException(e);
			}

		}


		private void IniXmlXsdDocument(string XmlFileName, string XsdFileName, NodeExtensible NodeExtensible) 
		{
			try
			{
				_nodeExtensible = NodeExtensible;

				System.Xml.Schema.XmlSchemaCollection xmlSchemaCollection = new System.Xml.Schema.XmlSchemaCollection();

				xmlSchemaCollection.Add(null, XsdFileName);

				System.Xml.Schema.XmlSchemaCollectionEnumerator schemaEnumerator = xmlSchemaCollection.GetEnumerator();

				schemaEnumerator.MoveNext();

				_xsdDocument  = new XsdDocument(schemaEnumerator.Current);
			
				base.SetChildSchemaElements(schemaEnumerator.Current.Elements.Values);

				XmlValidatingReader xmlValidatingReader = new XmlValidatingReader(new System.Xml.XmlTextReader(XmlFileName), this);

				xmlValidatingReader.ValidationType = System.Xml.ValidationType.Schema;

				xmlValidatingReader.Schemas.Add(xmlSchemaCollection);

				xmlValidatingReader.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(xmlValidatingReader_ValidationEventHandler);

				_xmlDocument = new XmlDocument();

				_xmlDocument.Load(xmlValidatingReader);
			
				xmlValidatingReader.Close();

				SetExtensiblity();
			}
            catch (System.Xml.Schema.XmlSchemaException e)
			{
                throw new Puma.Xml.XmlExceptions.ReadTimeXmlException(e);
			}
		}

		private void SetExtensiblity()
		{
			if (NodeExtensible != null)
			{
				_xsdDocument.SetTagEx(NodeExtensible.OnXsdDocumentCreated(_xsdDocument));
				_xmlDocument.SetTagEx(NodeExtensible.OnXmlDocumentCreated(_xmlDocument));
				this.SetTagEx(NodeExtensible.OnXmlXsdDocumentCreated(this));
			}
		}

		public Puma.Xml.XmlDocument XmlDocument
		{
			get
			{
				return _xmlDocument;
			}
		}

		public Puma.Xml.XsdDocument XsdDocument
		{
			get
			{
				return _xsdDocument;
			}
		}

        public void SaveBin(string FileName)
        {
            BinaryWriter.Save(FileName, this, null);
        }

		public void Save(string FileName)
		{
			_xmlDocument.Save(FileName);
		}

		public void Save()
		{
			_xmlDocument.Save();
		}

		private void xmlValidatingReader_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e)
		{
			throw new Puma.Xml.XmlExceptions.ReadTimeXmlException(e.Exception);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions