Hi, I'm a newbie in programming in C # and I need advice ..
I have a list of XML to Listbox but unfortunately I do not know how to do it on condition that it has called me a text from the ID
XML file :

<slovicka>
	<lekce id="1">
		<verb cz="Ahoj" en="Hello" />
		<verb cz="Pes" en="Dog" />
	</lekce>
	<lekce id="2">
		<verb cz="Pivo" en="Beer" />
	</lekce>
</slovicka>

C# :

//--------XML----------
	XmlDocument xml = new XmlDocument();
       	 xml.Load("verbs.xml");
		    
            foreach (XmlNode n in xml.SelectNodes("/slovicka/lekce"))
            {
            	lekceview.Items.Add("Lekce : " + n.Attributes["id"].Value);
            }
            
            foreach (XmlNode na in xml.SelectNodes("/slovicka/lekce/verb"))
            {
            	cz.Text = na.Attributes["cz"].Value;
            	en.Text = na.Attributes["en"].Value;
            	czen.Items.Add("Cz : " + na.Attributes["cz"].Value);
            	czen.Items.Add("En : " + na.Attributes["en"].Value);
            	czen.Items.Add("---------------------------------");
            }
            
            //--------EndExtractXML--------

It lists me unfortunately everything :-(
//Sorry my English is Bad//

Recommended Answers

All 13 Replies

This here Yeah I know but me on how to make it a condition that when clicked it will automatically detect my ID and write it in my terms

This here Yeah I know but me on how to make it a condition that when clicked it will automatically detect my ID and write it in my terms

Add an event on the Combobox and replace the '12' from the previous post by a variable:

foreach (XmlNode na in xml.SelectNodes("/slovicka/lekce[@id="+cmb.SelectedValue()+"]/verb"))

I'm not quite sure what the exact command but there is something like this, intellisense will complete this for you ;)

While I do not know how you think might work but it throws me tudle mistake and I do not know what to do

System.Windows.Forms.ListControl.SelectedValue member can not be used as a method.

When the picture is here

While I do not know how you think might work but it throws me tudle mistake and I do not know what to do

System.Windows.Forms.ListControl.SelectedValue member can not be used as a method.

When the picture is here

Sorry for typo. It's not a method it's a property so it'll only be cmb.SelectedValue

But unfortunately that's when you start writing errors :-(
System.Xml.XPath.XPathException: expression must be evaluated as a set of nodes.
Error

But unfortunately that's when you start writing errors :-(
System.Xml.XPath.XPathException: expression must be evaluated as a set of nodes.
Error

try this xpath instead.

//lekce[@id="+cmb.SelectedValue()+"]

Which means. For all lekce that has this id.

I just do not know where I'm wrong you throw me your advice mistakes and I do not know what to do ... Whether you write it anywhere so I wrote it mainly a bug I do not know the command SelectedValue()

I just do not know where I'm wrong you throw me your advice mistakes and I do not know what to do ... Whether you write it anywhere so I wrote it mainly a bug I do not know the command SelectedValue()

Here is how I would do it.

XmlDocument xml = new XmlDocument();
xml.Load("verbs.xml");

XmlNodeList xnl = xml.SelectNodes("//lekce[@" + cmb.SelectedValue + "]");

foreach(XmlElement xe in xnl)
{
          //do your stuff here
}

or with linq

XDocument xml = XDocument.Load("verbs.xml");

XElement x = xml.Descendants("lekce").Where(p => p.Attributes("id").Value == cmb.SelectedValue).FirstOrDefault();

//do whatever with x

Still a bug in the SelectedValue not know what it is and not know who's friend has programmed a few years
XmlNodeList xnl = xml.SelectNodes("//lekce[@" + cmb.SelectedValue +"]")
Error it's Whether you write it anywhere so I wrote it mainly a bug I do not know the command SelectedValue() in Starting Program

Still a bug in the SelectedValue not know what it is and not know who's friend has programmed a few years
XmlNodeList xnl = xml.SelectNodes("//lekce[@" + cmb.SelectedValue +"]")
Error it's Whether you write it anywhere so I wrote it mainly a bug I do not know the command SelectedValue() in Starting Program

try:

string thisShouldWork = listBox1.SelectedItem.ToString();

Well, This here would be but again when selecting 'lekce' is nothing but lists :-(

foreach (XmlNode na in xml.SelectNodes("/slovicka/lekce[@id=\""+lekceview.SelectedValue+"\"]/verb"))

Please help me, I'm not show an error but again the selection of 'lekce' nothing write :-(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.