Reputation: 6918
I have a XML file and i want to read it and save the result into datatable, here is my XML file:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table name="tblAdminUser">
<Column>
<displayname>
Created Date
</displayname>
<orignalvalue>
Created_Date
</orignalvalue>
</Column>
<Column>
<displayname>
First Name
</displayname>
<orignalvalue>
F_Name
</orignalvalue>
</Column>
</Table>
<Table name="test1">
<Column>
<displayname>
Last Name
</displayname>
<orignalvalue>
L_Name
</orignalvalue>
</Column>
<Column>
<displayname>
Created By
</displayname>
<orignalvalue>
Created_By
</orignalvalue>
</Column>
</Table>
</NewDataSet>
Now what i want to do is:
If the "Name" of "table" node is "tblAdminuser" then i want to get the "display column" node's inner values and corresponding "original value" in a Datatable in two columns.
What i have done up to now is:
XmlDataDocument xmldoc = new XmlDataDocument();
xmldoc.Load(Server.MapPath("~/XMLFile.xml"));
XmlElement root = xmldoc.DocumentElement;
XmlNodeList tablenodes = root.SelectNodes("Table");
foreach (XmlNode nodes in tablenodes)
{
if (nodes.LocalName == "tblAdminUser")
{
XmlNodeList Columnnodes = root.SelectNodes("Column"); // You can also use XPath here
XmlNodeList displayColumnnodes = root.SelectNodes("Column");
foreach (XmlNode node in displayColumnnodes)
{
Response.Write(node.InnerText);
// use node variable here for your beeds
}
}
}
Please help me.
Upvotes: 1
Views: 1710
Reputation: 6918
Here is my code to full fill my requirement :
using System;
using System.Xml;
using System.Web;
using System.Data;
public class ClsXML
{
public DataTable GetColumnsFromXML(String XMLPath, String TableName)
{
DataTable dtForColumns = DatatableforColumns();
XmlDataDocument xmldoc = new XmlDataDocument();
xmldoc.Load(XMLPath);
XmlElement root = xmldoc.DocumentElement;
XmlNodeList tablenodes = root.SelectNodes("Table");
if (tablenodes != null)
foreach (XmlNode nodes in tablenodes)
{
if (!nodes.HasChildNodes) continue;
if (nodes.Attributes == null) continue;
//TableName = nodes.Attributes[0].Value;
if (nodes.Attributes[0].Value == TableName)
{
String PrimaryKey = nodes.Attributes[1].Value;
var nodesdisplayname = nodes.SelectNodes("Column/DisplayColumn");
var nodesorignalvalue = nodes.SelectNodes("Column/OrignalColumn");
if (nodesdisplayname != null && nodesorignalvalue != null)
{
for (int i = 0; i <= nodesdisplayname.Count - 1; i++)
{
var xmlDisplayNode = nodesdisplayname.Item(i);
var xmlOrignalNode = nodesorignalvalue.Item(i);
if (xmlDisplayNode != null && xmlOrignalNode != null)
{
DataRow dr;
dr = dtForColumns.NewRow();
dr["DisplayColumn"] = xmlDisplayNode.InnerText;
dr["OrignalColumn"] = xmlOrignalNode.InnerText;
dr["PrimaryKey"] = PrimaryKey;
dtForColumns.Rows.Add(dr);
}
}
}
}
}
return dtForColumns;
}
private DataTable DatatableforColumns()
{
DataTable dt = new DataTable();
dt.Columns.Add("DisplayColumn", typeof(String));
dt.Columns.Add("OrignalColumn", typeof(String));
dt.Columns.Add("PrimaryKey", typeof(String));
return dt;
}
}
Thanks all happy code....
Upvotes: 1
Reputation: 2923
Seems like a better XPath statement would help out.
You have
root.SelectNodes("Table");
Change to
root.SelectNodes( "//Table[(@name='tblAdminUser')]/Column" )
You might consider XSLT, here's a sample
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match='/'>
<table>
<tr>
<th>Display Name</th>
<th>Original Value</th>
</tr>
<xsl:for-each select='//Table[(@name="tblAdminUser")]/Column'>
<tr>
<td><xsl:value-of select='displayname'/></td>
<td><xsl:value-of select='orignalvalue'/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2
Reputation: 482
Try This
XmlDataDocument xmldoc = new XmlDataDocument();
xmldoc.Load("C:/test.xml");
var root = xmldoc.DocumentElement;
if (root == null) return;
var users = new List<User>();
var tablenodes = root.SelectNodes("Table");
if (tablenodes != null)
foreach (XmlNode nodes in tablenodes)
{
if (!nodes.HasChildNodes) continue;
if (nodes.Attributes == null) continue;
var user = new User { UserName = nodes.Attributes[0].Value };
var customer = new Customer();
var nodesdisplayname = nodes.SelectNodes("Column/displayname");
if (nodesdisplayname != null)
{
var xmlNode = nodesdisplayname.Item(0);
if (xmlNode != null)
{
customer.DisplayName = xmlNode.InnerText;
}
}
var nodesorignalvalue = nodes.SelectNodes("Column/orignalvalue");
if (nodesorignalvalue != null)
{
var xmlNode = nodesorignalvalue.Item(0);
if (xmlNode != null)
{
customer.OriginalValue = xmlNode.InnerText;
}
}
user.Customers = customer;
users.Add(user);
}
Also add condition in above code as
if(nodes.Attributes[0].Value == "tblAdminUser")
{
// Added code here
}
And take user and customer class as example
public class User
{
public Customer Customers;
public string UserName { get; set; }
}
public class Customer
{
public string DisplayName { get; set; }
public string OriginalValue { get; set; }
}
It will return list of user.
I suppose it will help.
Upvotes: 2