473,320 Members | 1,884 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to read a xml file and write it to a text file

If I have the below code in the books.xml.

I would like to know how to read the xml file and write it into a html file in the table format.

Please help as I am new in c# coding.
Expand|Select|Wrap|Line Numbers
  1. <book>
  2. <person>
  3.   <first>Kiran</first>
  4.   <last>Pai</last>
  5.   <age>22</age>
  6. </person>
  7. <person>
  8.   <first>Bill</first>
  9.   <last>Gates</last>
  10.   <age>46</age>
  11. </person>
  12. <person>
  13.   <first>Steve</first>
  14.   <last>Jobs</last>
  15.   <age>40</age>
  16. </person>
  17. </book>
Oct 15 '10 #1
5 2274
Frinavale
9,735 Expert Mod 8TB
You could consider using LINQ to XML.

Or you could consider using the classes in the XML Namespace.


This is an example of how to use the XMLTextReader to read XML (retrieved from a url or path), fill a DataSet based on the XML...and display it in a Repeater (where you can specify the HTML template used to display the data...so in your case you would specify the template for a Table)

(C#)
Expand|Select|Wrap|Line Numbers
  1.         System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(Server.MapPath("~/testData.xml"));
  2.         DataSet ds = new DataSet();
  3.         ds.ReadXml(reader);
  4.         myXMLRepeater.DataSource = ds.Tables(0);
  5.         myXMLRepeater.DataBind();
  6.  

(VB.NET)
Expand|Select|Wrap|Line Numbers
  1.         Dim reader As New System.Xml.XmlTextReader(Server.MapPath("~/testData.xml"))
  2.         Dim ds As New DataSet()
  3.         ds.ReadXml(reader)
  4.         myXMLRepeater.DataSource = ds.Tables(0)
  5.         myXMLRepeater.DataBind()
  6.  
The "myXMLRepeater" would look something like:
Expand|Select|Wrap|Line Numbers
  1.  <asp:Repeater ID="myXMLRepeater" runat="server">
  2.     <HeaderTemplate>
  3.       <table>
  4.         <tr>
  5.           <th>first</th>
  6.           <th>last</th>
  7.           <th>age</th>
  8.         </tr>
  9.     </HeaderTemplate>
  10.     <ItemTemplate>
  11.         <tr>
  12.          <td><asp:Localize ID="first" runat="server" Text='<%# Eval("first") %>' ></asp:Localize></td>
  13.          <td><asp:Localize ID="last" runat="server" Text='<%# Eval("last") %>' ></asp:Localize></td>
  14.          <td><asp:Localize ID="age" runat="server" Text='<%# Eval("age") %>' ></asp:Localize></td>
  15.         </tr>
  16.     </ItemTemplate>
  17.     <FooterTemplate>
  18.       </table>
  19.     </FooterTemplate>
  20.   </asp:Repeater>

-Frinny
Oct 15 '10 #2
Hi,

This was awesome...but can you please explain me little more...where do I add this code???

Expand|Select|Wrap|Line Numbers
  1. System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(Server.MapPath("~/testData.xml"));
  2.         DataSet ds = new DataSet();
  3.         ds.ReadXml(reader);
  4.         myXMLRepeater.DataSource = ds.Tables(0);
  5.         myXMLRepeater.DataBind();
  6.  
and where do I add this code???

Expand|Select|Wrap|Line Numbers
  1. <asp:Repeater ID="myXMLRepeater" runat="server">
  2.     <HeaderTemplate>
  3.       <table>
  4.         <tr>
  5.           <th>first</th>
  6.           <th>last</th>
  7.           <th>age</th>
  8.         </tr>
  9.     </HeaderTemplate>
  10.     <ItemTemplate>
  11.         <tr>
  12.          <td><asp:Localize ID="first" runat="server" Text='<%# Eval("first") %>' ></asp:Localize></td>
  13.          <td><asp:Localize ID="last" runat="server" Text='<%# Eval("last") %>' ></asp:Localize></td>
  14.          <td><asp:Localize ID="age" runat="server" Text='<%# Eval("age") %>' ></asp:Localize></td>
  15.         </tr>
  16.     </ItemTemplate>
  17.     <FooterTemplate>
  18.       </table>
  19.     </FooterTemplate>
  20.   </asp:Repeater>
  21.  
I am sorry I am a beginner of this .net, so can you please explain me little more...that will be very helpful...
Oct 18 '10 #3
Frinavale
9,735 Expert Mod 8TB
You are developing an ASP.NET application right??
Oct 19 '10 #4
sigo
2
Here this open an xml file and output it to an html file as an html table

Expand|Select|Wrap|Line Numbers
  1. var sb = new StringBuilder();
  2.  
  3.             var doc = XDocument.Load("Book.xml");
  4.  
  5.             sb.AppendLine("<table>");
  6.             sb.AppendLine("\t<tbody>");
  7.             var persons = from p in doc.Element("book").Elements("person")
  8.                           select sb.AppendLine("\t\t<tr>" + 
  9.                                               "<td>" + p.Element("first").Value + "</td>" +
  10.                                               "<td>" + p.Element("last").Value + "</td>" +
  11.                                               "<td>" + p.Element("age").Value + "</td>"
  12.                                            + "</tr>");
  13.  
  14.             persons.Count();
  15.             sb.AppendLine("\t</tbody>");
  16.             sb.AppendLine("</table>");
  17.  
  18.             var output = File.CreateText("out.html");
  19.             output.Write(sb.ToString());
  20.             output.Close();
You will get the output

<table>
<tbody>
<tr><td>Kiran</td><td>Pai</td><td>22</td></tr>
<tr><td>Bill</td><td>Gates</td><td>46</td></tr>
<tr><td>Steve</td><td>Jobs</td><td>40</td></tr>
</tbody>
</table>


I think an other (better?) solution would be to create an XSLT an call it from c# code using XslCompiledTransform class
Oct 20 '10 #5
sigo
2
Here a solution using xslt

The xslt sheet:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3.     xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
  4. >
  5.   <xsl:output omit-xml-declaration="yes" method="xml" indent="yes"/>
  6.  
  7.   <xsl:template match="book">
  8.     <table>
  9.       <tbody>
  10.         <xsl:apply-templates select="person"/>
  11.       </tbody>
  12.     </table>
  13.   </xsl:template>
  14.  
  15.   <xsl:template match="first|last|age">
  16.     <td>
  17.       <xsl:value-of select="." />
  18.     </td>
  19.   </xsl:template>
  20.  
  21.   <xsl:template match="person">
  22.     <tr>
  23.       <xsl:apply-templates select="first"/>
  24.       <xsl:apply-templates select="last"/>
  25.       <xsl:apply-templates select="age"/>
  26.     </tr>
  27.   </xsl:template>
  28.  
  29. </xsl:stylesheet>
And the c# code

Expand|Select|Wrap|Line Numbers
  1. XslCompiledTransform xslt = new XslCompiledTransform();
  2. xslt.Load("Book.xslt");
  3. xslt.Transform("Book.xml", "out.html");
But im not sure this solution performs better :p but you get rid of all ugly string concatenation in the c#, so pretty useful if you need to generate complex html with styles/css etc in your table
Oct 20 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data into an array for some manipulation then reading...
40
by: googler | last post by:
I'm trying to read from an input text file and print it out. I can do this by reading each character, but I want to implement it in a more efficient way. So I thought my program should read one...
11
by: Michael McGarry | last post by:
Hi, I am reading strings from a text file using fscanf("%s", currToken); This returns strings delimited by whitespace. How can I tell if the string was followed by a carriage return in the...
2
by: SteMc | last post by:
today I tackled, for the first time, opening and reading from a text file. Following the example on the MSDN and declared a variable, strline as a string and objstreamreader as a streamreader. ...
2
by: bienwell | last post by:
Hi all, Do you have any source code to import data from Excel file or text file into database in ASP.NET program ? Please give me your reference if you have. Thanks in advance
8
by: pappu | last post by:
Hello friends, I want to convert .xls file into text file.So can anybody pls help me out. Thxxxxxx, Sachin.
3
by: manigattani | last post by:
Please let me know of some java code for converting dbf file to text file
1
by: LakshmiE | last post by:
Hi, Can any one help me by providing me the VB code to convert binary file to text file. The extension of the binary file i have is ".ST5". This is not in the readable format. i want to read...
3
by: minhtran | last post by:
Hi all Anyone has any idea how to get a Excel header column in VB.NET. Please, help me. Thank you so much in advance
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.