-1

I am trying to create following tree structure from an xml in C#:

Root
-->A
--->1
--->2
-->B
--->1
--->2

The xml would be like this:

<root>
    <parent1 name="A">
        <child name=1>
        <child name=2>
    </parent1>
    <parent2 name="B">
        <child name=1>
        <child name=2>
    </parent2>
</root>

Any guidelines?

Sara
  • 23
  • 2

3 Answers3

5

Assuming its ASCII art you want ... this one-liner Linq query:

string tree = doc.Root.DescendantsAndSelf().Aggregate("",
    (bc, n) => bc + n.Ancestors().Aggregate("", (ac, m) => (m.ElementsAfterSelf().Any() ? "| " : "  ") + ac,
    ac => ac + (n.ElementsAfterSelf().Any() ? "+-" : "\\-")) + n.Name + "\n");

Will convert your XML into this:

\-Root
  \-parent1 
  | +-child
  | \-child
  \-parent2 
    +-child
    \-child

Do I get the prize for making it a one-line answer? ;-)

ColinE
  • 68,894
  • 15
  • 164
  • 232
2
var doc = System.XML.Linq.XElement.Parse(xmlString);

An XDocument is a tree.

H H
  • 263,252
  • 30
  • 330
  • 514
0

Old answer, assuming you had control of the XML

You could try creating the data structure you want, mocking up a console app that populates that data structure, and use XmlSerializer to automatically output a sample XML file. C# will be able to import the result with no extra code.

If you want to customize the XML, you can use attributes in the System.Xml.Serialization namespace to mark up your code, and get custom element/attribute names.

Edit:

Removed code sample, because it doesn't apply to the final answer.

The XML you have isn't conducive to simple XML serialization in C#. If you play around with the suggestion I made in the old version of my answer, you'll see this is true.

I suggest you create hierarchical data structures in C# that map to the problem you are trying to solve, in C#, rather than trying to bend them around the XML data that you happen to have.

Once you have that structure, work on implementing IXmlSerializable for each of those data types.

Beware that custom XML serialization is a pain, so if you can, you should make your serialization/data work to support your code, not the other way around.

You could get around this fact by writing an XSLT to convert your data to a format that is more amenable to XML serialization. Here's a way to apply an XSLT transform in C#:

How to apply an XSLT Stylesheet in C#

If you do this, my original answer applies. Make a data structure that is easily serializable to/from your intermediate format.

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • I already have the xml I am trying to create the tree here. – Sara Jun 06 '11 at 06:53
  • @Sara: Gotcha. I edited my answer to apply more to your situation. Ultimately, XML serialization is the best way to convert between data structures and XML. Unfortunately, it's going to be more work. You'll be happier coding against the end result, though, than if you tried to work directly against XML, or if you write an XML -> data converter that doesn't use XML serialization. – Merlyn Morgan-Graham Jun 06 '11 at 07:08