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;
}

Recommended Answers

All 4 Replies

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);

            }

Hi

Regrds,
Camilo

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

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.