views:

35

answers:

1
<?xml version='1.0' encoding='UTF-8'?>  
<StockMarket>
    <StockDate Day = "02" Month="06" Year="2010"> 
        <Stock>     
            <Symbol>ABC</Symbol>                    
            <Amount>110.45</Amount>
        </Stock>
        <Stock>
            <Symbol>XYZ</Symbol>                
            <Amount>366.25</Amount>
        </Stock>
    </StockDate>
    <StockDate Day = "03" Month="06" Year="2010"> 
        <Stock>     
            <Symbol>ABC</Symbol>                
            <Amount>110.35</Amount>
        </Stock>
        <Stock>
            <Symbol>XYZ</Symbol>                
            <Amount>369.70</Amount>
        </Stock>
    </StockDate>
</StockMarket>

My approach so far is

     XDocument doc =
  new XDocument(
    new XElement("StockMarket",
      new XElement("StockDate", new XAttribute("Day", "02"),new XAttribute("Month","06"),new XAttribute("Year","2010")),
      new XElement("Stock", new XElement("Symbol", "ABC"), new XElement("Amount", "110.45"))
      )
    );

Since I am new to Linq to XML, I am presently struggling a lot and henceforth seeking for help.

What I need is to generate the above XML programatically(I mean to say that some kind of looping...) The values will be passed from textbox and hence will be filled at runtime.

At present the one which I have done is a kind of static one. This entire stuff has to be done at runtime.

One more problem that I am facing is at the time of saving the record for the second time.

Suppose first time I executed the code and saved it(Using the program I have done). Next time when I will try to save then only

<StockDate Day = "xx" Month="xx" Year="xxxx"> 
            <Stock>     
                <Symbol>ABC</Symbol>                    
                <Amount>110.45</Amount>
            </Stock>
            <Stock>
                <Symbol>XYZ</Symbol>                
                <Amount>366.25</Amount>
            </Stock>
        </StockDate>

should get save(or better appended) and not <StockMarket> .... </StockMarket>.. Because that will be created only at the first time when the XML will be generated or created.

I hope I am able to convey my problem properly. If you people is having difficulty in understanding my situation, kindly let me know.

Using C#3.0 .

Thanks

+1  A: 

You can add elements and attributes once the document is created easy enough:

XDocument doc = /* what you had already */

// get the "<StockMarket>" element
var stockMarketElem = doc.Element("StockMarket");

// add a new StockDate to it in a loop
for(var dt = new DateTime(2010, 1, 1); dt <= new DateTime(2010, 6, 1); dt = dt.AddDays(1))
{
    stockMarketElem.Add(
      new XElement("StockDate",
        new XAttribute("Day", dt.Day), new XAttribute("Month", dt.Month), ...
    );
}

Also, to add new elements after you've saved the file, you can load it back with XDocument.Load then use the same as above to add new stocks to the end before saving it again.

If you're expecting the file to get rather large, then XML may not be the best option, but if you're not expecting too much data then it's probably OK.

Dean Harding