|
/*
* Copyright (c) 2004 Marc Clifton
* All Rights Reserved
*
* License: GNU General Public License, see doc\license.txt
* http://www.gnu.org/licenses/licenses.html#GPL
* Owner: Marc Clifton email: webmaster@knowledgeautomation.com
* First release published:
* http://www.codeproject.com/cs/miscctrl/xmlGuiGenerator.asp
* 2/25/04
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Xml;
namespace MyXaml.Extensions
{
[TypeConverter(typeof(XmlDataValueTypeConverter))]
public class XmlDataValue
{
protected string name;
protected XmlDataSource src;
protected string path;
public string Name
{
get {return name;}
set {name=value;}
}
public XmlDataSource Source
{
get {return src;}
set {src=value;}
}
public string Path
{
get {return path;}
set {path=value;}
}
public override string ToString()
{
string ret=String.Empty;
XmlNodeList nodes=src.Document.SelectNodes(src.XPath+"/"+path);
if (nodes != null)
{
if (nodes.Count==1)
{
ret=nodes[0].InnerText;
}
}
return ret;
}
}
public class XmlDataColumn
{
protected string columnName;
protected string xpath;
public String ColumnName
{
get {return columnName;}
set {columnName=value;}
}
public String XPath
{
get {return xpath;}
set {xpath=value;}
}
}
[TypeConverter(typeof(XmlDataSourceTypeConverter))]
public class XmlDataSource
{
protected string source;
protected string xpath;
protected string path;
protected XmlDocument document;
protected ArrayList columns;
public string Source
{
get {return source;}
set
{
source=value;
document=new XmlDocument();
try
{
document.Load(source);
}
catch(XmlException e)
{
Trace.WriteLine(e.Message);
}
}
}
public string XPath
{
get {return xpath;}
set {xpath=value;}
}
public string Path
{
get {return path;}
set {path=value;}
}
public ArrayList XmlDataColumns
{
get {return columns;}
}
public XmlDocument Document
{
get {return document;}
}
public XmlDataSource()
{
columns=new ArrayList();
}
public override string ToString()
{
string ret=String.Empty;
XmlNodeList nodes;
try
{
nodes=document.SelectNodes(xpath+"/"+path);
if (nodes.Count==1)
{
ret=nodes[0].InnerText;
}
else if (nodes.Count==0)
{
ret="zero row return";
}
else
{
ret="multiple row return";
}
}
catch(Exception e)
{
Trace.WriteLine(e.Message);
}
return ret;
}
public DataTable LoadDataTable()
{
DataTable dt=new DataTable();
XmlNodeList nodes;
try
{
nodes=document.SelectNodes(xpath+"/"+path);
foreach(XmlDataColumn xdc in columns)
{
dt.Columns.Add(new DataColumn(xdc.ColumnName));
}
XmlDocument doc=new XmlDocument();
foreach(XmlNode n in nodes)
{
string xml=n.OuterXml;
doc.LoadXml(xml);
DataRow dr=dt.NewRow();
foreach(XmlDataColumn xdc in columns)
{
try
{
XmlNodeList nodeList=doc.SelectNodes(xdc.XPath);
dr[xdc.ColumnName]=nodeList[0].InnerText;
}
catch(Exception e)
{
Trace.WriteLine(e.Message+"\r\nxml="+xml+"\r\nColumnName="+xdc.ColumnName);
}
}
dt.Rows.Add(dr);
}
}
catch(Exception e)
{
Trace.WriteLine(e.Message);
}
return dt;
}
}
public class XmlDataValueTypeConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type t)
{
if (t==typeof(String)) return true;
if (t==typeof(decimal)) return true;
if (t==typeof(int)) return true;
return false;
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object val, Type destinationType)
{
if (destinationType==typeof(String))
{
return ((XmlDataValue)val).ToString();
}
if (destinationType==typeof(decimal))
{
return Convert.ToDecimal(((XmlDataValue)val).ToString());
}
if (destinationType==typeof(int))
{
return Convert.ToInt32(((XmlDataValue)val).ToString());
}
return String.Empty;
}
}
public class XmlDataSourceTypeConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type t)
{
// if (t.FullName=="System.Object") return true;
if (t==typeof(decimal)) return true;
if (t==typeof(string)) return true;
if (t==typeof(DataTable)) return true;
return false;
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object val, Type destinationType)
{
object ret=null;
XmlDataSource xds=(XmlDataSource)val;
if (destinationType==typeof(DataTable))
{
ret=xds.LoadDataTable();
}
else if (destinationType==typeof(string))
{
ret=xds.ToString();
}
else if (destinationType==typeof(decimal))
{
ret=Convert.ToDecimal(xds.ToString());
}
return ret;
}
}
}
|
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.