Home » Blog » ArrayList XMLFileToArray c#

ArrayList XMLFileToArray c#

/// <summary>
/// Read the Supplied XML File, Return a Generic System Array.
/// </summary>
/// <param name=”sFile”></param>
/// <returns></returns>
public static ArrayList XMLFileToArray(string sFile, string sElementName) {

//Declare Variables
ArrayList aRoot = new ArrayList();
Hashtable hElement;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(sFile);

XmlNodeList xmlNodeList = xmlDoc.GetElementsByTagName(sElementName);

foreach (XmlNode xmlNode in xmlNodeList) {

//Reset the user
hElement = new Hashtable();

foreach (XmlNode xmlChild in xmlNode.ChildNodes) {
hElement.Add(xmlChild.Name, xmlChild.InnerText);
}
//Add the single, key indexed XML-entry to the arrayList
aRoot.Add(hElement);
}

return aRoot;

}

-> XML File eg:
<Users>
<User>
<name>Joe Blogs</name>
<uid>joeblogs@gmail.com</uid>
<type>Logistics</type>
<level>Project</level>
</User>
</Users>