Fixing XmlDataSourceView
I know Scott posted a "fix" for the XmlDataSourceView that simply strips all the namespaces out of the document with a transform. That seems pretty lame IMO, because if you are actually using the namespaces, this isn't really a fix and could lead to lots of bad side effects. So, I figured, there has to be a way to fix this. And here it is. It's not really all that tested, but it should do the trick. Feel free to use it and/or make it better or prettier (for example, you could implement all the members of XmlDataSource view--with the exception of the GetView ones of course--and have them wrap the provider's ones):
using System;using System.Reflection;using System.Xml;using System.Collections.Generic;using System.Text;using System.Web.UI;using System.Web.UI.WebControls;namespace MyNamespace{public class MyXmlDataSource : IDataSource
{ public MyXmlDataSource(XmlNamespaceManager namespaceManager) { _provider = new XmlDataSource(); ((IDataSource)_provider).DataSourceChanged += new EventHandler(MyXmlDataSource_DataSourceChanged);_manager = namespaceManager;
}
public MyXmlDataSource(XmlDataSource provider, XmlNamespaceManager namespaceManager) {_manager = namespaceManager;
_provider = provider;
((IDataSource)_provider).DataSourceChanged += new EventHandler(MyXmlDataSource_DataSourceChanged);}
void MyXmlDataSource_DataSourceChanged(object sender, EventArgs e)
{if (DataSourceChanged != null)
{ DataSourceChanged(this, e);}
}
#region IDataSource Memberspublic event EventHandler DataSourceChanged;
XmlNamespaceManager _manager;
XmlDataSource _provider;
public XmlDataSource Provider {get
{ return _provider;}
}
public DataSourceView GetView(string viewName)
{ if (viewName.Length == 0) { viewName = "DefaultView";}
return new MyXmlDataSourceView(_provider, viewName, _manager);
}
public System.Collections.ICollection GetViewNames() { return ((IDataSource)_provider).GetViewNames();}
#endregion}
public class MyXmlDataSourceView : DataSourceView
{public MyXmlDataSourceView(XmlDataSource source, string name, XmlNamespaceManager manager) : base(source,name)
{_source = source;
_xmlNamespaceManager = manager;
}
XmlDataSource _source;
XmlNamespaceManager _xmlNamespaceManager;
protected override System.Collections.IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
{ XmlNodeList list = null;if (this._source.XPath.Length != 0)
{list = _source.GetXmlDocument().SelectNodes(_source.XPath, _xmlNamespaceManager);
}
else { list = _source.GetXmlDocument().SelectNodes("/node()/node()");}
Type t = typeof(XmlDataSourceView); Type enumerationType = t.GetNestedType("XmlDataSourceNodeDescriptorEnumeration", BindingFlags.NonPublic); return (System.Collections.IEnumerable)Activator.CreateInstance(enumerationType, list);}
}
}