0% found this document useful (0 votes)
135 views

12.1 How To Write The Data From Database Into An XML File?

The document discusses several ways to work with XML data in .NET: 1) Write data from a database to an XML file using DataSet.WriteXml. 2) Read data from an XML file and display it in a DataGrid by loading the file into a DataSet using DataSet.ReadXml. 3) Similarly load an XML file into a DataSet using FileStream and DataSet.ReadXml to display the data. 4) Save an XML string to an XML document file using XmlDocument, XmlTextWriter, and XmlDocument.Save.

Uploaded by

api-3841500
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
135 views

12.1 How To Write The Data From Database Into An XML File?

The document discusses several ways to work with XML data in .NET: 1) Write data from a database to an XML file using DataSet.WriteXml. 2) Read data from an XML file and display it in a DataGrid by loading the file into a DataSet using DataSet.ReadXml. 3) Similarly load an XML file into a DataSet using FileStream and DataSet.ReadXml to display the data. 4) Save an XML string to an XML document file using XmlDocument, XmlTextWriter, and XmlDocument.Save.

Uploaded by

api-3841500
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

XML

12.1 How to write the data from database into an XML file?

VB.NET

'Fill the DataSet


ds.WriteXml(Server.MapPath ("products.xml" ) )

C#

//Fill the DataSet


ds.WriteXml(Server.MapPath ("products.xml" ) );

Note : Make sure you have write and modify rights.

12.2 How to read data from an XML file and display it in a DataGrid?

VB.NET

dim ds as new DataSet()


ds.ReadXml (Server.MapPath ("products.xml"))
DataGrid1.DataSource =ds
DataGrid1.DataBind()

C#

DataSet ds= new DataSet ();


ds.ReadXml (Server.MapPath ("products.xml"));
DataGrid1.DataSource =ds;
DataGrid1.DataBind();

12.3 How to read data from the XML file using FileStream and display it
in a DataGrid?

Use namespace System.IO

VB.NET

dim ds as new DataSet()


dim fs as FileStream = new FileStream (Server.MapPath ("products.xml"),FileMode.Open , FileAccess.Read )
ds.ReadXml (fs)
DataGrid1.DataSource = ds
DataGrid1.DataBind ()

C#

DataSet ds= new DataSet ();


FileStream fs = new FileStream (Server.MapPath ("products.xml"),FileMode.Open , FileAccess.Read );
ds.ReadXml (fs);
DataGrid1.DataSource = ds;
DataGrid1.DataBind ();

12.4 How to save an xml-string into an Xml document?

VB.NET

Dim xmlText As String = "<?xml version=""1.0""?> Node1 Node2 "


Dim xmlDoc As New XmlDocument
xmlDoc.LoadXml(xmlText)
Dim writer As XmlTextWriter = New XmlTextWriter(Server.MapPath("data.xml"), Nothing)
writer.Formatting = Formatting.Indented
xmlDoc.Save(writer)

C#

string xmlText = "<?xml version=\"1.0\"?> Node1 Node2 ";


XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlText);
XmlTextWriter writer = new XmlTextWriter(Server.MapPath("data.xml"),null);
writer.Formatting = Formatting.Indented;
xmlDoc.Save(writer);

12.5 How to use the <asp:xml> tag?

<asp:Xml id="Xml1" DocumentSource="products.xml" runat="server"></asp:Xml>

12.6 How to display the Attribute values in an XML Document in a


DataGrid?
products.xml

<?xml version="1.0" standalone="yes"?>


<NewDataSet>
<Product>
<ProductID pcode="P2">2</ProductID>
<ProductName>Chang</ProductName>
<SupplierID>1</SupplierID>
<CategoryID>1</CategoryID>
<QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit>
</Product>
<Product>
<ProductID pcode="P4">3</ProductID>
<ProductName>Aniseed Syrup</ProductName>
<SupplierID>1</SupplierID>
<CategoryID>2</CategoryID>
<QuantityPerUnit>12 - 550 ml bottles</QuantityPerUnit>
</Product>
</NewDataSet>

Use Namespace System.Xml

VB.NET

<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">


<Columns>
<asp:TemplateColumn HeaderText="ProductCode">
<ItemTemplate>
<%#CType(Container.DataItem, System.Xml.XmlNode).Attributes("pcode").value%>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

dim xmldoc as XmlDocument = new XmlDocument()


xmldoc.Load(Server.MapPath("products.xml"))
dim xmlnodes as XmlNodeList = xmldoc.SelectNodes("NewDataSet/Product/ProductID")
DataGrid1.DataSource = xmlnodes
DataGrid1.DataBind ()

C#

<asp:DataGrid id="DataGrid1" AutoGenerateColumns="False" runat="server">


<Columns>
<asp:TemplateColumn HeaderText ="ProductCode">
<ItemTemplate>
<%# ((System.Xml.XmlNode)Container.DataItem).Attributes["pcode"].Value %>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>

XmlDocument xmldoc = new XmlDocument();


xmldoc.Load(Server.MapPath("products.xml"));
XmlNodeList xmlnodes = xmldoc.SelectNodes("NewDataSet/Product/ProductID");
DataGrid1.DataSource = xmlnodes;
DataGrid1.DataBind ();

You might also like