views:

76

answers:

2

Hi All,

I have a xml file which is currently made manually and I have to make a functionality(UI) where user can enter the data and I have to store the data and generate the xml file dynamically in .NET.

Problem is the format of the xml file. I am not able to decide how I am going to store that data and then dynamically generate xml from that.

Please find the some of the extract of the code from the xml file below:

<?xml version="1.0" encoding="UTF-8"?>
<DATA>
  <SDACTS>
    <SDACT TYPE="Economy" COLOUR="0xff0000"/>
    <SDACT TYPE="Environment" COLOUR="0x00ff00"/>
    <SDACT TYPE="People" COLOUR="0x0000ff"/>
    <SDACT TYPE="Society" COLOUR="0xff00ff"/>
  </SDACTS>
  <INDUSTRIES>
    <INDUSTRY TYPE="Platinum" COLOUR="0x0094B1">
      <PRODUCT>Platinum</PRODUCT>
      <PRODUCT>Palladium</PRODUCT>
      <PRODUCT>Rhodium</PRODUCT>
      <PRODUCT>Gold</PRODUCT>
    </INDUSTRY>
    <INDUSTRY TYPE="Diamonds" COLOUR="0x652382">
      <PRODUCT>Diamonds</PRODUCT>
    </INDUSTRY>
 <INDUSTRY TYPE="Metallurgical Coal" COLOUR="0x999a8f">
      <PRODUCT>Metallurgical Coal</PRODUCT>
    </INDUSTRY>
</INDUSTRIES>
  <LOCATIONS>
    <CONTINENT TITLE="South America">
      <COUNTRY TITLE="Brazil">
        <HEADOFFICE>So Paulo</HEADOFFICE>
        <ADDRESS>
         Sau, polo, ambikaui 
        </ADDRESS>
        <LATITUDE>-23.571157</LATITUDE>
        <LONGITUDE>-46.644146</LONGITUDE>
        <BUSINESSUNITS>Nickel; Iron ore and manganese</BUSINESSUNITS>
        <DESCRIPTION>Anglo American has been operating in Brazil since 1973. Our core operations are involved in the production of nickel, iron ore and maganese, while our interests in the production of phosphates and niobium at Copebras and Catalo respectively have been identified for divestment. Nickel projects in the pipeline include Barro Alto.</DESCRIPTION>
        <EMPLOYEES/>
        <NUMBEROFBUSINESS>2</NUMBEROFBUSINESS>
        <!--New project added - 12/02/2010  start -->
        <PROJECT>
          <TYPE>Greenfield</TYPE>

          <NAME>Minas Rio expansion</NAME>
          <UNITTYPE>Iron Ore and Manganese</UNITTYPE>
          <RELATEDOPERATION>Greenfield</RELATEDOPERATION>
          <LATITUDE>-18.92814</LATITUDE>
          <LONGITUDE>-43.42562</LONGITUDE>
          <STATUS>Future unapproved</STATUS>

          <DESCRIPTION/>
          <FULLPRODUCTIONDATE>TBD</FULLPRODUCTIONDATE>
          <PRODUCTIONVOLUME>Up to 53 Mtpa iron ore pellet feed (wet basis)</PRODUCTIONVOLUME>
          <!-- <ESTIMATEDCAPEX>TBD</ESTIMATEDCAPEX>-->
          <FOOTNOTES>
            <![CDATA[1. Capital expenditure shown on 100% basis in nominal terms. Platinum projects reflect approved capex.<br><br>2. Production volume represents 100% of average incremental or replacement production, at full production, unless otherwise stated.]]>
          </FOOTNOTES>
        </PROJECT>
        <SDACTIVITY>
          <ID>3.2.4.20</ID>

          <TYPE>Society</TYPE>
          <BUSINESS>Nickel</BUSINESS>
          <RELATEDOPERATION/>
          <HEADING>Listening - and acting - in Brazil</HEADING>
          <SUBHEADING>SEAT community engagement session in Brazil</SUBHEADING>
          <COPY>
            local government.
          </COPY>
          <IMAGE>3.2.4.20.jpg</IMAGE>
          <LINKCAPTION>Read more about SEAT in Brazil</LINKCAPTION>
          <LINKURL>http://www.angloamerican.co.uk/aa/development/case-studies/society/society01/&lt;/LINKURL&gt;
        </SDACTIVITY>
 </COUNTRY>
   </CONTINENT>
  </LOCATIONS>
</DATA>
A: 

You could just store it directly in the DOM, and serialise when desired.

Paul Butcher
Thanks Paul but i wud need more information!
Varun
Like what? Create a DOM of the skeleton XML, then when the user changes the values, store those values into the appropriate nodes in the DOM. When you want the XML to become a file, save it.
Paul Butcher
Right Paul. I feel i have to store the values into the database but i need to decide the table structure.
Varun
Why do you have that feeling? Are you doing something other than create this XML? If the only purpose of this UI is to create an XML representation of the inputs, then messing about with databases and tables is pointless.
Paul Butcher
Paul, let me get bit in detail. The xml file is used by flash developer for something like google map feature. User needs to have the facility to delete and modify the data(i.e. nodes and attributes). So I feel that storing it in database can be handy. I believe just fetch the data do something like dataset.getXML or dataset.wrtieXml
Varun
All of that can be achieved via the DOM. Unless you wish to run queries that you can't write in XPath, a Tabular Database is a pointless bit of cruft that will hamper future maintenance.
Paul Butcher
Right, Can you give some sample/code or start -up sort of which matches my requirement.You saw my xml data right.
Varun
Here: http://support.microsoft.com/kb/317665
Paul Butcher
Thanks paul. Let me go through it. Will get back to you for further suggestions :)
Varun
Here I get my answer :http://support.microsoft.com/kb/317666Now the challenge is how to make a UI which allows user to enter the values and attributes they want to add,remove, and modify
Varun
A: 

If I were you I would just store the data in a db and then use an xmltexwriter to write it to where ever.

using (XmlTextWriter writer = new XmlTextWriter(OutputStream, Encoding.UTF8)) {
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("DATA");
writer.WriteStartElement("SDACTS");
foreach ( SDACT in SDACTs) {
    writer.WriteStartElement("SDACT");
    writer.WriteAttributeString("TYPE", SDACT.Type);
    writer.WriteAttributeString("COLOUR", SDACT.COLOUR);
    writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();

}

You could also take advantage of using .nets XML serialization see http://msdn.microsoft.com/en-us/library/ms950721.aspx

Mike
Hi Mike, Thanks for replying.I would like to tell you that I have not worked more in xml, have just stared and also I use C# as my language but that is ok...Now I would like to have some more information in details like the loop which you are doing is of table SDACTS? And the columns of the table would be Type and colour?Can you please just give a detailed information on my first few lines of xml file starting from SDACTS to Country tag and what kind of table structure wud i need to make to store data and then the code which builds the xml file?I wud appreciate tat.I saw d link but :(
Varun
I updated it for c#. I made alot of assumptions here so it is more or less psudocode. I was assuming a generic list for the SDACTS list<SDACT>. If I were you I would create a class with for each item in your xml. Starting with DATA. Data would then have properties such as SDACTs which would be of the generic list type I mentioned earlier. Then the SDACT class would have properties of Type and Colour. and so on. this may be overkill for what you are doing, so if you have the data you could just manually write it out the same way without lopping through collections.
Mike
Thanks Mike. I would like to have your personal mail id if u r ok with that.
Varun
@Mike: +1 for using the `using` block, but -1 for using `new XmlTextWriter()`. that's been deprecated since .NET 2.0! Use `XmlWriter.Create()` instead.
John Saunders
Thanks John!!!!
Mike