Bind Datagrid to an xml string (not file)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Can I bind datagrid to an xml string (not to file)? For example, the result of transformation?
 
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]
 
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 said:
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 said:
Can I bind datagrid to an xml string (not to file)? For example, the
result of transformation?
 
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 said:
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 said:
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 said:
Can I bind datagrid to an xml string (not to file)? For example, the
result of transformation?
 
I'm only using a string of XML markup. There's no file involved.


Mark Goldin said:
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 said:
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 said:
Can I bind datagrid to an xml string (not to file)? For example, the
result of transformation?
 
Right, but you are appending every node manually.
That's is not what data binding does.

Ken Cox said:
I'm only using a string of XML markup. There's no file involved.


Mark Goldin said:
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 said:
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]

Can I bind datagrid to an xml string (not to file)? For example, the
result of transformation?
 
Back
Top