Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get xml Node name and Inner Text and Fill Grid view

I have One XML file, showing Below,

Now I dont know number of its nodes under "appsetting" element. I am trying to fill grid view using this XML file, somehow like Gridview have column of node name like tag 1, tag 2, tag 3... etc And have one row which have corresponding tags values like val 1, val 2, val 3..

I try somemy self, but i am not getting how to fetch inner value without giving Node Name. I try this,

       XmlDocument doc = new XmlDocument();
        doc.Load(XmlPath);
        XmlNodeList xnList = doc.SelectNodes("appSettings");
        foreach (XmlNode xn in xnList)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Tag1");
            dt.Columns.Add("Tag2");
            dt.Columns.Add("Tag3");
            DataRow dr;
            dr = dt.NewRow();
            dr["Tag1"] = xn["Tag1"].InnerText;
            dr["Tag2"] = xn["Tag2"].InnerText;
            dr["Tag3"] = xn["Tag3"].InnerText;
            dt.Rows.Add(dr);

            dgv.DataSource = dt;
        }

        dgv.AllowUserToAddRows = false;
        dgv.ReadOnly = true;   

enter image description here

like image 835
VARUN NAYAK Avatar asked Feb 11 '14 09:02

VARUN NAYAK


People also ask

How do I select a specific node in XML?

XPath is used programmatically to evaluate expressions and pick specific nodes in an XML document. To select nodes from XML, use the Evaluate() method. Copy var dealers = document. evaluate("//Dealer", document, null, XPathResult.

How do I get XML nodes?

To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string.

What is XML node name?

Indicates the name of the XML element or attribute representing the ProDataSet, the temp-table, the temp-table buffer, or the temp-table buffer-field object name in an XML document. This attribute's purpose overlaps with the SERIALIZE-NAME attribute.

What is node and tag in XML?

Everything in an XML document is a node. For example, the entire document is the document node, and every element is an element node. Root node. The topmost node of a tree. In the case of XML documents, it is always the document node, and not the top-most element.


1 Answers

I got This Answer With Help of @ Arin Ghazarian , I just Modify little bit in his code,,

        XmlDocument doc = new XmlDocument();
        doc.Load(XmlPath);


        DataTable dt = new DataTable();
        foreach (XmlNode xn in doc.ChildNodes[0])
        {
            string tagName = xn.Name;
            if (!dt.Columns.Contains(tagName))
            {
                dt.Columns.Add(tagName);
            }

        }
        DataRow dr = dt.NewRow();
        foreach (XmlNode xn in doc.ChildNodes[0])
        {

            dr[xn.Name] = xn.InnerText;


        }
        dt.Rows.Add(dr);
like image 55
VARUN NAYAK Avatar answered Oct 13 '22 04:10

VARUN NAYAK