Total noob when it comes to XML here, I've got an XML file with the structure as follows:
<Error_Codes>
<Test_1>
<Code>ABC</Code>
<Description>Item #1</Description>
</Test_1>
<Test_1>
<Code>DEF</Code>
<Description>Item #2</Description>
</Test_1>
<Test_2>
<Code>UVW</Code>
<Description>Item #3</Description>
</Test_2>
<Test_2>
<Code>XYZ</Code>
<Description>Item #4</Description>
</Test_2>
What I want to do is have the user select either Test 1 or Test 2 from a previous ComboBox, then populate the 2nd ComboBox (let's call it codeCBO) with the values in the <Test 1>
portion of the XML file, ideally with each item in codeCBO structured like Code-Description
. I looked online and see there are a lot of XML tutorials, but I don't see anything like what I'm trying to do. Any help/advice would be much appreciated.
For some reason the Error_codes end tag does not show up here, but it is in the XML file I'm working with
After messing with this for 3 days, I finally found the solution that worked for me. XML file now looks like this:
<Error_Codes>
<Test1 code="0001" description="test ABC"/>
<Test1 code="0002" description="test DEF"/>
<Test2 code="9999" description="test UVW"/>
<Test2 code="8888" description="test XYZ"/>
</Error_Codes>
Then, my C# code looks like this:
private void stationCBO_SelectedIndexChanged(object sender, EventArgs e)
{
codeCBO.Items.Clear();
LoadXML(stationCBO.Text);
}
private void LoadXML(string station)
{
XmlReader reader = XmlReader.Create("Failure_Modes.xml");
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == station))
{
if (reader.HasAttributes)
{
string text = reader.GetAttribute("code") + "-" + reader.GetAttribute("description");
codeCBO.Items.Add(text);
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.