all groups > dotnet xml > november 2004 >
You're in the

dotnet xml

group:

XmlDocument and DataBinding


Re: XmlDocument and DataBinding Christoph Schittko [MVP]
11/26/2004 3:04:59 PM
dotnet xml:
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/archive/2003/07/04/9713.aspx

[quoted text, click to view]

XmlDocument and DataBinding pete
11/26/2004 5:59:20 PM
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

Re: XmlDocument and DataBinding pete
11/26/2004 7:06:35 PM
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/default.asp?url=/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?

Cheers, Pete

[quoted text, click to view]

Re: XmlDocument and DataBinding Martin Honnen
11/26/2004 7:50:31 PM


[quoted text, click to view]


[quoted text, click to view]

Here are the docs:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlNodeListClassTopic.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemXmlXmlDocumentClassTopic.asp
there is nothing there indicating that XmlDocument implements the
IEnumerable interface so you can bind to it.




--

Martin Honnen
Re: XmlDocument and DataBinding Martin Honnen
11/27/2004 4:29:30 PM


[quoted text, click to view]

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).NodeType%>
</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
Re: XmlDocument and DataBinding pete
11/28/2004 1:16:16 AM
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

[quoted text, click to view]

Re: XmlDocument and DataBinding Christoph Schittko [MVP]
11/28/2004 10:30:38 AM
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

[quoted text, click to view]

Re: XmlDocument and DataBinding Martin Honnen
11/28/2004 1:26:17 PM


[quoted text, click to view]


[quoted text, click to view]

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

--

Martin Honnen
Re: XmlDocument and DataBinding pete
11/29/2004 6:48:26 PM
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


[quoted text, click to view]

AddThis Social Bookmark Button