Is it possible to create the following XML using Xdocument and append the relevant protion in subseq

Posted by Newbie on Stack Overflow See other posts from Stack Overflow or by Newbie
Published on 2010-06-03T03:10:22Z Indexed on 2010/06/03 4:14 UTC
Read the original article Hit count: 260

Filed under:
|
|
<?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

© Stack Overflow or respective owner

Related posts about Xml

Related posts about c#3.0