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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jack baver

    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.
    Code:
    <book>
    <person>
      <first>Kiran</first>
      <last>Pai</last>
      <age>22</age>
    </person>
    <person>
      <first>Bill</first>
      <last>Gates</last>
      <age>46</age>
    </person>
    <person>
      <first>Steve</first>
      <last>Jobs</last>
      <age>40</age>
    </person>
    </book>
    Last edited by Frinavale; Oct 15 '10, 07:58 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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#)
    Code:
            System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(Server.MapPath("~/testData.xml"));
            DataSet ds = new DataSet();
            ds.ReadXml(reader);
            myXMLRepeater.DataSource = ds.Tables(0);
            myXMLRepeater.DataBind();

    (VB.NET)
    Code:
            Dim reader As New System.Xml.XmlTextReader(Server.MapPath("~/testData.xml"))
            Dim ds As New DataSet()
            ds.ReadXml(reader)
            myXMLRepeater.DataSource = ds.Tables(0)
            myXMLRepeater.DataBind()
    The "myXMLRepea ter" would look something like:
    Code:
     <asp:Repeater ID="myXMLRepeater" runat="server">
        <HeaderTemplate>
          <table>
            <tr>
              <th>first</th>
              <th>last</th>
              <th>age</th>
            </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
             <td><asp:Localize ID="first" runat="server" Text='<%# Eval("first") %>' ></asp:Localize></td>
             <td><asp:Localize ID="last" runat="server" Text='<%# Eval("last") %>' ></asp:Localize></td>
             <td><asp:Localize ID="age" runat="server" Text='<%# Eval("age") %>' ></asp:Localize></td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
          </table>
        </FooterTemplate>
      </asp:Repeater>

    -Frinny
    Last edited by Frinavale; Oct 19 '10, 01:42 PM. Reason: Forgot the C# code..

    Comment

    • Jack Baver

      #3
      Hi,

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

      Code:
      System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(Server.MapPath("~/testData.xml"));
              DataSet ds = new DataSet();
              ds.ReadXml(reader);
              myXMLRepeater.DataSource = ds.Tables(0);
              myXMLRepeater.DataBind();
      and where do I add this code???

      Code:
      <asp:Repeater ID="myXMLRepeater" runat="server">
          <HeaderTemplate>
            <table>
              <tr>
                <th>first</th>
                <th>last</th>
                <th>age</th>
              </tr>
          </HeaderTemplate>
          <ItemTemplate>
              <tr>
               <td><asp:Localize ID="first" runat="server" Text='<%# Eval("first") %>' ></asp:Localize></td>
               <td><asp:Localize ID="last" runat="server" Text='<%# Eval("last") %>' ></asp:Localize></td>
               <td><asp:Localize ID="age" runat="server" Text='<%# Eval("age") %>' ></asp:Localize></td>
              </tr>
          </ItemTemplate>
          <FooterTemplate>
            </table>
          </FooterTemplate>
        </asp:Repeater>
      I am sorry I am a beginner of this .net, so can you please explain me little more...that will be very helpful...
      Last edited by MMcCarthy; Oct 18 '10, 08:11 PM. Reason: added code tags

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        You are developing an ASP.NET application right??

        Comment

        • sigo
          New Member
          • Oct 2010
          • 2

          #5
          Here this open an xml file and output it to an html file as an html table

          Code:
          var sb = new StringBuilder();
                      
                      var doc = XDocument.Load("Book.xml");
          
                      sb.AppendLine("<table>");
                      sb.AppendLine("\t<tbody>");
                      var persons = from p in doc.Element("book").Elements("person")
                                    select sb.AppendLine("\t\t<tr>" + 
                                                        "<td>" + p.Element("first").Value + "</td>" +
                                                        "<td>" + p.Element("last").Value + "</td>" +
                                                        "<td>" + p.Element("age").Value + "</td>"
                                                     + "</tr>");
          
                      persons.Count();
                      sb.AppendLine("\t</tbody>");
                      sb.AppendLine("</table>");
          
                      var output = File.CreateText("out.html");
                      output.Write(sb.ToString());
                      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 XslCompiledTran sform class

          Comment

          • sigo
            New Member
            • Oct 2010
            • 2

            #6
            Here a solution using xslt

            The xslt sheet:
            Code:
            <?xml version="1.0" encoding="utf-8"?>
            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
            >
              <xsl:output omit-xml-declaration="yes" method="xml" indent="yes"/>
            
              <xsl:template match="book">
                <table>
                  <tbody>
                    <xsl:apply-templates select="person"/>
                  </tbody>
                </table>
              </xsl:template>
            
              <xsl:template match="first|last|age">
                <td>
                  <xsl:value-of select="." />
                </td>
              </xsl:template>
            
              <xsl:template match="person">
                <tr>
                  <xsl:apply-templates select="first"/>
                  <xsl:apply-templates select="last"/>
                  <xsl:apply-templates select="age"/>
                </tr>
              </xsl:template>
            
            </xsl:stylesheet>
            And the c# code

            Code:
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load("Book.xslt");
            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

            Comment

            Working...