1

nth_element_name

public partial class XML_3 : Window
{
    public XML_3()
    {
        this.InitializeComponent();

        XmlDocument doc = new XmlDocument();
        doc.Load("D:/sample.xml");


        XmlNodeList student_list = doc.GetElementsByTagName("Student");

        foreach (XmlNode node in student_list)
        {
            XmlElement student = (XmlElement)node;

            int element_count = student.ChildNodes.Count;               

        }
    }
}

In above code.I can get the count of element except root element(Student). now the count is 3.

But i have to get 2ed element name(Kavi),it's attribute element name(ID) and it's child element name(FName,MName).

what should i do to get those stuff.

Please help me...

CHANDRA
  • 4,778
  • 8
  • 32
  • 51

1 Answers1

1

Use XDocument (why?):

var doc = XDocument.Parse(xml); // OR Load(...)

var nodeCount = doc.Elements().Count();
var secondNode = doc.Elements().Skip(1).First();
var studentName = secondNode.Name;
var studentId = secondNode.Attribute("ID").Value;

or (for your code):

var secondNode = student.ChildNodes[1] as XmlElement;
var studentName = secondNode.LocalName;
var studentId = secondNode.Attributes["ID"];

Added:

var secondNode = student.ChildNodes[1];
var fName =
    secondNode.ChildNodes.Cast<XmlElement>().FirstOrDefault(x => x.LocalName == "FName").InnerText;
var mName =
    secondNode.ChildNodes.Cast<XmlElement>().FirstOrDefault(x => x.LocalName == "MName").InnerText;
var studentId = secondNode.Attributes["ID"].Value;
Community
  • 1
  • 1
Roman Sokk
  • 175
  • 4
  • var secondNode = student.ChildNodes[1] as XmlElement; with this code i can get second node kavi.Thank you very much for that... But i want to get attribute element like "ID" and child node FName and MName. but i can't get it with above code... – CHANDRA Jun 08 '12 at 11:51
  • Added code for FNAme, MName and ID values :) But in my opinion, XPath and Linq2XML - a more convenient solutions... – Roman Sokk Jun 08 '12 at 12:33
  • but it through the exception 'System.Xml.XmlNodeList' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type ' – CHANDRA Jun 08 '12 at 13:45
  • 1
    Check your using section for System.Linq – Roman Sokk Jun 08 '12 at 14:02
  • Above all codes are very useful to me.but i think that you didn't understand my question.i need only tag name(,) and attribute name(ID) as a String value.I don't want it's InnerText... – CHANDRA Jun 08 '12 at 15:34
  • Ok. How do you expect to get results? two lists? a single list? all child elements (if there is anything other than FName, MName and ID)? or meet the criteria? – Roman Sokk Jun 08 '12 at 16:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/12314/discussion-between-chandru-a-and-roman-sokk) – CHANDRA Jun 08 '12 at 17:50
  • If i get the string value of , and attribute name(ID),then i can get all those stuff what you mentioned above. var secondNode = student.ChildNodes[1] as XmlElement; var studentName = secondNode.LocalName; Here you got the string value("Kavi") of . Like this i have to get the sting value("Fname") of and attribute ID as well. – CHANDRA Jun 08 '12 at 18:05