Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

WSDL and Schema Parser

, 25 Nov 2005
Modeling WSDL and XML schema on a TreeView control and generating XML document from XML schemas.
//Author contact: Thanh.Dao@gmx.netusing System;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;

namespace WSDLParser
{
	/// <summary>
	/// Summary description for XMLGenerator.
	/// </summary>
	public class XMLGenerator
	{
		public XMLGenerator()
		{
		}

		public static string GetXmlSample(TreeNode node)
		{
			StringWriter sw=new StringWriter ();
			XmlTextWriter writer=new XmlTextWriter (sw);
			writer.Formatting=Formatting.Indented;
			writer.WriteStartDocument ();					
			prefIndex=-1;
			WriteNode(writer, node);
			
			writer.Close() ;
			
			return sw.ToString() ;
		}

		static int prefIndex=0;

		private static  void WriteNode(XmlTextWriter writer, TreeNode node)
		{
			SchemaParser.NodeData data=null;
			if (node.Tag != null)
			{
				data=node.Tag as SchemaParser.NodeData ;
			}
			
			if (data == null) return;			
			
			if (data.baseObj is XmlSchemaElement
				|| data.baseObj is XmlSchemaComplexType
				|| data.baseObj is XmlSchemaSimpleType
				)
			{				
				
				
				

				//if (pref != null)  pref + ":" + node.Text;
				string name=node.Text;
				if (name.LastIndexOf(":") != -1)
					name=name.Substring(name.LastIndexOf(":") + 1, name.Length - name.LastIndexOf(":") - 1) ;

				if (data.Namespace != string.Empty )
				{
					++prefIndex;
					string pref=writer.LookupPrefix (data.Namespace);
					
					if (pref == null) pref="m"+prefIndex;

					writer.WriteStartElement(pref, name, data.Namespace) ;					
				}
				else
					writer.WriteStartElement(name) ;
				
				bool ct=false;
				if (node.Nodes.Count > 0)
				{
					foreach (TreeNode child in node.Nodes)
					{						
						if (child.Tag is SchemaParser.NodeData)
						{
							if (((SchemaParser.NodeData)child.Tag).baseObj is XmlSchemaAttribute)
								writer.WriteAttributeString(child.Text, ((SchemaParser.NodeData)child.Tag).Type );
						}						
					}

					foreach (TreeNode child in node.Nodes)
					if (child.Tag is SchemaParser.NodeData)
					if (!(((SchemaParser.NodeData)child.Tag).baseObj is XmlSchemaAttribute))
					{
						ct=true;
						WriteNode(writer, child);
					}
				};
				
				if (!ct)writer.WriteString (data.Type);

				writer.WriteEndElement();
				
			} 
		}
	}
}

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

Share

About the Author

Thanh Dao
Software Developer
Vietnam Vietnam
I'm still alive...but temporarily moved to work on mobile & web stuffs(j2me/brew/php/flash...something not M$). things have just been very busy, and probably will continue...so don't have chance to maintain & respond. Hope will have time to try to write again, because many ideas with WPF &silver light are waiting. wish me luck Smile | :)
 
FYI:
- MESHSimPack project(c# library for measuring similarity among concepts of the MESH ontology):
http://sourceforge.net/projects/meshsimpack.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 25 Nov 2005
Article Copyright 2005 by Thanh Dao
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid