I’m coding an XML viewer for a school assignment but I don’t have any clue how I can load XML-files without knowing the structure of the file.
I’ll display the XML structure in a listbox after I’ve read the file.
It’s a school assignment and This should be the result :

I have an example file that I can load perfectly but I’m stuck with a random file.
XmlDocument doc = new XmlDocument();
using(XmlReader xmlReader = XmlReader.Create("c:\\temp\\sites.xml"))
{
//Load file
doc.Load(xmlReader);
XmlNode root = doc.DocumentElement;
foreach (XmlNode siteNode in root)
{
//Nodes
XmlNode URLNode = siteNode.FirstChild;
XmlNode EmailNode = siteNode.LastChild;
//Create site
Site site = new Site(URLNode.FirstChild.Value, EmailNode.FirstChild.Value);
//Add to list
sites.Add(site);
}
Console.WriteLine(sites.Count);
}
You’re not going to be able to turn the XML data into known classes (like your
Site) class without knowing something about the structure, as you’d need some way to migrate the data to the class constructor parameters/properties/etc.That being said, if you just want to view or inspect the XML file itself, you can just recursively inspect the XmlNode elements inside the root element.
XmlNode includes all of the properties you’d need to do this, such as ChildNodes, Attributes, and Value.