Reputation: 5709
I made this XML file that I need to try and generate a GUI from. No I am not going to jump over to WPF if you were wondering :-)
Here is the XML that I made:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<gui groupboxlabel="Barnets Stamdata" type="CHILD">
<textbox label="CPR" />
<textbox label="Navn" />
<textbox label="Efternavn" />
<textbox label="Addresse" />
<textbox label="Hus nr." />
<textbox label="Opgang" />
<textbox label="Post Nr." />
<textbox label="By" />
<textbox label="Email" />
<textbox label="Telefon nr." />
<textbox label="Sagsbehandler" />
<textbox label="Konsulent" />
<textbox label="Aflastning" />
<!-- <combobox label="Foranstaltning" /> -->
<!-- <date label="Anbring" /> -->
<!-- <date label="Udskriv" /> -->
</gui>
</root>
I need to find the gui
tag first, so I can extract the 2 pieces of information there. Then I have to make a custom textbox control with a certain label name for every textbox child there.
I tried to do something like this at firs to try and print out what it looked like, but the code doesn't work because the child nodes that I find are null:
public void CreateNewLayout(Form parent, String path, String token)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(path);
XmlNodeList gui = xmlDoc.GetElementsByTagName("gui");
if (gui.Count == 0)
{
MessageBox.Show("XML fil har ingen elementer", "Fejl");
return;
}
while (gui.GetEnumerator().MoveNext())
{
gui.GetEnumerator().Current.ToString();
}
}
Problem is, my XML is pretty rusty...any help?
Upvotes: 0
Views: 118
Reputation: 1625
Use something like this:
XDocument document = XDocument.Load(@"C:\DOTNET\PRACTICE\XmlTest\XmlTest\XMLFile1.xml");
XElement guiNode = document.Root.Element("gui");
List<XAttribute> attributes = new List<XAttribute>();
foreach(var attribute in guiNode.Attributes())
{
attributes.Add(attribute);
}
This uses the XDocument API instead of the older XMLDocument API. You can add null checks wherever you need them.
Upvotes: 0
Reputation: 2454
I would suggest giving different values for your textbox nodes such as textbox1, textbox2... If not something like the following should work:
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
List<string> labels = new List<string>();
doc.LoadXml([This would be text from file]);
string groupboxlabel = doc.SelectSingleNode("root/gui").Attributes["groupboxlabel"].Value;
string type = doc.SelectSingleNode("root/gui/textbox").Attributes["type"].Value;
System.XmlNodeList nodeList = doc.SelectNodes("root/gui");
foreach (XmlNode node in nodeList)
{
labels.Add(node.Attributes["label"].Value;);//Now you will have a list of labels
}
Hope this helps
Upvotes: 0
Reputation: 52798
You can use Linq-Xml
var document = XDocument.Parse(inputXmlString);
document
.Root
.Element("gui")
.Elements()
.Select(element =>
new
{
Type = element.Name,
Label = element.Attribute("label").Value,
})
.Dump();
This is a quick example in Linqpad to show how to turn your XML into an anonymous type. It doesn't have to be an anonymous type it can be able type you want...
Also, if you want to filter elements, pass the element name into .Elements(string)
.
Upvotes: 4