i want to display the first node xml attribute on form?



I learn C# already, so I have this xml file:



<tree>
<grand name ="tom" id="1" sex="m" status="d" child="2" father="" />
<grand name="adam" id="11" sex="m" status="d" child="1" father="1" />
<grand name="john" id="111" sex="m" status="d" child="1" father="11" />
<grand name="pierre" id="1111" sex="m" status="d" child="3" father="111" />
<grand name="jan" id="11111" sex="f" status="d" child="" father="1111" />
<grand name="marc" id="11112" sex="m" status="d" child="" father="1111" />
</tree>


i try this code to display attribute of the first node only on load the form, but I get nothing:



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace olwantree
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
XmlDocument XDoc = new XmlDocument();
XDoc.Load("F:\\tree.xml");
XmlNode att = XDoc.SelectSingleNode("//grand/@name");
string nam = att.ToString();
label1.Text = att;
}
}
}


thank you very much


No comments:

Post a Comment