Reputation: 984
Basically, I have a small issue trying to display each attibute seperetly when I play it, it seems to display all the attributes in title. I thought you could sort of take the same approach as you do with arrays by writing something like
listView1.Items.Add(items[0]);
I am completly new to this so i apoligize if the question sounds noobish.
xml file:
<books>
<type>
<price>2.50</price>
<title>Harry</title>
</type>
<type>
<price>2.70</price>
<title>bob</title>
</type>
</books>
Code:
XmlTextReader reader = new XmlTextReader("XMLfile1.xml");
XmlNodeType type;
while(reader.Read())
{
type = reader.NodeType;
if (type == XmlNodeType.Element)
{
if (reader.Name == "title")
{
reader.Read();
listView1.Items.Add(reader.Value);
}
}
}
reader.Close();
Upvotes: 1
Views: 233
Reputation: 1535
Try this:
XDocument document = XDocument.Load(@"XMLfile1.xml");
foreach (var titleElement in document.Descendants("title"))
{
listView1.Items.Add(titleElement.Value);
}
or alternatively:
XDocument document = XDocument.Load(@"XMLfile1.xml");
foreach (var titleElement in document.Root.Elements("type").Select(x => x.Element("title")))
{
listView1.Items.Add(titleElement.Value);
}
Upvotes: 1