Pages

Showing posts with label xmlwriter. Show all posts
Showing posts with label xmlwriter. Show all posts

Sunday, September 8, 2013

How to write HTML tags in XML using CDATA (disable-output-escaping)-C#

              
C# code to write XML:

                   XmlWriter writer = XmlWriter.Create(MapPath("xmls1.xml"))
                   writer.WriteStartDocument();
                    writer.WriteStartElement("root");
                    writer.WriteStartElement("Items");        // Create subroot
                    for (int col = 0; col < 5; col++)
                    {

                        writer.WriteStartElement("Title");
                        writer.WriteCData("<b>HI</b>This is title");
                        writer.WriteEndElement();
                    }
                    writer.WriteEndElement();                // End subroot

                    writer.WriteEndElement();                  
                    writer.WriteEndDocument();



Output :

<?xml version="1.0" encoding="utf-8"?>
<root>

<items>
    <
Title><![CDATA[<b>HI</b>This is title]]></Title>
  </items>

.......
</root>




NOTE :

When Transform the XML using XSLT , Set disable-output-escaping="yes", to get the output same as in node i.e with html tags.

<xslt:foreach select="root/items">
 <h1>
          <xsl:value-of select="title" disable-output-escaping="yes"/>
 </h1> 

</xsl:foreach>








Monday, September 2, 2013

Create XML manually using XML writer



 XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        using (XmlWriter writer = XmlWriter.Create(MapPath("Products.xml"), settings))
        {
            writer.WriteStartDocument();
            writer.WriteComment("This file is generated by the program.");
            writer.WriteStartElement("Books");

//if u want  foreach()    
             writer.WriteStartElement("Book");
            writer.WriteAttributeString("ISBN",
"0553212419");
            writer.WriteElementString("Title", "
Sherlock Holmes");
            writer.WriteElementString("Author", "
Sir Arhur Conan Doyle");
            writer.WriteEndElement();

//end foreach
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }


It Creates "Products.xml" file as a output,



Product.xml
<?xml version="1.0" encoding="utf-8"?>
<!--This file is generated by the program.-->
<Books>
  <Book ISBN="0553212419">
    <Title>Sherlock Holmes</Title>
    <Author>Sir Arhur Conan Doyle</Author>
  </Book>
</Books>

Create XML file from Datatable with some additonal nodes.

Create Datatable (u can use your datatable)

 private DataTable createDT()
    {
        // Initialise Datatable
        DataTable dt = new DataTable();
        // add columns with their Datatypes
        dt.Columns.Add("Id", Type.GetType("System.String"));                    // decimal type
        dt.Columns.Add("Country_ID", Type.GetType("System.String"));             // string type
        dt.Columns.Add("Name", Type.GetType("System.String"));
        dt.Columns.Add("Description", Type.GetType("System.String"));

        DataRow dr = dt.NewRow();
        dr["Id"] = "323";
        dr["Country_ID"] = "india";
        dr["Name"] = "isaa";
        dr["Description"] = "ewewed";
        dt.Rows.Add(dr);
        return dt;
    }



Create XML file from datatable "dt"

  private void WriteXML(DataTable dt)
    {
        using (XmlWriter writer = XmlWriter.Create(MapPath("employees.xml")))       // creates 'employee.xml' to write data from datatable
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("root");                                        // create root node named 'root'

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                writer.WriteElementString("ID", dt.Rows[i]["Id"].ToString());                        // create childs of the root
                writer.WriteElementString("Country_ID", dt.Rows[i]["Country_ID"].ToString());

                writer.WriteStartElement("Gallery");                                     // create sub root..
                writer.WriteElementString("Name", dt.Rows[i]["Name"].ToString());                  // create sub child nodes..
                writer.WriteElementString("Description", dt.Rows[i]["Description"].ToString());
                writer.WriteEndElement();
            }
            writer.WriteEndElement();                                              // end root node
            writer.WriteEndDocument();
        }
    }



Output : (employees.xml)

<?xml version="1.0" encoding="utf-8"?>
<root>
  <ID>323</ID>
  <Country_ID>india</Country_ID>
  <Gallery>
    <Name>isaa</Name>
    <Description>ewewed</Description>
  </Gallery>
</root>