How to read multiple node element from xml data in c# - c#

I have xml like below. Here allowedSessionType can be multiple value sometime. Could anyone help me to read both and store it into data table c# gridview.
I tried but getting single value
<sessionManagementSubscriptionData>
<singleNssai>1-000001</singleNssai>
<dnnConfiguration>
<pduSessionTypes>
<defaultSessionType>IPV4</defaultSessionType>
<allowedSessionType>IPV4</allowedSessionType>
</pduSessionTypes>
<sscModes>
<defaultSscMode>SSC_MODE_1</defaultSscMode>
<allowedSscMode>SSC_MODE_1</allowedSscMode>
</sscModes>
</dnnConfiguration>
<dnnConfiguration>
<pduSessionTypes>
<defaultSessionType>IPV4</defaultSessionType>
<allowedSessionType>IPV4</allowedSessionType>
<allowedSessionType>IPV6</allowedSessionType>
</pduSessionTypes>
<sscModes>
<defaultSscMode>SSC_MODE_1</defaultSscMode>
<allowedSscMode>SSC_MODE_1</allowedSscMode>
</sscModes>
</dnnConfiguration>
</sessionManagementSubscriptionData>
DataTable dt1 = new DataTable();
dt1.Columns.Add("Allowed Session Type", typeof(string));
XmlNodeList nodeList1 = doc.SelectNodes("//sessionManagementSubscriptionData/dnnConfiguration");
foreach (XmlNode node1 in nodeList1)
{
DataRow dtrow1 = dt1.NewRow();
var SMSDDefaultSessionType = node1.SelectSingleNode("//defaultSessionType").InnerText;
dtrow1["Default Session Type"] = SMSDDefaultSessionType;
var SMSDallowedSessionType = node1.SelectSingleNode("//allowedSessionType").InnerText;
dtrow1["Allowed Session Type"] = SMSDallowedSessionType;
dt1.Rows.Add(dtrow1);
}
GridView2.DataSource = dt1;
GridView2.DataBind();

using System.Xml.Serialization;
XmlSerializer serializer = new XmlSerializer(typeof(SessionManagementSubscriptionData));
using (StringReader reader = new StringReader(xml))
{
var test = (SessionManagementSubscriptionData)serializer.Deserialize(reader);
//test.DnnConfiguration has your data
}
[XmlRoot(ElementName="pduSessionTypes")]
public class PduSessionTypes {
[XmlElement(ElementName="allowedSessionType")]
public List<string> AllowedSessionType;
}
[XmlRoot(ElementName="sscModes")]
public class SscModes {
[XmlElement(ElementName="defaultSscMode")]
public string DefaultSscMode;
[XmlElement(ElementName="allowedSscMode")]
public string AllowedSscMode;
}
[XmlRoot(ElementName="dnnConfiguration")]
public class DnnConfiguration {
[XmlElement(ElementName="pduSessionTypes")]
public PduSessionTypes PduSessionTypes;
[XmlElement(ElementName="sscModes")]
public SscModes SscModes;
}
[XmlRoot(ElementName="sessionManagementSubscriptionData")]
public class SessionManagementSubscriptionData {
[XmlElement(ElementName="singleNssai")]
public DateTime SingleNssai;
[XmlElement(ElementName="dnnConfiguration")]
public List<DnnConfiguration> DnnConfiguration;
}
Use this webiste to convert xml to c#

Here is results in a DataTable using Xml Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication11
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("Default Session Type", typeof(string));
dt.Columns.Add("Allowed Session Type", typeof(string));
dt.Columns.Add("Default SSC Mode", typeof(string));
dt.Columns.Add("Allowed SSC Mode", typeof(string));
XDocument doc = XDocument.Load(FILENAME);
foreach (XElement dnnConfiguration in doc.Descendants("dnnConfiguration"))
{
string defaultSessionType = (string)dnnConfiguration.Descendants("defaultSessionType").FirstOrDefault();
string[] allowedSessionType = dnnConfiguration.Descendants("allowedSessionType").Select(x => (string)x).ToArray();
string defaultSscMode = (string)dnnConfiguration.Descendants("defaultSscMode").FirstOrDefault();
string[] allowedSscMode = dnnConfiguration.Descendants("allowedSscMode").Select(x => (string)x).ToArray();
dt.Rows.Add(new object[] { defaultSessionType, string.Join(",", allowedSessionType), defaultSscMode, string.Join(",", allowedSscMode) });
}
}
}
}

Related

How to use yahoo Yql query to gather weather information using woeid number

I have a textbox where i want the user to input their woeid number but i am not sure how to add it to the query string, in the code below i can get the weather for Los Angeles, but what i want now is to get it by using the woeid number the user provides.
try
{
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='Los Angeles')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
var wData = new XmlDocument();
wData.Load(query);
var man = new XmlNamespaceManager(wData.NameTable);
man.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode channel = wData.SelectSingleNode("query").SelectSingleNode("results").SelectSingleNode("channel");
XmlNodeList nodes = wData.SelectNodes("query/results/channel");
MainForm.WindSpeed = channel.SelectSingleNode("yweather:wind", man).Attributes["speed"].Value;
MainForm.Town = channel.SelectSingleNode("yweather:location", man).Attributes["city"].Value;
MainForm.Temperature = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["temp"].Value;
MainForm.Condition = channel.SelectSingleNode("item").SelectSingleNode("yweather:condition", man).Attributes["text"].Value;
MainForm.Humidity = channel.SelectSingleNode("yweather:atmosphere", man).Attributes["humidity"].Value;
MainForm.TFCond = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["text"].Value;
MainForm.TFHigh = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["high"].Value;
MainForm.TFLow = channel.SelectSingleNode("item").SelectSingleNode("yweather:forecast", man).Attributes["low"].Value;
}
catch {}
}
Try xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string city = "Los Angeles";
string query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(1) where text='{0}')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", city);
XDocument wData = XDocument.Load(query);
XNamespace ns = wData.Root.GetDefaultNamespace();
XElement xWind = wData.Descendants().Where(x => x.Name.LocalName == "wind").FirstOrDefault();
int speed = (int)xWind.Attribute("speed");
XElement xLocation = wData.Descendants().Where(x => x.Name.LocalName == "location").FirstOrDefault();
string town = (string)xLocation.Attribute("city");
XElement xCondition = wData.Descendants().Where(x => x.Name.LocalName == "condition").FirstOrDefault();
int temp = (int)xCondition.Attribute("temp");
XElement xAtmosphere = wData.Descendants().Where(x => x.Name.LocalName == "atmosphere").FirstOrDefault();
int humidity = (int)xAtmosphere.Attribute("humidity");
List<XElement> xForecast = wData.Descendants().Where(x => x.Name.LocalName == "forecast").ToList(); ;
string tfCond = (string)xForecast.FirstOrDefault().Attribute("text");
int high = (int)xForecast.FirstOrDefault().Attribute("high");
int low = (int)xForecast.FirstOrDefault().Attribute("low");
}
}
}
Add the line
var woeid = Request.QueryString["woeid"];
then include it in your existing query like this:
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid = '{0}'&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", woeid);
I like solution below which handles multiple cities with the same name
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.Threading;
namespace WindowsFormsApplication23
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
string[] cities = { "Los Angeles", "New York", "Salem", "Portland", "Washington" };
DataTable dt = GetWeather(cities);
dataGridView1.DataSource = dt;
}
DataTable GetWeather(string[] cities)
{
DataTable dt = new DataTable();
dt.Columns.Add("City", typeof(string));
dt.Columns.Add("Region", typeof(string));
dt.Columns.Add("Humidity", typeof(int));
dt.Columns.Add("Wind Speed", typeof(int));
dt.Columns.Add("Temperature", typeof(int));
dt.Columns.Add("Condition", typeof(string));
dt.Columns.Add("High Temperature", typeof(int));
dt.Columns.Add("Low Temperature", typeof(int));
foreach (string inputCity in cities)
{
String query = String.Format("https://query.yahooapis.com/v1/public/yql?q=select * from weather.forecast where woeid in (select woeid from geo.places(100) where text='{0}')&format=xml&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys", inputCity);
XDocument wData = XDocument.Load(query);
XNamespace ns = wData.Root.GetDefaultNamespace();
//System.Threading.Thread.Sleep(10000);
foreach (XElement channel in wData.Descendants().Where(x => x.Name.LocalName == "channel"))
{
string city = "";
string region = "";
int? humidity = null;
int? speed = null;
int? temp = null;
string tfCond = "";
int? high = null;
int? low = null;
XNamespace yWeatherNs = channel.Elements().First().GetNamespaceOfPrefix("yweather");
XElement xLocation = channel.Element(yWeatherNs + "location");
if (xLocation == null)
{
continue;
}
else
{
city = (string)xLocation.Attribute("city");
region = (string)xLocation.Attribute("region");
}
XElement xAtmosphere = channel.Element(yWeatherNs + "atmosphere");
if (xAtmosphere != null)
{
humidity = (int)xAtmosphere.Attribute("humidity");
}
XElement xWind = channel.Element(yWeatherNs + "wind");
if (xWind != null)
{
speed = (int)xWind.Attribute("speed");
}
XElement item = channel.Element("item");
if (item != null)
{
XElement xCondition = item.Element(yWeatherNs + "condition");
if (xCondition != null)
{
temp = (int)xCondition.Attribute("temp");
}
List<XElement> xForecast = item.Elements(yWeatherNs + "forecast").ToList(); ;
if (xForecast != null)
{
tfCond = (string)xForecast.FirstOrDefault().Attribute("text");
high = (int)xForecast.FirstOrDefault().Attribute("high");
low = (int)xForecast.FirstOrDefault().Attribute("low");
}
}
dt.Rows.Add(new object[] { city, region, humidity, speed, temp, tfCond, high, low });
}
}
dt = dt.AsEnumerable().OrderBy(x => x.Field<string>("City")).ThenBy(x => x.Field<string>("Region")).CopyToDataTable();
return dt;
}
}
}

C# dataGridView from XML file

I know there are lots of resources that show how to bind dataGridViews to XML files or populate from XML, but the XML i'm using as source is a bit more complex than any example I've seen used and I'm struggling. (although it really shouldn't be difficult)
I basically need to run several queries (I think) to get the data I want to populate the DGV, because the elements I want the content from are on different parent nodes of the XML.
Here is what I have, and the comments should show you what I'm trying to achieve:
XDocument xmlDoc = XDocument.Load("Techdocx_dml.xml");
var q = from c in xmlDoc.Root.Descendants("dmentry")
.Descendants("avee")
//.Descendants("dmtitle") I also need to access this descendant
select new
{
modelic = c.Element("modelic").Value,
sdc = c.Element("sdc").Value,
chapnum = c.Element("chapnum").Value,
section = c.Element("section").Value,
subsect = c.Element("subsect").Value,
subject = c.Element("subject").Value,
discode = c.Element("discode").Value,
discodev = c.Element("discodev").Value,
incode = c.Element("incode").Value,
incodev = c.Element("incodev").Value,
itemloc = c.Element("itemloc").Value,
// techname = c.Element("techname").Value,
//need this value, which is on the "dmtitle" node, not the "avee" node
};
dataGridView1.DataSource = q.ToList();
dataGridView1.ColumnHeadersVisible = false;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
This is what i want in my dataGridView:
AA A 32 3 5 00 01 A 018 A A | Some title 1 | Introduction
AA A 32 3 5 00 01 A 920 A A | Some title 2 | Some infoname 2
How do I achieve this please? Example XML below:
<dml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<dmentry>
<addresdm>
<dmc>
<avee>
<modelic>AA</modelic>
<sdc>A</sdc>
<chapnum>32</chapnum>
<section>3</section>
<subsect>5</subsect>
<subject>00</subject>
<discode>01</discode>
<discodev>A</discodev>
<incode>018</incode>
<incodev>A</incodev>
<itemloc>A</itemloc>
</avee>
</dmc>
<dmtitle>
<techname>Some title 1</techname>
<infoname>Introduction</infoname>
</dmtitle>
<issno issno="001" type="New"/>
<issdate year="2016" month="06" day="10"/>
<language language="SX" country="GB"/>
</addresdm>
<security class="1"/>
</dmentry>
<dmentry>
<addresdm>
<dmc>
<avee>
<modelic>AA</modelic>
<sdc>A</sdc>
<chapnum>32</chapnum>
<section>3</section>
<subsect>5</subsect>
<subject>00</subject>
<discode>01</discode>
<discodev>A</discodev>
<incode>920</incode>
<incodev>A</incodev>
<itemloc>A</itemloc>
</avee>
</dmc>
<dmtitle>
<techname>Some title 2</techname>
<infoname>Some infoname 2</infoname>
</dmtitle>
<issno issno="001" type="New"/>
<issdate year="2016" month="06" day="10"/>
<language language="SX" country="GB"/>
</addresdm>
<security class="1"/>
</dmentry>
</dml>
Try this :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
const string FILENAME = #"c:\temp\test.xml";
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("modelic", typeof(string));
dt.Columns.Add("sdc", typeof(string));
dt.Columns.Add("chapnum", typeof(string));
dt.Columns.Add("section", typeof(string));
dt.Columns.Add("subsect", typeof(string));
dt.Columns.Add("subject", typeof(string));
dt.Columns.Add("discode", typeof(string));
dt.Columns.Add("discodev", typeof(string));
dt.Columns.Add("incode", typeof(string));
dt.Columns.Add("incodev", typeof(string));
dt.Columns.Add("itemloc", typeof(string));
dt.Columns.Add("techname", typeof(string));
dt.Columns.Add("infoname", typeof(string));
XDocument doc = XDocument.Load(FILENAME);
foreach (XElement addresdm in doc.Descendants().Where(x => x.Name.LocalName == "addresdm"))
{
XElement avee = addresdm.Descendants("avee").FirstOrDefault();
XElement dmtitle = addresdm.Descendants("dmtitle").FirstOrDefault();
dt.Rows.Add(new object[] {
(string)avee.Element("modelic"),
(string)avee.Element("sdc"),
(string)avee.Element("chapnum"),
(string)avee.Element("section"),
(string)avee.Element("subsect"),
(string)avee.Element("subject"),
(string)avee.Element("discode"),
(string)avee.Element("discodev"),
(string)avee.Element("incode"),
(string)avee.Element("incodev"),
(string)avee.Element("itemloc"),
(string)dmtitle.Element("techname"),
(string)dmtitle.Element("infoname")
});
}
dataGridView1.DataSource = dt;
}
}
}

LINQ algorithm to create generic table from XML

Here is a piece of code:
XNamespace z = "#SomeSchema";
var listCols = new HashSet<Col>();
var colNameList = new List<string>(..some values..);
var xElementList = doc.Descendants(z + "row");
return new HashSet<Row>(xElementList .Select(x=> new Row
{
Col= new List<Col>(listCols).Select(col =>
{
col.Value= (string)x.Attribute(colNameList.First(colName=> colName == col.Name));
return col;
}).ToList()
}));
What is wrong is that, the return value contains a List of Row, but all of these rows have the exact same value (for the Col Value).
Ex, Row[1].Col[1].Value == Row[2].Col[2].Value
And these values should be completly different. I am obtaining those values from an Xml file. When I debug the xElementList, values are différents, but when I try to create rows with them, all rows are the same.
Actually, the Rows have the same Columns list, which is the last record of the xElementList.
Am I doing something wrong?
Thank you.
See code below. I read the xml twice. Once to get the columns names and add columns to table. Then read xml 2nd time to get row data.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
StreamReader sReader = new StreamReader(FILENAME, Encoding.GetEncoding(1252));
XmlReader reader = XmlReader.Create(sReader);
Dictionary<string, string> colDict = new Dictionary<string, string>();
while (!reader.EOF)
{
if (reader.Name != "FIELD")
{
reader.ReadToFollowing("FIELD");
}
if (!reader.EOF)
{
XElement field = (XElement)XElement.ReadFrom(reader);
string attrname = (string)field.Attribute("attrname");
string fieldtype = (string)field.Attribute("fieldtype");
switch (fieldtype)
{
case "string":
dt.Columns.Add(attrname, typeof(string));
break;
case "i4":
dt.Columns.Add(attrname, typeof(int));
break;
}
colDict.Add(attrname, fieldtype);
}
}
reader.Close();
sReader = new StreamReader(FILENAME, Encoding.GetEncoding(1252));
reader = XmlReader.Create(sReader);
while (!reader.EOF)
{
if (reader.Name != "ROW")
{
reader.ReadToFollowing("ROW");
}
if (!reader.EOF)
{
XElement row = (XElement)XElement.ReadFrom(reader);
DataRow newRow = dt.Rows.Add();
foreach (XAttribute attrib in row.Attributes())
{
string colName = attrib.Name.LocalName;
if (colDict.ContainsKey(colName))
{
switch (colDict[colName])
{
case "string":
newRow[colName] = (string)attrib;
break;
case "i4":
newRow[colName] = (int)attrib;
break;
}
}
}
}
}
}
}
}

Need advice on using XML as database C#

I am making an application that stores data that user inputs.
http://i827.photobucket.com/albums/zz200/ArnasG/question_in_stackowerflow__zps4f7uy3l7.png
I have a bit of an issue because of my lack of experience, I want to save all the data user inputs in XML file and to load it when program starts next time. I had an idea to use dataset to read all the data from XML file and then work with the table[0] of that dataset(add/delete rows). It turn out that I can not make it to work properly. It loads some blank lines and lines that I created in previous tries, but there is only two lines that are actually saved in XML file. How could I make this work?
Thank you for your time :)
Actual XML file:
http://i827.photobucket.com/albums/zz200/ArnasG/question_in_stackowerflow_V2_zpshmwjnllr.png
DataSet ListOfTrades = new DataSet();
DataTable Lentele = new DataTable();
ListOfTrades.Tables.Add(Lentele);
// adding columns to the table
try
{
DataColumn Pair = new DataColumn("Pair", typeof(string));
Pair.AllowDBNull = false;
DataColumn Entry = new DataColumn("Entry", typeof(string));
Entry.AllowDBNull = false;
DataColumn StopLoss = new DataColumn("StopLoss", typeof(string));
StopLoss.AllowDBNull = false;
DataColumn TakeProfit = new DataColumn("TakeProfit", typeof(string));
TakeProfit.AllowDBNull = false;
DataColumn TakeProfit1 = new DataColumn("TakeProfit1", typeof(string));
TakeProfit1.AllowDBNull = false;
DataColumn TakeProfit2 = new DataColumn("TakeProfit2", typeof(string));
TakeProfit2.AllowDBNull = false;
DataColumn TakeProfit3 = new DataColumn("TakeProfit3", typeof(string));
TakeProfit3.AllowDBNull = false;
DataColumn LongShort = new DataColumn("LongShort", typeof(string));
LongShort.AllowDBNull = false;
DataColumn WinLoss = new DataColumn("WinLoss", typeof(string));
WinLoss.AllowDBNull = false;
data.Tables[0].Columns.AddRange(new DataColumn[] {
Pair, Entry, StopLoss, TakeProfit, TakeProfit1, TakeProfit2,
TakeProfit3, LongShort, WinLoss
});
}
catch(Exception Ex)
{
MessageBox.Show(Ex.Message);
}
// Adding new line to the table after user clicks save button
private void button1_Click(object sender, EventArgs e)
{
DataRow eilute = ListOfTrades.Tables[0].NewRow();
eilute[0] = comboBox1.Text.ToString();
eilute[1] = textBox1.Text.ToString();
eilute[2] = textBox2.Text.ToString();
eilute[3] = textBox3.Text.ToString();
eilute[4] = textBox4.Text.ToString();
eilute[5] = textBox5.Text.ToString();
eilute[6] = textBox6.Text.ToString();
if (radioButton1.Checked) { eilute[7] = "Long"; }
else { eilute[7] = "short"; }
if (radioButton1.Checked) { eilute[8] = "Win"; }
else { eilute[8] = "Loss"; }
ListOfTrades.Tables[0].Rows.Add(eilute);
ListOfTrades.Tables[0].WriteXml(DefaultPathToJournalXML);
dataGridView1.Update();
dataGridView1.Refresh();
}
Not getting duplicated. here is the xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Table1>
<Pair>AUD/USD</Pair>
<Entry>0.00000</Entry>
<StopLoss>0.00000</StopLoss>
<TakeProfit>0.00000</TakeProfit>
<TakeProfit1>0.00000</TakeProfit1>
<TakeProfit2>0.00000</TakeProfit2>
<TakeProfit3>0.00000</TakeProfit3>
<LongShort>short</LongShort>
<WinLoss>loss</WinLoss>
</Table1>
<Table1>
<Pair>AUD/USD</Pair>
<Entry>0.00000</Entry>
<StopLoss>0.00000</StopLoss>
<TakeProfit>0.00000</TakeProfit>
<TakeProfit1>0.00000</TakeProfit1>
<TakeProfit2>0.00000</TakeProfit2>
<TakeProfit3>0.00000</TakeProfit3>
<LongShort>short</LongShort>
<WinLoss>Loss</WinLoss>
</Table1>
</NewDataSet>
​
Here is code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
DataSet ds = new DataSet();
ds.ReadXml(FILENAME);
}
}
}
​
Think object oriented, and think Linq.
For example, let's say you have this XML file:
<trades>
<trade>
<pair>some pair</pair>
<stop-loss>stop loss 1</stop-loss>
</trade>
<trade>
<pair>other pair</pair>
<stop-loss>stop loss 2</stop-loss>
</trade>
</trades>
You can create a Trade class to hold the data in the trade tags and then use a Linq query to populate the object given the XML file:
class Trade
{
public string Pair;
public string StopLoss;
// Other variables from trade tag would go here...
// Function that can load trade objects from XML file into a list of Trade objects (List<Trade>)
public static List<Trade> loadTrade(string xmlFilePath)
{
// Load your XML document given the path to the .xml file
var doc = XDocument.Load(xmlFilePath);
// For each trade element in the trades element
var trades = (from trade in doc.Element("trades").Elements("trade")
select new Trade
{
// For each element in the trade element, put value in class variable
Pair = trade.Element("pair").Value,
StopLoss = trade.Element("stop-loss").Value
}).ToList<Trade>();
return trades;
}
}
When you're ready to save to a file you basically do the opposite of the Linq query to create an XML file. It will look very similar.
On there other hand, read this article and consider if there's a better alternative.

Using Reflection to create a DataTable from a Class?

I've just learned about Generics and I'm wondering whether I can use it to dynamically build datatables from my classes.
Or I might be missing the point here.
Here is my code, what I'm trying to do is create a datatable from my existing class and populate it. However I'm getting stuck in my thought process.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
namespace Generics
{
public class Dog
{
public string Breed { get; set; }
public string Name { get; set; }
public int legs { get; set; }
public bool tail { get; set; }
}
class Program
{
public static DataTable CreateDataTable(Type animaltype)
{
DataTable return_Datatable = new DataTable();
foreach (PropertyInfo info in animaltype.GetProperties())
{
return_Datatable.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
return return_Datatable;
}
static void Main(string[] args)
{
Dog Killer = new Dog();
Killer.Breed = "Maltese Poodle";
Killer.legs = 3;
Killer.tail = false;
Killer.Name = "Killer";
DataTable dogTable = new DataTable();
dogTable = CreateDataTable(Dog);
//How do I continue from here?
}
}
}
Now At the DataTable point it errors.
Also, being new to reflection and Generics, how will I actually populate the data with the Killer class?
Building up on all the previous answers, here is a version that creates a DataTable from any collection:
public static DataTable CreateDataTable<T>(IEnumerable<T> list)
{
Type type = typeof(T);
var properties = type.GetProperties();
DataTable dataTable = new DataTable();
dataTable.TableName = typeof(T).FullName;
foreach (PropertyInfo info in properties)
{
dataTable.Columns.Add(new DataColumn(info.Name, Nullable.GetUnderlyingType(info.PropertyType) ?? info.PropertyType));
}
foreach (T entity in list)
{
object[] values = new object[properties.Length];
for (int i = 0; i < properties.Length; i++)
{
values[i] = properties[i].GetValue(entity);
}
dataTable.Rows.Add(values);
}
return dataTable;
}
Here is a more compact version of David's answer that is also an extension function. I've posted the code in a C# project on Github.
public static class Extensions
{
public static DataTable ToDataTable<T>(this IEnumerable<T> self)
{
var properties = typeof(T).GetProperties();
var dataTable = new DataTable();
foreach (var info in properties)
dataTable.Columns.Add(info.Name, Nullable.GetUnderlyingType(info.PropertyType)
?? info.PropertyType);
foreach (var entity in self)
dataTable.Rows.Add(properties.Select(p => p.GetValue(entity)).ToArray());
return dataTable;
}
}
I have found that this works very well in conjunction with code to write a DataTable to CSV.
my favorite homemade function. it create and populate all at same time. throw any object.
public static DataTable ObjectToData(object o)
{
DataTable dt = new DataTable("OutputData");
DataRow dr = dt.NewRow();
dt.Rows.Add(dr);
o.GetType().GetProperties().ToList().ForEach(f =>
{
try
{
f.GetValue(o, null);
dt.Columns.Add(f.Name, f.PropertyType);
dt.Rows[0][f.Name] = f.GetValue(o, null);
}
catch { }
});
return dt;
}
The error can be resolved by changing this:
dogTable = CreateDataTable(Dog);
to this:
dogTable = CreateDataTable(typeof(Dog));
But there are some caveats with what you're trying to do. First, a DataTable can't store complex types, so if Dog has an instance of Cat on it, you won't be able to add that as a column. It's up to you what you want to do in that case, but keep it in mind.
Second, I would recommend that the only time you use a DataTable is when you're building code that knows nothing about the data its consuming. There are valid use cases for this (e.g. a user-driven data mining tool). If you already have the data in the Dog instance, just use it.
Another little tidbit, this:
DataTable dogTable = new DataTable();
dogTable = CreateDataTable(Dog);
can be condensed to this:
DataTable dogTable = CreateDataTable(Dog);
Here is a little bit modified code, which fixed time zone issue for datatime fields:
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
if (props[i].PropertyType == typeof(DateTime))
{
DateTime currDT = (DateTime)props[i].GetValue(item);
values[i] = currDT.ToUniversalTime();
}
else
{
values[i] = props[i].GetValue(item);
}
}
table.Rows.Add(values);
}
return table;
}
Here's a VB.Net version that creates a data table from a generic list passed to the function as an object. There is also a helper function (ObjectToDataTable) that creates a data table from an object.
Imports System.Reflection
Public Shared Function ListToDataTable(ByVal _List As Object) As DataTable
Dim dt As New DataTable
If _List.Count = 0 Then
MsgBox("The list cannot be empty. This is a requirement of the ListToDataTable function.")
Return dt
End If
Dim obj As Object = _List(0)
dt = ObjectToDataTable(obj)
Dim dr As DataRow = dt.NewRow
For Each obj In _List
dr = dt.NewRow
For Each p as PropertyInfo In obj.GetType.GetProperties
dr.Item(p.Name) = p.GetValue(obj, p.GetIndexParameters)
Next
dt.Rows.Add(dr)
Next
Return dt
End Function
Public Shared Function ObjectToDataTable(ByVal o As Object) As DataTable
Dim dt As New DataTable
Dim properties As List(Of PropertyInfo) = o.GetType.GetProperties.ToList()
For Each prop As PropertyInfo In properties
dt.Columns.Add(prop.Name, prop.PropertyType)
Next
dt.TableName = o.GetType.Name
Return dt
End Function
Using the answer provided by #neoistheone I've changed the following sections. Works fine now.
DataTable dogTable = new DataTable();
dogTable = CreateDataTable(typeof(Dog));
dogTable.Rows.Add(Killer.Breed, Killer.Name,Killer.legs,Killer.tail);
foreach (DataRow row in dogTable.Rows)
{
Console.WriteLine(row.Field<string>("Name") + " " + row.Field<string>("Breed"));
Console.ReadLine();
}
you can convert the object to xml then load the xml document to a dataset, then extract the first table out of the data set. However i dont see how this be practical as it infers creating streams, datasets & datatables and using converstions to create the xml document.
I guess for proof of concept i can understand why. Here is an example, but somewhat hesitant to use it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data;
using System.Xml.Serialization;
namespace Generics
{
public class Dog
{
public string Breed { get; set; }
public string Name { get; set; }
public int legs { get; set; }
public bool tail { get; set; }
}
class Program
{
public static DataTable CreateDataTable(Object[] arr)
{
XmlSerializer serializer = new XmlSerializer(arr.GetType());
System.IO.StringWriter sw = new System.IO.StringWriter();
serializer.Serialize(sw, arr);
System.Data.DataSet ds = new System.Data.DataSet();
System.Data.DataTable dt = new System.Data.DataTable();
System.IO.StringReader reader = new System.IO.StringReader(sw.ToString());
ds.ReadXml(reader);
return ds.Tables[0];
}
static void Main(string[] args)
{
Dog Killer = new Dog();
Killer.Breed = "Maltese Poodle";
Killer.legs = 3;
Killer.tail = false;
Killer.Name = "Killer";
Dog [] array_dog = new Dog[5];
Dog [0] = killer;
Dog [1] = killer;
Dog [2] = killer;
Dog [3] = killer;
Dog [4] = killer;
DataTable dogTable = new DataTable();
dogTable = CreateDataTable(array_dog);
// continue here
}
}
}
look the following example here
If you want to set columns order/ Include only some columns/ exclude some columns try this:
private static DataTable ConvertToDataTable<T>(IList<T> data, string[] fieldsToInclude = null,
string[] fieldsToExclude = null)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
foreach (PropertyDescriptor prop in properties)
{
if ((fieldsToInclude != null && !fieldsToInclude.Contains(prop.Name)) ||
(fieldsToExclude != null && fieldsToExclude.Contains(prop.Name)))
continue;
table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
}
foreach (T item in data)
{
var atLeastOnePropertyExists = false;
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
if ((fieldsToInclude != null && !fieldsToInclude.Contains(prop.Name)) ||
(fieldsToExclude != null && fieldsToExclude.Contains(prop.Name)))
continue;
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
atLeastOnePropertyExists = true;
}
if(atLeastOnePropertyExists) table.Rows.Add(row);
}
if (fieldsToInclude != null)
SetColumnsOrder(table, fieldsToInclude);
return table;
}
private static void SetColumnsOrder(DataTable table, params String[] columnNames)
{
int columnIndex = 0;
foreach (var columnName in columnNames)
{
table.Columns[columnName].SetOrdinal(columnIndex);
columnIndex++;
}
}

Categories