471,594 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

XmlDocument and DataBinding

Hi there,

Can someone explain to me why I can't bind to an XmlDocument but I can bind
to an XmlNodeList. It's my understanding that they both implement the
IEnumerable interface which is required for databinding. Am happy to work
with the XmlNodeList but I can't shake the feeling I'm missing something
<blush>

Thanks, Pete
Nov 12 '05 #1
8 6035


pete wrote:

Can someone explain to me why I can't bind to an XmlDocument but I can bind
to an XmlNodeList. It's my understanding that they both implement the
IEnumerable interface which is required for databinding. Am happy to work
with the XmlNodeList but I can't shake the feeling I'm missing something


Here are the docs:
http://msdn.microsoft.com/library/de...ClassTopic.asp
http://msdn.microsoft.com/library/de...ClassTopic.asp
there is nothing there indicating that XmlDocument implements the
IEnumerable interface so you can bind to it.


--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
It's my understanding that XmlDocument inherits its implementation of
IEnumerable interface from the XmlNode type. Here is the doc:
http://msdn.microsoft.com/library/de...classtopic.asp.
However, by looking at the documentation for the IEnumerable interface its
clear that XmlDocument isn't one of the classes that supports it.

I guess the question I'm asking is, if XmlNode is IEnumerable then why isn't
XmlDocument?

Cheers, Pete

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2******************@TK2MSFTNGP09.phx.gbl...


pete wrote:

Can someone explain to me why I can't bind to an XmlDocument but I can bind to an XmlNodeList. It's my understanding that they both implement the
IEnumerable interface which is required for databinding. Am happy to work with the XmlNodeList but I can't shake the feeling I'm missing something
Here are the docs:

http://msdn.microsoft.com/library/de...ClassTopic.asp http://msdn.microsoft.com/library/de...ClassTopic.asp there is nothing there indicating that XmlDocument implements the
IEnumerable interface so you can bind to it.


--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #3
To make you feel better, you're not missing anything ... Former MVP Kirk
Allen Evans posted the XmlNodeList approach a while ago [0] as the way
to bind data to ASP.NET controls -- and Kirk would know ;)

Alternatively, you could read the XML into a DataSet if your format
permits.

HTH,
Christoph Schittko
MS MVP XML
http://weblogs.asp.net/cschittko

[0] http://blogs.msdn.com/kaevans/archiv...7/04/9713.aspx
-----Original Message-----
From: pete [mailto:no*****@hotmail.com]
Posted At: Friday, November 26, 2004 11:59 AM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlDocument and DataBinding
Subject: XmlDocument and DataBinding

Hi there,

Can someone explain to me why I can't bind to an XmlDocument but I can
bind
to an XmlNodeList. It's my understanding that they both implement the
IEnumerable interface which is required for databinding. Am happy to work with the XmlNodeList but I can't shake the feeling I'm missing something <blush>

Thanks, Pete

Nov 12 '05 #4


pete wrote:
It's my understanding that XmlDocument inherits its implementation of
IEnumerable interface from the XmlNode type. Here is the doc:
http://msdn.microsoft.com/library/de...classtopic.asp.
However, by looking at the documentation for the IEnumerable interface its
clear that XmlDocument isn't one of the classes that supports it.

I guess the question I'm asking is, if XmlNode is IEnumerable then why isn't
XmlDocument?


You are right, as XmlNode implements IEnumerable XmlDocument inherits
that implementation. And thus XmlDocument can be bound as in the
following example:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<html>
<head>
<title>binding an XmlDocument to a DataList</title>
<script runat="server">
void Page_Load () {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.LoadXml(@"<?xml version=""1.0""?>
<!-- Kibology for all -->
<gods>
<god>Kibo</god>
<god>Xibo</god>
</gods>");
DataList1.DataSource = xmlDocument;
DataList1.DataBind();
}
</script>
</head>
<body>
<form runat="server">
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<%#((System.Xml.XmlNode)Container.DataItem).NodeTy pe%>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>

which then outputs

XmlDeclaration
Whitespace
Comment
Whitespace
Element

So what the enumerator for an XmlDocument does is enumerating the top
level child nodes and you can bind those if you are interested in them.
Only the data in an XML document is usually not contained directly in
top level child nodes of the document itself but in child nodes and
descendants of the document element and if you want to display that data
you need to first access the child nodes or descendants so that is why
you will probably end up using a node list returned by SelectNodes or
GetElementsByTagName.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #5
Thanks for the explanation martin. I didn't expect to be able to use the
IEnumerable interface of the XmlDocument in any sensible way but was curious
why I couldn't use it at all. In your example, I notice that you use
explicit casting in your databinding expression rather than
DataBinder.Eval(). Is there any reason for this?

Cheers, Pete

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...


pete wrote:
It's my understanding that XmlDocument inherits its implementation of
IEnumerable interface from the XmlNode type. Here is the doc:
http://msdn.microsoft.com/library/de...classtopic.asp. However, by looking at the documentation for the IEnumerable interface its clear that XmlDocument isn't one of the classes that supports it.

I guess the question I'm asking is, if XmlNode is IEnumerable then why isn't XmlDocument?


You are right, as XmlNode implements IEnumerable XmlDocument inherits
that implementation. And thus XmlDocument can be bound as in the
following example:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<html>
<head>
<title>binding an XmlDocument to a DataList</title>
<script runat="server">
void Page_Load () {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.LoadXml(@"<?xml version=""1.0""?>
<!-- Kibology for all -->
<gods>
<god>Kibo</god>
<god>Xibo</god>
</gods>");
DataList1.DataSource = xmlDocument;
DataList1.DataBind();
}
</script>
</head>
<body>
<form runat="server">
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<%#((System.Xml.XmlNode)Container.DataItem).NodeTy pe%>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>

which then outputs

XmlDeclaration
Whitespace
Comment
Whitespace
Element

So what the enumerator for an XmlDocument does is enumerating the top
level child nodes and you can bind those if you are interested in them.
Only the data in an XML document is usually not contained directly in
top level child nodes of the document itself but in child nodes and
descendants of the document element and if you want to display that data
you need to first access the child nodes or descendants so that is why
you will probably end up using a node list returned by SelectNodes or
GetElementsByTagName.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #6


pete wrote:

In your example, I notice that you use
explicit casting in your databinding expression rather than
DataBinder.Eval(). Is there any reason for this?


No, no particular reason, certainly none related to XmlDocument, I was
just adapting an example using that syntax.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #7
Pete,

It might also be interesting to you that Visual Studio 2005 ships with
an XmlDataSource object to bind XML data to web server controls [0].
Note that Microsoft did not make the XmlDocument itself bindable but
followed the general provider pattern in ASP.NET 2.0 to enable binding
of XML data.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko

[0] http://msdn2.microsoft.com/library/e8d8587a.aspx
-----Original Message-----
From: pete [mailto:no*****@hotmail.com]
Posted At: Saturday, November 27, 2004 7:16 PM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlDocument and DataBinding
Subject: Re: XmlDocument and DataBinding

Thanks for the explanation martin. I didn't expect to be able to use the IEnumerable interface of the XmlDocument in any sensible way but was
curious
why I couldn't use it at all. In your example, I notice that you use
explicit casting in your databinding expression rather than
DataBinder.Eval(). Is there any reason for this?

Cheers, Pete

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...


pete wrote:
It's my understanding that XmlDocument inherits its implementation of IEnumerable interface from the XmlNode type. Here is the doc:
http://msdn.microsoft.com/library/de...l=/library/en-
us/cpref/html/frlrfsystemxmlxmlnodeclasstopic.asp. However, by looking at the documentation for the IEnumerable
interface
its clear that XmlDocument isn't one of the classes that supports it.

I guess the question I'm asking is, if XmlNode is IEnumerable then
why
isn't XmlDocument?


You are right, as XmlNode implements IEnumerable XmlDocument inherits that implementation. And thus XmlDocument can be bound as in the
following example:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<html>
<head>
<title>binding an XmlDocument to a DataList</title>
<script runat="server">
void Page_Load () {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.LoadXml(@"<?xml version=""1.0""?>
<!-- Kibology for all -->
<gods>
<god>Kibo</god>
<god>Xibo</god>
</gods>");
DataList1.DataSource = xmlDocument;
DataList1.DataBind();
}
</script>
</head>
<body>
<form runat="server">
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<%#((System.Xml.XmlNode)Container.DataItem).NodeTy pe%>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>

which then outputs

XmlDeclaration
Whitespace
Comment
Whitespace
Element

So what the enumerator for an XmlDocument does is enumerating the top level child nodes and you can bind those if you are interested in them. Only the data in an XML document is usually not contained directly in top level child nodes of the document itself but in child nodes and
descendants of the document element and if you want to display that data you need to first access the child nodes or descendants so that is why you will probably end up using a node list returned by SelectNodes or GetElementsByTagName.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #8
Pete,

Thats certainly sounds a whole lot more flexible. I'll get chance to play
around with the beta one day ... happy grin ... one day

Peter
"Christoph Schittko [MVP]" <IN**********@austin.rr.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Pete,

It might also be interesting to you that Visual Studio 2005 ships with
an XmlDataSource object to bind XML data to web server controls [0].
Note that Microsoft did not make the XmlDocument itself bindable but
followed the general provider pattern in ASP.NET 2.0 to enable binding
of XML data.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko

[0] http://msdn2.microsoft.com/library/e8d8587a.aspx
-----Original Message-----
From: pete [mailto:no*****@hotmail.com]
Posted At: Saturday, November 27, 2004 7:16 PM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlDocument and DataBinding
Subject: Re: XmlDocument and DataBinding

Thanks for the explanation martin. I didn't expect to be able to use

the
IEnumerable interface of the XmlDocument in any sensible way but was
curious
why I couldn't use it at all. In your example, I notice that you use
explicit casting in your databinding expression rather than
DataBinder.Eval(). Is there any reason for this?

Cheers, Pete

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:uk**************@TK2MSFTNGP09.phx.gbl...


pete wrote:

> It's my understanding that XmlDocument inherits its implementation of > IEnumerable interface from the XmlNode type. Here is the doc:
>

http://msdn.microsoft.com/library/de...l=/library/en-
us/cpref/html/frlrfsystemxmlxmlnodeclasstopic.asp.
> However, by looking at the documentation for the IEnumerable interface
its
> clear that XmlDocument isn't one of the classes that supports it.
>
> I guess the question I'm asking is, if XmlNode is IEnumerable then

why
isn't
> XmlDocument?

You are right, as XmlNode implements IEnumerable XmlDocument

inherits that implementation. And thus XmlDocument can be bound as in the
following example:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<html>
<head>
<title>binding an XmlDocument to a DataList</title>
<script runat="server">
void Page_Load () {
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.PreserveWhitespace = true;
xmlDocument.LoadXml(@"<?xml version=""1.0""?>
<!-- Kibology for all -->
<gods>
<god>Kibo</god>
<god>Xibo</god>
</gods>");
DataList1.DataSource = xmlDocument;
DataList1.DataBind();
}
</script>
</head>
<body>
<form runat="server">
<asp:DataList id="DataList1" runat="server">
<ItemTemplate>
<%#((System.Xml.XmlNode)Container.DataItem).NodeTy pe%>
</ItemTemplate>
</asp:DataList>
</form>
</body>
</html>

which then outputs

XmlDeclaration
Whitespace
Comment
Whitespace
Element

So what the enumerator for an XmlDocument does is enumerating the top level child nodes and you can bind those if you are interested in them. Only the data in an XML document is usually not contained directly in top level child nodes of the document itself but in child nodes and
descendants of the document element and if you want to display that data you need to first access the child nodes or descendants so that is why you will probably end up using a node list returned by SelectNodes or GetElementsByTagName.

--

Martin Honnen
http://JavaScript.FAQTs.com/


Nov 12 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Martin Honnen | last post: by
2 posts views Thread by Graham Pengelly | last post: by
3 posts views Thread by Kevin Swanson | last post: by
5 posts views Thread by Rob Panosh | last post: by
8 posts views Thread by GaryDean | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.