Fixing XmlDataSourceView

Now that 2.0 is out... its about time to fix another MS class.

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 Members
 
 public 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);
 }
 }
}

2 Comments

  • I posted another different type of XmlDataSource enhancement on Product feedback about creating an XPathParameters property (ParameterCollection) and using a feature of the Mvp.Xml project to support $ parameters in the Xpath for an XmlDataSource. I wll post the link to the suggestion and workaround in a bit.

  • I posted another different type of XmlDataSource enhancement on Product feedback about creating an XPathParameters property (ParameterCollection) and using a feature of the Mvp.Xml project to support $ parameters in the Xpath for an XmlDataSource. I wll post the link to the suggestion and workaround in a bit.

Comments have been disabled for this content.