Create XML document from ArrayList

LBJuice

n00b
Joined
Jan 23, 2005
Messages
27
I'm trying to create an XML document from a C# ArrayList that contains redundant data.

IE:

IT
Employee Name

IT
Systems
Employee Name

IT
Systems
Employee Name

IT
Business Applications
Emplyoee Name

IT
Business Applications
Emplyoee Name

Legal
Emplyoee Name

and I want to create an XML Document in this order:

<IT>
-<EmployeeName>
Mary Lee</EmployeeName>
-<Systems>
-----<EmployeeName>Joe Smo</EmployeeName>
-----<EmployeeName>Steve Perry</EmployeeName>
-</Systems>
-<Business Applications>
-----<EmployeeName>So On</EmployeeName>
-----<EmployeeName>So Forth</EmployeeName>
-</Business Applications>
</IT>
<Legal>
-----<EmployeeName>Harry Mary</EmployeeName>
...
(Notice how some data isn't necessarily the same "depth")

I believe I need to use DataTables and create table relationships, but I do not know if I need a table for each child, that would take a lot of work, and I'm sure there's an easier way.
 
I'd suggest looking in to the System.Xml namespace and learning how to use the XmlDocument class. If you are unfamiliar with the DOM I'd suggest searching for it on your favorite search engine and going from there.

Using XmlDocument it'd just be a matter of knowing how to parse the input data. Once a reliable method is established you'd start at the top and traverse down, adding nodes until you get to a text data node. Add that as the InnerText (or Value, depending upon how you do this) and you should be fine. Most of your loops will consist of performing a SelectSingleNode("whatever") to see if it exists already--if not, create a new node and add it to the children, then navigate to it and continue your parsing exercise.

Based on the data you have up there it appears that each block of data has a blank line between it. The data inside of each block consists of single entries delimited by a newline, and the final entry would (presumably) be the employee's name.

202276
 
Back
Top