XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
<node1 attribute1="attrib1" attribute2="attrib2">
<node2>
<node3>Item1</node3>
<node3>Item2</node3>
<node3>Item3</node3>
</node2>
</node1>
</root>
. : , . XmlDocument.GetElementsByTagName(), .
using System;
using System.Xml;
using System.Collections.Generic;
public static class MyXmlParser
{
public static List<string> GetItemsFromXmlByLoopingThroughEachNode(string Filename)
{
List<string> Items = new List<string>();
XmlDocument doc = new XmlDocument();
doc.Load(Filename);
foreach(XmlNode RootNode in doc.ChildNodes)
{
if(RootNode.NodeType != XmlNodeType.XmlDeclaration)
{
foreach(XmlNode Node1Node in RootNode.ChildNodes)
{
XmlAttributeCollection attributes = Node1Node.Attributes;
XmlAttribute Attribute1 = attributes["attribute1"];
foreach(XmlNode Node2Node in Node1Node.ChildNodes)
{
foreach(XmlNode Node3Node in Node2Node.ChildNodes)
{
Items.Add(Node3Node.InnerText);
}
}
}
}
}
return Items;
}
public static List<string> GetItemsFromXmlUsingTagNames(string Filename, string TagName)
{
List<string> Items = new List<string>();
XmlDocument doc = new XmlDocument();
doc.Load(Filename);
XmlNodeList Node3Nodes = doc.GetElementsByTagName(TagName);
foreach(XmlNode Node3Node in Node3Nodes)
{
Items.Add(Node3Node.InnerText);
}
return Items;
}
}
Once you have the necessary data, you can add items to the ComboBox
List<string> Items = MyXmlParser.GetItemsFromXmlUsingTagNames("C:\\test.xml","node3");
ComboBox1.Items.AddRange(Items.ToArray())
Cm.
Xmldocument
XmlNodeList
Xmlnode
XmlAttributeCollection
XmlAttribute
source
share