LINQ to XML, a problem conceptualizing how the tree should look.

Posted by snark on Stack Overflow See other posts from Stack Overflow or by snark
Published on 2010-05-19T05:54:23Z Indexed on 2010/05/19 6:00 UTC
Read the original article Hit count: 322

Filed under:
|
|

Have you ever had one of those days when you've dug a hole but your so deep in now that the only option is to keep on digging and see where you come out? I thought just for giggles in the latest component I'm writing to use LINQ to XML because it was about time I saw what all the hooplah was about.

The problem: I am making a graph component that contains series of data that get graphed and then you can apply a formula to that series and graph another series then apply a formula to that series and so on. So I figured that I would do so in 2 steps, create (and manage) an XML representaion of the series and how they relate to each other, then pass this xml to a draw engine which draws. Conceptually its a tree, with the exception of the root all child series being based upon a parent (1 parent can have many children.

So I should always be adding child nodes to their parent and if I delete a node(series) then I can simply delete the series and its descendants (then draw) and voila all the messy iterating through each node finding parents and children is unneccessary.

Trouble is I dont know how to represent this tree in XML i.e. the structure. My first attempt saw me programatically adding each series as siblings, which worked like a treat because I ended up with an ordered list and thus my order of rendering was maintained.

I had this

    <Chart>
        <Series id="1">seriesText1</Series>
        <Series id="2">seriesText2</Series>
        <Series id="3">seriesText3</Series>
        <Series id="4">seriesText4</Series>
    </Chart>

I'm in a muddle now ... how can I represent a series and a series that has children series. If some-one can give me a hint to how my tree should look (perhaps with a snippet on how to programatically add nodes to their parents) All the examples I have read usually have some container elements such as <ContactS> or <BookS> but my head says I have <series> some of them parent some of them children.

Would appreciate a nudge in the right direction.

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about Xml