Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I build an Hierarchy of data in C# using a table of data
    primarykey
    data
    text
    <p>I am in a middle of an exercise where I am trying to manually make an XML file from data which a user enters into a table. Here is a sample Table:</p> <p><strong>Note, in my code example later, more fields are exposed than this table for future purposes.</strong></p> <p><strong>Note 2, The data in this table is manually entered by a user into a datagridview control on my windows form application. No SQL involved here.</strong></p> <pre><code>ID | ParentID | Element | DefaultValue | ======================================== 0 | -1 | root | | 1 | 0 | first | SomeData | 2 | 0 | second | OtherData | 3 | 0 | third | | 4 | 3 | firstCh | Child of ID=3| ======================================== </code></pre> <p>I am trying to construct an XML File from this using C#, and without using any XML Classes. I am trying to do this through pure string manipulation and formatting, because if this test succeeds, I may expand it to do more things which could prove use to us in our production environment at work. </p> <p>My current thought pattern is to iterate through each and every element, determine if the element at a certain ID contains a child, build a object list out of these children. Iterate through the child list, then determining if "grandchildren" exist in the children elements in the children list. Here is my code class which I made.</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace XMLtoXSDconverter { class XMLConstruct { public enum ElementType { None, StringElement, LongElement, BoolElement, DateTimeElement, FloatElement, IntElement, DoubleElement }; public int id; public int parentID; public string element; public string comment; public int minOccurs; public int maxOccurs; List&lt;XMLConstruct&gt; children; int indent = 0; public string buildXMLConstruct(int id, int parentId, string element, string comment, ElementType elementType, List&lt;XMLConstruct&gt; children, int minOccurs, int maxOccurs) { this.id = id; this.parentID = parentId; this.element = element; this.comment = comment; this.minOccurs = minOccurs; this.maxOccurs = maxOccurs; string elementTypeString = string.Empty; switch (elementType) { case ElementType.StringElement: elementTypeString = "xs:string"; break; case ElementType.LongElement: elementTypeString = "xs:long"; break; case ElementType.IntElement: elementTypeString = "xs:int"; break; case ElementType.FloatElement: elementTypeString = "xs:float"; break; case ElementType.DoubleElement: elementTypeString = "xs:double"; break; case ElementType.DateTimeElement: elementTypeString = "xs:dateTime"; break; case ElementType.BoolElement: elementTypeString = "xs:boolean"; break; default: elementTypeString = string.Empty; break; } if (this.id == 0) element += "Wrapper"; //Hiccup here, how would I construct the children elements? Recursion perhaps? These children should come in the List&lt;XSDConstruct&gt;, which may contain children too return null; } } } </code></pre> <p>I have been scratching my head about how to approach this problem and how to solve it. As you can see, the reader and my possible savior from this Friday afternoon nightmare, <code>XSDConstruct</code> contains most fields required to be able to make an XML and XSD file out of this. There is a <code>List&lt;XSDConstruct&gt;</code> child object which can contain the children of each element, and so forth, clearly yelling at using recursion.</p> <p>So my question is, how will I read the data in my table into my object to contain a tree of objects, n - levels deep?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload