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;
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With