my xml is somehting like this:

<loc id="123">
		<value>data1</value>
	</loc>
	<loc id="223">
		<value>data2</value>
	</loc>
	<loc id="323">
		<value>data3</value>
	</loc>
	<loc id="423">
		<value>data4</value>
	</loc>

how do I read all the ids?
this code give me only the first id (123). when it loops to next, throws exception.

XmlNodeList xmlnode = xmldoc.GetElementsByTagName("loc");
for (int x = 0; x < xmlnode.Count; x++)
{
     XmlAttributeCollection xmlattrc = xmlnode[x].Attributes;
     string id = xmlattrc[x].Value;
}

Hi,

Try changing xmlttrc.[x].Value for xmlttcr[0].Value

Hint - Whenever you get an exception copi the output an pasted in your question.

Camilo

通过下面的方法你可能从提供的xml文件中,提取出所有id的value;

I hope it is useful to you .

XmlDocument xmldoc = new XmlDocument();


            StreamReader sr = new StreamReader("../../XMLTest.xml");

            XmlReader xmlrd=XmlReader.Create(sr);

            //xmldoc.LoadXml();
            xmldoc.Load(xmlrd);
           XmlNodeList nodes= xmldoc.SelectNodes("locs/loc");

           foreach (XmlNode node in nodes) {
              string str1= node.Attributes["id"].Value;
              comboBox1.Items.Add(str1);

            }

yep, that solves the issue. thanks Camilo.

string id = xmlattrc[0].Value;

Hi,

Try changing xmlttrc.[x].Value for xmlttcr[0].Value

Hint - Whenever you get an exception copi the output an pasted in your question.

Camilo

This article has been dead for over six months. Start a new discussion instead.