By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

Bind Datagrid to an xml string (not file)

P: n/a
Can I bind datagrid to an xml string (not to file)? For example, the result of transformation?
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Mark,

No problem doing what you want to do. Here's one way, there are probably
many others.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Ken Cox - Microsoft MVP [ASP.NET] Feb 13/04
' Create a stringbuilder to hold the XML
Dim sb As New System.Text.StringBuilder
' Create a dataset to hold the data
Dim ds As New DataSet
' create a TextReader to get the XML
Dim xmlrder As System.Xml.XmlTextReader
' Build some sample XML data (must be well-formed)
sb.Append("<?xml version=""1.0"" standalone=""yes""?>")
sb.Append("<CanadaPostalCodes>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1A0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AQUAFORTE
PO</DeliveryInstallation>")
sb.Append("<Key>1</Key>")
sb.Append("</Postal_Code>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1B0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AVONDALE
PO</DeliveryInstallation>")
sb.Append("<Key>2</Key>")
sb.Append("</Postal_Code>")
sb.Append("</CanadaPostalCodes>")
' Point the xmltextreader to the string full of xml and
' tell it to expect an XML document
xmlrder = New System.Xml.XmlTextReader _
(sb.ToString, System.Xml.XmlNodeType.Document, Nothing)
' Read all of the XML as one chunk
xmlrder.ReadOuterXml()
' Read the outer XML into the Dataset
ds.ReadXml(xmlrder)
' Tell the datagrid to use this dataset
DataGrid1.DataSource = ds
' Bind the data
DataGrid1.DataBind()
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]

"Mark" <an*******@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
Can I bind datagrid to an xml string (not to file)? For example, the
result of transformation?


Nov 18 '05 #2

P: n/a
But how about to bind to a complete xml string (just like you would do with
the file) but without converting the string into a file first.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:O#**************@TK2MSFTNGP10.phx.gbl...
Hi Mark,

No problem doing what you want to do. Here's one way, there are probably
many others.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Ken Cox - Microsoft MVP [ASP.NET] Feb 13/04
' Create a stringbuilder to hold the XML
Dim sb As New System.Text.StringBuilder
' Create a dataset to hold the data
Dim ds As New DataSet
' create a TextReader to get the XML
Dim xmlrder As System.Xml.XmlTextReader
' Build some sample XML data (must be well-formed)
sb.Append("<?xml version=""1.0"" standalone=""yes""?>")
sb.Append("<CanadaPostalCodes>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1A0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AQUAFORTE
PO</DeliveryInstallation>")
sb.Append("<Key>1</Key>")
sb.Append("</Postal_Code>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1B0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AVONDALE
PO</DeliveryInstallation>")
sb.Append("<Key>2</Key>")
sb.Append("</Postal_Code>")
sb.Append("</CanadaPostalCodes>")
' Point the xmltextreader to the string full of xml and
' tell it to expect an XML document
xmlrder = New System.Xml.XmlTextReader _
(sb.ToString, System.Xml.XmlNodeType.Document, Nothing)
' Read all of the XML as one chunk
xmlrder.ReadOuterXml()
' Read the outer XML into the Dataset
ds.ReadXml(xmlrder)
' Tell the datagrid to use this dataset
DataGrid1.DataSource = ds
' Bind the data
DataGrid1.DataBind()
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]

"Mark" <an*******@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
Can I bind datagrid to an xml string (not to file)? For example, the
result of transformation?

Nov 18 '05 #3

P: n/a
Mark,
Ken's example is not converting xml to file in the example he provided.
Please look at the code again.
Hope this helps.
Marshal Antony
..NET Developer
http://www.dotnetmarshal.com

"Mark Goldin" <ma********@comcast.net> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
But how about to bind to a complete xml string (just like you would do with the file) but without converting the string into a file first.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:O#**************@TK2MSFTNGP10.phx.gbl...
Hi Mark,

No problem doing what you want to do. Here's one way, there are probably
many others.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Ken Cox - Microsoft MVP [ASP.NET] Feb 13/04
' Create a stringbuilder to hold the XML
Dim sb As New System.Text.StringBuilder
' Create a dataset to hold the data
Dim ds As New DataSet
' create a TextReader to get the XML
Dim xmlrder As System.Xml.XmlTextReader
' Build some sample XML data (must be well-formed)
sb.Append("<?xml version=""1.0"" standalone=""yes""?>")
sb.Append("<CanadaPostalCodes>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1A0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AQUAFORTE
PO</DeliveryInstallation>")
sb.Append("<Key>1</Key>")
sb.Append("</Postal_Code>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1B0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AVONDALE
PO</DeliveryInstallation>")
sb.Append("<Key>2</Key>")
sb.Append("</Postal_Code>")
sb.Append("</CanadaPostalCodes>")
' Point the xmltextreader to the string full of xml and
' tell it to expect an XML document
xmlrder = New System.Xml.XmlTextReader _
(sb.ToString, System.Xml.XmlNodeType.Document, Nothing)
' Read all of the XML as one chunk
xmlrder.ReadOuterXml()
' Read the outer XML into the Dataset
ds.ReadXml(xmlrder)
' Tell the datagrid to use this dataset
DataGrid1.DataSource = ds
' Bind the data
DataGrid1.DataBind()
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]

"Mark" <an*******@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
Can I bind datagrid to an xml string (not to file)? For example, the
result of transformation?


Nov 18 '05 #4

P: n/a
I'm only using a string of XML markup. There's no file involved.
"Mark Goldin" <ma********@comcast.net> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
But how about to bind to a complete xml string (just like you would do
with
the file) but without converting the string into a file first.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:O#**************@TK2MSFTNGP10.phx.gbl...
Hi Mark,

No problem doing what you want to do. Here's one way, there are probably
many others.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Ken Cox - Microsoft MVP [ASP.NET] Feb 13/04
' Create a stringbuilder to hold the XML
Dim sb As New System.Text.StringBuilder
' Create a dataset to hold the data
Dim ds As New DataSet
' create a TextReader to get the XML
Dim xmlrder As System.Xml.XmlTextReader
' Build some sample XML data (must be well-formed)
sb.Append("<?xml version=""1.0"" standalone=""yes""?>")
sb.Append("<CanadaPostalCodes>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1A0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AQUAFORTE
PO</DeliveryInstallation>")
sb.Append("<Key>1</Key>")
sb.Append("</Postal_Code>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1B0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AVONDALE
PO</DeliveryInstallation>")
sb.Append("<Key>2</Key>")
sb.Append("</Postal_Code>")
sb.Append("</CanadaPostalCodes>")
' Point the xmltextreader to the string full of xml and
' tell it to expect an XML document
xmlrder = New System.Xml.XmlTextReader _
(sb.ToString, System.Xml.XmlNodeType.Document, Nothing)
' Read all of the XML as one chunk
xmlrder.ReadOuterXml()
' Read the outer XML into the Dataset
ds.ReadXml(xmlrder)
' Tell the datagrid to use this dataset
DataGrid1.DataSource = ds
' Bind the data
DataGrid1.DataBind()
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]

"Mark" <an*******@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
> Can I bind datagrid to an xml string (not to file)? For example, the
> result of transformation?



Nov 18 '05 #5

P: n/a
Right, but you are appending every node manually.
That's is not what data binding does.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:O8**************@TK2MSFTNGP10.phx.gbl...
I'm only using a string of XML markup. There's no file involved.
"Mark Goldin" <ma********@comcast.net> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...
But how about to bind to a complete xml string (just like you would do
with
the file) but without converting the string into a file first.

"Ken Cox [Microsoft MVP]" <BA************@sympatico.ca> wrote in message
news:O#**************@TK2MSFTNGP10.phx.gbl...
Hi Mark,

No problem doing what you want to do. Here's one way, there are probably many others.

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
' Ken Cox - Microsoft MVP [ASP.NET] Feb 13/04
' Create a stringbuilder to hold the XML
Dim sb As New System.Text.StringBuilder
' Create a dataset to hold the data
Dim ds As New DataSet
' create a TextReader to get the XML
Dim xmlrder As System.Xml.XmlTextReader
' Build some sample XML data (must be well-formed)
sb.Append("<?xml version=""1.0"" standalone=""yes""?>")
sb.Append("<CanadaPostalCodes>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1A0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AQUAFORTE
PO</DeliveryInstallation>")
sb.Append("<Key>1</Key>")
sb.Append("</Postal_Code>")
sb.Append("<Postal_Code>")
sb.Append("<FSA>A0A</FSA>")
sb.Append("<LDA>1B0</LDA>")
sb.Append("<Province>NEWFOUNDLAND AND LABRADOR</Province>")
sb.Append("<Type>Rural</Type>")
sb.Append("<DeliveryInstallation>AVONDALE
PO</DeliveryInstallation>")
sb.Append("<Key>2</Key>")
sb.Append("</Postal_Code>")
sb.Append("</CanadaPostalCodes>")
' Point the xmltextreader to the string full of xml and
' tell it to expect an XML document
xmlrder = New System.Xml.XmlTextReader _
(sb.ToString, System.Xml.XmlNodeType.Document, Nothing)
' Read all of the XML as one chunk
xmlrder.ReadOuterXml()
' Read the outer XML into the Dataset
ds.ReadXml(xmlrder)
' Tell the datagrid to use this dataset
DataGrid1.DataSource = ds
' Bind the data
DataGrid1.DataBind()
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]

"Mark" <an*******@discussions.microsoft.com> wrote in message
news:28**********************************@microsof t.com...
> Can I bind datagrid to an xml string (not to file)? For example, the
> result of transformation?


Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.