I have an XML File:
<Database>
<SMS>
<Number>+447761692278</Number>
<DateTime>2009-07-27T15:20:32</DateTime>
<Message>Yes</Message>
<FollowedUpBy>Unassigned</FollowedUpBy>
<Outcome></Outcome>
<Quantity>0</Quantity>
<Points>0</Points>
</SMS>
<SMS>
<Number>+447706583066</Number>
<DateTime>2009-07-27T15:19:16</DateTime>
<Message>STOP</Message>
<FollowedUpBy>Unassigned</FollowedUpBy>
<Outcome></Outcome>
<Quantity>0</Quantity>
<Points>0</Points>
</SMS>
</Database>
Currently I read it into a datagridview using this:
public void Read()
{
DataSet ds = new DataSet("SMS DataSet");
XmlDataDocument xmlDatadoc = new XmlDataDocument();
xmlDatadoc.DataSet.ReadXml(#"C:\Documents and Settings\Administrator\Desktop\RecSmsDB.xml");
ds = xmlDatadoc.DataSet;
this.dataGridView1.DataSource = ds;
this.dataGridView1.DataMember = "SMS";
this.dataGridView1.Sort(dataGridView1.Columns["DateTime"], ListSortDirection.Descending);
}
I want to be able to only read in the xml objects that have a specific DateTime. Does anyone know of a way of doing this? Currently I have tried a variety of the methods included in the namespace but to no avail.
Help greatly appreciated,
regards.
***EDIT: I want to be able to dynamically change the data displayed at run time.
Not that I can think of. However, you can read all the data into the DataSet, then create a DataView which filters the SMS table, and bind your grid to that instead of the DataTable.
What if you attach the DataSet to a BindingSource and attach this BindingSource to the grid? A BindingSource has a Filter property where you can enter SQL-like expressions.
Related
my C# WinForms Application has 3 GridViews which I want to save in one XML file.
For one gridview I do it like this:
DataTable dst = new Datatable();
dst = (DataTable)grdViewBGM.DataSource;
dst.TableName = "GridView";
dst.WriteXml(folderPath + String.Format("\\{0}.xml", filename), true);
It may be simple to solve that but I don't have a clue except for making 3 xml files which is everything but perfect.
If someone could give me an advice that would be awesome, thanks.
The created file looks like this:
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<GridView>
<Genre>Wald</Genre>
<Pfad>C:\Users\Kong\Desktop\Beards\Wald</Pfad>
<Hotkey>1</Hotkey>
</GridView>
<GridView>
<Genre>Wald Nachts</Genre>
<Pfad>C:\Users\Kong\Desktop\Beards\Wald Nachts</Pfad>
<Hotkey>2</Hotkey>
</GridView>
</DocumentElement>
One possible way is to populate the Tables collection of a DataSet. It has a method to write to xml.
void MakeXML(string fileName)
{
DataSet ds = new DataSet();
foreach(DataGridView dgv in this.Controls.OfType<DataGridView>())
{
ds.Tables.Add((DataTable)dgv.DataSource);
}
ds.WriteXml(fileName);
}
If the DataGridView's aren't in the current form(this), use the name of whichever control contains them. If they are spread out, keep a global List<DataGridView> that you add to each time a DataGridView is created, then use that.
I am trying a to make a report viewer that's gonna show all the reports in the same viewer. I used to make individual report viewer for every RDLC. but that's really a long-process. And kinda stupid.
I have the dataSet in App_Code folder using Table Adapter and i want to take that dataset as the ReportDatasource and use cases for different reports. But I dont know how to. Whatever I got on the internet is doing it using SQL command. But I have the connection and stored procedures ready in DataSet. I want to use that dataset.
Very new to .NET developement, sorry if I am not clear.
Any kind of help is appreciated.
You should do something like:
DataSet ds = SomeMethodToRetrieveDataSet(); // e.g. via DataAdapter
// Set parameters,
ReportParameter[] parameters = new ReportParameter[...];
ReportDataSource reportDataSource = new ReportDataSource();
//match the DataSource in the RDLC
reportDataSource.Name = "ReportData";
reportDataSource.Value = ds.Tables[0];
// Addparameters to the collection
reportViewer1.LocalReport.SetParameters(parameters);
reportViewer1.LocalReport.DataSources.Add(reportDataSource);
reportViewer1.DataBind();
I have a class that goes to a URL and gets a xml document using xmlDoc.Load(URL). To test the class, I added a web project to display the xml in a grid view.
In a button click I create an instance of an xml document and populate it as:
xmlDoc = myClassName()
I'm stuck at how to get xmlDoc into a format usable by the datasource
I am totally confused as to how to get the xml to be displayed in the grid as dataset.ReadXml seems to want a file path. I don't understand the other overloads. I suppose I have to read the xml into a string or something else, but I don't understand how to do this - even after reading numerous posts here and MSDN - Thanks!
Example:
string xml =#"<xml><customer><id>1</id></customer></xml>";
DataSet ds = new DataSet();
ds.ReadXml(XmlReader.Create(new StringReader(xml)));
Now set the datasource to your grid:
grid.DataSource=newDataSet.Tables[0];
Update:
DataSet ds = new DataSet();
//xmlDocument is your XmlDocument instance
ds.ReadXml(XmlReader.Create(new StringReader(xmlDocument.InnerXml)));
grid.DataSource=newDataSet.Tables[0];
I have to load a response from an xml file into a dataset. I have written the following code in c#
XmlDocument doc = new XmlDocument();
doc.LoadXml(Response);
DataSet ds = new DataSet();
ds.ReadXml(new XmlNodeReader(doc));
DataTable EquoteRes = ds.Tables["EQuote"];
When I debug, I get this error:
A column named 'ChildNodes' already belongs to this DataTable:
cannot set a nested table name to the same name.
xml file is as below
How can I get rid of this error?
Very Similar discussion on the asp.net forum here.
And for more information msdn can also be referred.
Im using c# .net , windows form application. I have a XML file which contains two columns and some rows of data. now i have to fill this data into a data grid view. im using a button, when i click on the button an open dialog box will appear. i have to select the xml file name and when i click on open the contents of that xml file should come to the data grid view. i have tried with the following code:
{
XmlDataDocument xmlDatadoc=new XmlDataDocument();
XmlDatadoc.Dataset.ReadXml(filename);
ds=xmlDatadoc.Dataset;
datagridview1.DataSource=ds.DefaultViewManager;
datagridview1.Datamember="language";
}
My xml file is:
<languages>
<language>
<key> key1</key>
<value>value1</value>
</language>
<language>
<key> key2</key>
<value>value2</value>
</language>
</languages>
Its working fine but only for "language" . I need it to work file other xml files also.
This one's pretty simple: if you want to use this with a different XML file, use a different Datamember property, like so:
datagridview1.Datamember="taxes"; // or whatever
If you're expecting your DataGridView to somehow magically know what elements in your XML to use for rows, you're out of luck.
Hello i this is example, xml must be in correct format, its easy to correct code if needed, that's just the main gist of idea:
XElement xElement = XElement.Load("file.xml");
DataTable dTable = new DataTable();
// keys must have unique name
xElement.Elements().First().Elements().ToList()
.ForEach(element=>dTable.Columns.Add(element.Name.ToString()));
xElement.Elements().ToList().ForEach((item) =>
{
// fileds must place in the same order
// but you can correct it if you want
var itemToAdd = new List<string>();
item.Elements().ToList().ForEach(field => itemToAdd.Add(field.Value));
dTable.Rows.Add(itemToAdd.ToArray());
}
);
dataGridView1.DataSource = dTable;