Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

    Error: Twitter did not respond. Please wait a few minutes and refresh this page.

  • My footsteps

Archive for the ‘XML’ Category

Read,Write XML with .Net

Posted by Praveen Kumar on February 16, 2008

DVDlist.xml

<?xml version=”1.0″?><!–Commentss: XmlWriter Test Program–>
<DVDlist>
  <DVD ID=”1″>
    <Title>Tere Naam</Title>
    <Starring>
      <Actor>Salman Khan</Actor>
    </Starring>
  </DVD>
</DVDlist>

try.xml

<NewDataSet>

<Table>
  <user_id>1</user_id>
  <password>pop</password>
  </Table>
<Table>
  <user_id>2</user_id>
  <password>praveen</password>
  </Table>
  </NewDataSet>
library.xml
<?xml version=”1.0″ encoding=”ISO-8859-1″ ?>
<?xml-stylesheet type=”text/xsl” href=”sample.xsl” mce_href=”sample.xsl”?>
 <Library_menu>
<CS>
  <name>XML</name>
  <author>O’Reily</author>
  <price>$5.95</price>
</CS>
<CS>
  <name>OS</name>
  <author>Galiburg</author>
sample.xsl //this is the stylesheet for library.xml
<?xml version=”1.0″ encoding=”ISO-8859-1″ ?>
<html xsl:version=”1.0″ xmlns:xsl=”
http://www.w3.org/1999/XSL/Transform” xmlns=”http://www.w3.org/1999/xhtml“>
<body style=”font-family:Arial;font-size:12pt;background-color:#EEEEEE”>
<h2>MAHITI LIBRARY</h2>
    <table border=”1″>
    <tr bgcolor=”#9acd32″>
      <th align=”left”>Title</th>
      <th align=”left”>Author</th>
   <th align=”left”>Price</th>
    </tr>
    <xsl:for-each select=”Library_menu/CS”>
    <tr>
      <td bgcolor=”#f6f6f6″><xsl:value-of select=”name”/></td>
      <td><xsl:value-of select=”author”/></td>
   <td><xsl:value-of select=”price”/></td>
    </tr>
    </xsl:for-each>
    </table>
<xsl:for-each select=”Library_menu/CS”>
<div style=”background-color:teal;color:black;padding:4px”>
<span style=”font-weight:bold”>
  <xsl:value-of select=”name” />
  </span>
  <xsl:value-of select=”author” />
  </div>
 <div style=”margin-left:20px;margin-bottom:1em;font-size:10pt”>
  <xsl:value-of select=”price” />
  </div>
  </xsl:for-each>
  </body>
  </html>
C# code
try
{
OdbcConnection cn = new OdbcConnection(“DSN=test”);
OdbcDataAdapter cmd = new OdbcDataAdapter(“select *from usermaster”, cn);
DataSet ds = new DataSet();//this dataset to write XML from database
DataSet ds1 = new DataSet();//this dataset to read XML to datagridcmd.Fill(ds);//System.Xml.XmlDataDocument sourceXML = new System.Xml.XmlDataDocument();
string xmlFile = Server.MapPath(“DVDlist.xml”);
string df = Server.MapPath(“try.xml”);
string xslPath = Server.MapPath(“sample.xsl”);XslCompiledTransform transform = new XslCompiledTransform();transform.Load(xslPath);
//create a XML file is not exist
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(xmlFile, null);
System.Xml.XmlTextWriter wrt = new System.Xml.XmlTextWriter(df, null);
//System.Xml.XmlTextReader reader=new System.Xml.XmlTextReader(Server.MapPath(“library.xml”));
ds.WriteXml(wrt);
///////// write to XML(try.xml)file from database//starts a new documentwriter.WriteStartDocument();//write commentswriter.WriteComment(“Commentss: XmlWriter Test Program”);writer.Formatting = Formatting.Indented;writer.WriteStartElement(

“DVDlist”);

writer.WriteStartElement(“DVD”);
writer.WriteAttributeString(“ID”, “1”);
//write some simple elements
writer.WriteElementString(“Title”, “Tere Naam”);
writer.WriteStartElement(“Starring”);
writer.WriteElementString(“Actor”, “Salman Khan”);
writer.WriteEndElement();writer.WriteEndElement(); writer.WriteEndElement();writer.WriteEndDocument();writer.Flush();writer.Close();

//wrt.WriteEndDocument();

wrt.Flush();wrt.Close(); //read XML to datagridtransform.Transform(Server.MapPath(“library.xml”), null, Response.Output);

//ds1.ReadXml(Server.MapPath(“library.xml”));

//GridView1.DataSource = ds1; //GridView1.DataBind(); }catch (Exception e1) { Page.Response.Write(e1); }

Posted in XML | Leave a Comment »

Write XML file through C#

Posted by Praveen Kumar on February 11, 2008

try {
var pageTracker = _gat._getTracker(“UA-7968286-1”);
pageTracker._trackPageview();
} catch(err) {}
protected void Button1_Click(object sender, EventArgs e)
{

try {
//XmlDataDocument sourceXML = new XmlDataDocument();
string xmlFile = Server.MapPath(“DVDlist.xml”);
//create a XML file is not exist
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(xmlFile, null);
//starts a new document
writer.WriteStartDocument();
//write comments
writer.WriteComment(“Commentss: XmlWriter Test Program”);
writer.Formatting = Formatting.Indented;

writer.WriteStartElement(“DVDlist”);
writer.WriteStartElement(“DVD”);
writer.WriteAttributeString(“ID”, “1”);

//write some simple elements
writer.WriteElementString(“Title”, “Tere Naam”);
writer.WriteStartElement(“Starring”);
writer.WriteElementString(“Actor”, “Salman Khan”);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();

writer.Close();
} catch (Exception e1) { Page.Response.Write(e1); }
}

Posted in XML | Tagged: | 79 Comments »