Reputation: 15
The XML data:
<LIST>
<TITLE>Item</TITLE>
<YEAR>2013</YEAR>
<NAME>
<NAME_ n="1">AB</NAME_>
<NAME_ n="2">CD</NAME_>
<NAME_ n="3">EF</NAME_>
<NAME_ n="4">GH</NAME_>
<NAME_ n="5">IJ</NAME_>
</NAME>
<PRICE>
<PRICE_ n="1">100</PRICE_>
<PRICE_ n="2">103</PRICE_>
</PRICE>
<AV>
<AV_ n="0">128,457</AV_>
<AV_ n="1">1,746,301</AV_>
<AV_ n="2">173,247</AV_>
<AV_ n="3">246,102</AV_>
</AV>
<PIC>
www.example.com/pictest.jpg
</PIC>
<URL>www.test.com/itemtest</URL>
</LIST>
I tried with this code:
XmlTextReader reader = new XmlTextReader(a);
while (reader.Read())
{
if (reader.Name == "AV")
{
label1.Text += reader.Value;
}
Now, I know that I need more help with my code since I can't get any output!
How can I read values from this XML?
Anyone can help me for get all value from this XML ? bcs I'm very very new in xml and need for sample for start work ! :(
Upvotes: 0
Views: 462
Reputation: 3220
You are finding the parent element and getting the text value of it. It doesn't have a text value, only another child.
Try instead
XmlTextReader reader = new XmlTextReader(a);
while (reader.Read())
{
if (reader.Name == "AV_")
{
label1.Text += reader.Value;
}
Upvotes: 0
Reputation: 23646
To get all numeric values and attribute number from contents of AV_
node, use next script:
string str = "..."; //xml contents go here
var xml = XDocument.Parse(str);
var values = xml.Descendants("AV_")
.Select(n => n.Attribute("n").Value + " : " + n.Value);
values.ToList()
.ForEach(Console.WriteLine);
prints:
0 : 128,457
1 : 1,746,301
2 : 173,247
3 : 246,102
don't forget to add reference to Linq to XML namespace using System.Xml.Linq
Upvotes: 1