Breaking News

Editors Picks

Tuesday, April 19, 2011

how to create dynamic xml file in asp.net


function for create XML in C#

public void CreateXMLfile()
        {
            try
            {
                string[,] mc_namedataArray = new string[12,2];
                mc_namedataArray[0, 0] = "#c4d5ea";
                mc_namedataArray[0,1] = "Drenthe";
                mc_namedataArray[1, 0] = "#c4d5ea";
                mc_namedataArray[1,1] = "Flevoland";
                mc_namedataArray[2, 0] = "#76933c";
                mc_namedataArray[2, 1] = "Friesland";
                mc_namedataArray[3, 0] = "#BA636B";
                mc_namedataArray[3, 1] = "Gelderland";
                mc_namedataArray[4, 0] = "#4075b5";
                mc_namedataArray[4, 1] = "Groningen";
                mc_namedataArray[5, 0] = "#bfb0f0";
                mc_namedataArray[5, 1] = "Limburg";
                mc_namedataArray[6, 0] = "#c0d595";
                mc_namedataArray[6, 1] = "Noord-Brabant";
                mc_namedataArray[7, 0] = "#d99593";
                mc_namedataArray[7, 1] = "Noord-Holland";
                mc_namedataArray[8, 0] = "#bfb0f0";
                mc_namedataArray[8, 1] = "Overijssel";
                mc_namedataArray[9, 0] = "#83665a";
                mc_namedataArray[9, 1] = "Utrecht";
                mc_namedataArray[10, 0] = "#c4d5ea";
                mc_namedataArray[10, 1] = "Zeeland";
                mc_namedataArray[11, 0] = "#76933c";
                mc_namedataArray[11, 1] = "Zuid-Holland";
                string[] mcdataArray = new string[12];

                mcdataArray[0] = "NL_DR";
                mcdataArray[1] = "NL_FL";
                mcdataArray[2] = "NL_FR";
                mcdataArray[3] = "NL_GE";
                mcdataArray[4] = "NL_GR";
                mcdataArray[5] = "NL_LI";
                mcdataArray[6] = "NL_NB";
                mcdataArray[7] = "NL_NH";
                mcdataArray[8] = "NL_OV";
                mcdataArray[9] = "NL_UT";
                mcdataArray[10] = "NL_ZE";
                mcdataArray[11] = "NL_ZH";

                XmlDataDocument sourceXML = new XmlDataDocument();
                string xmlFile = Server.MapPath("WorldMap/EXnetherlands_data.xml");
                ////create a XML file is not exist
                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("map");
                writer.WriteAttributeString("map_file", "maps/netherlands.swf");
                writer.WriteAttributeString("tl_long", "3.359523");
                writer.WriteAttributeString("tl_lat", "53.560582");
                writer.WriteAttributeString("br_long", "7.227268");
                writer.WriteAttributeString("br_lat", "80%");

                writer.WriteStartElement("areas");
                dtAnswerPollID = Poll.GetAnswerByPollID(Convert.ToString(PollID));
                for (int i = 0; i < 12; i++)
                {
                    writer.WriteStartElement("area");
                    writer.WriteAttributeString("mc_name", mcdataArray[i]);
                    writer.WriteAttributeString("title", mc_namedataArray[i, 1]);
                    writer.WriteAttributeString("color", mc_namedataArray[i, 0]);                  
                    string stateID=Poll.GetStateIDByStateName(Convert.ToString( mc_namedataArray[i, 1]));
                    if (stateID != "")
                    {
                        writer.WriteAttributeString("value", TotalVote);
                        writer.WriteElementString("description", GetAllMapPollVote(MapPollID, Convert.ToString(stateID)));
                    }
                    else
                    {
                        writer.WriteAttributeString("value", "0");
                        writer.WriteElementString("description", "No Voting");
                       
                    }
                    writer.WriteEndElement();
                }
                writer.WriteStartElement("area");
                writer.WriteAttributeString("mc_name", "borders");
                writer.WriteAttributeString("title", "borders");
                writer.WriteAttributeString("color", "#000000");
                writer.WriteAttributeString("balloon", "false");
                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("labels");
                writer.WriteStartElement("label");
                writer.WriteAttributeString("x", "133");
                writer.WriteAttributeString("y", "153");
                writer.WriteAttributeString("text_size", "18");             
                writer.WriteAttributeString("color", "#000000");
                writer.WriteAttributeString("remain", "true");
                writer.WriteAttributeString("url", "!WorldMap/ammap_data.xml");
                writer.WriteElementString("text", "Back to world map: Netherlands");
                writer.WriteEndElement();
                writer.WriteEndElement();
                //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.Close();

            }
            catch (Exception ex)
            {
                AppBase.handleException("Form:resultspage, Function:CreateXMLfile", ex);
            }
        }


output : XML file

xml version="1.0"?>
<map map_file="maps/netherlands.swf" tl_long="3.359523" tl_lat="53.560582" br_long="7.227268" br_lat="80%">
      <areas>
            <area mc_name="NL_DR" title="Drenthe" color="#c4d5ea" value="0">
                  <description>1 - Google Crome   description>
            area>
            <area mc_name="NL_FL" title="Flevoland" color="#c4d5ea" value="0">
                  <description>1 - Google Crome  description>
            area>
            <area mc_name="NL_FR" title="Friesland" color="#76933c" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="NL_GE" title="Gelderland" color="#BA636B" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="NL_GR" title="Groningen" color="#4075b5" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="NL_LI" title="Limburg" color="#bfb0f0" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="NL_NB" title="Noord-Brabant" color="#c0d595" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="NL_NH" title="Noord-Holland" color="#d99593" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="NL_OV" title="Overijssel" color="#bfb0f0" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="NL_UT" title="Utrecht" color="#83665a" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="NL_ZE" title="Zeeland" color="#c4d5ea" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="NL_ZH" title="Zuid-Holland" color="#76933c" value="0">
                  <description>No Votingdescription>
            area>
            <area mc_name="borders" title="borders" color="#000000" balloon="false" />
      areas>
      <labels>
            <label x="133" y="153" text_size="18" color="#000000" remain="true" url="!WorldMap/ammap_data.xml">
                  <text>Back to world map: Netherlandstext>
            label>
      labels>
map>

No comments :

Post a Comment

Contact Us

Name

Email *

Message *