Read XML file data to textbox - c#

I created an application with a listbox that loads a column with people's names, I would like to know how I can do it when I click on the person's name in the listbox it brings out the user.xml data and fills the textbox with the data of the nodes according to the name of the user clicked.
Xml file:
<user id="1">
<name>Louise</name>
<lastname>Maria</lastname>
<age>25</age>
</user>
<user id="2">
<name>John</name>
<lastname>Huster</lastname>
<age>33</age>
</user>
<user id="2">
<name>Linda</name>
<lastname>Blayer</lastname>
<age>19</age>
</user>
My code:
private void lsbUser_SelectedIndexChanged(object sender, EventArgs e)
{
var file = #"D:\User\user.xml";
using (XmlReader info = XmlReader.Create(file))
{
while (info.Read())
{
if (info.NodeType == XmlNodeType.Element && info.Name == "name")
{
txtName.Text = info.ReadElementContentAsString();
}
if (info.NodeType == XmlNodeType.Element && info.Name == "lastName")
{
txtLastName.Text = info.ReadElementContentAsString();
}
if (info.NodeType == XmlNodeType.Element && info.Name == "age")
{
txtAge.Text = info.ReadElementContentAsString();
}
}
}
}
Hello, I created an application with a listbox that loads a column with people's names, I would like to know how I can do it when I click on the person's name in the listbox it brings out the user.xml data and fills the textbox with the data of the nodes according to the name of the user clicked.

Related

Delete selected row from Datagridview and simultaneously XML node upon delete button click

At present the code deletes the row from Datgridview when user highlights entire row and selects delete button but it's not erased from XML file. So if I refresh page, the previously deleted row is still showing as grid view loads data from XML which it hasn't been deleted from.
Here is the code for the XML
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Contacts>
<FirstName>s</FirstName>
<LastName>s</LastName>
<Address>s</Address>
<City>s</City>
<Postcode>s</Postcode>
<Number>s</Number>
</Contacts>
<Contacts>
<FirstName>w</FirstName>
<LastName>w</LastName>
<Address>w</Address>
<City>w</City>
<Postcode>w</Postcode>
<Number>w</Number>
</Contacts>
<Contacts>
<FirstName>
</FirstName>
<LastName>
</LastName>
<Address>
</Address>
<City>
</City>
<Postcode>
</Postcode>
<Number>
</Number>
</Contacts>
<Contacts>
<FirstName>dkd</FirstName>
<LastName>dkfskldf</LastName>
<Address>asdfasf</Address>
<City>dfs</City>
<Postcode>dd</Postcode>
<Number>dd</Number>
</Contacts>
</NewDataSet>
Here is the code for the delete button event handler
private void button2_Click(object sender, EventArgs e)
{
string Lname;
DataGridViewSelectedRowCollection dgv_rc = this.dataGridView1.SelectedRows;
foreach (DataGridViewRow dgv_r in dgv_rc)
{
this.dataGridView1.Rows.Remove(dgv_r);
}
if (dataGridView1.SelectedCells.Count > 0)
{
int selectedrowindex = dataGridView1.SelectedCells[0].RowIndex;
DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];
Lname = Convert.ToString(selectedRow.Cells["LastName"].Value);
XmlDocument xdoc = new XmlDocument();
xdoc.Load("Contacts.xml");
foreach (XmlNode xNode in xdoc.SelectNodes("Contacts"))
if (xNode.SelectSingleNode("LastName").InnerText == Lname) xNode.ParentNode.RemoveChild(xNode);
xdoc.Save("Contacts.xml");
DialogResult dialog = MessageBox.Show(Lname + "deleted");
}
}
Any help would be greatly appreciated

how to filter a grid based on codtions and than bind the grid using xml

I have one text box and a button and a grid
textbox ID-->Client side---------------->txtFirstName
button Name-->Client side--------------->btnSearch
Grid id-->Client side------------------->gvContactUS
On click of button Search i am trying to search a record which i have entered in textbox whose name is is their in the xml file and will show that in the grid
my xml file is :Contact.xml
<CATALOG>
<CD>
<ID>1</ID>
<HeaderDetailID>1</HeaderDetailID>
<FirstName>Basant</FirstName>
<LastName>Gera</LastName>
<EmployeeID>0012</EmployeeID>
<Department>SD</Department>
<Postion>Programmer</Postion>
<Application>P and L - Joint Cost***BI_AP_KL_COST</Application>
<Filter>Africa_ww_READ</Filter>
<AreaorCountryorStation>India</AreaorCountryorStation>
<NetworkDomain>.Net</NetworkDomain>
<Action>
Modification
</Action>
<NameOfController>Nitin</NameOfController>
</CD>
</CATALOG>
I am trying to filter only 1 record which is their into the grid.
But i am getting the full grid.
Code as follows:
protected void btnSearch_Click(object sender, EventArgs e)
{
string FilepathContact;
string FirstName = txtFirstName.Text;
FilepathContact = Server.MapPath("~/Contact.xml");
DataSet dsdata = new DataSet();
dsdata.ReadXml(FilepathContact);
}
if (dsdata.Tables["CD"].Rows.Count != 0)
{
for (int i = 0; i < dsdata.Tables["CD"].Rows.Count; i++)
{
if (Convert.ToString(dsdata.Tables["CD"].Rows[i]["FirstName"]) == Convert.ToString(FirstName))
{
gvContactUS.DataSource = dsdata;
gvContactUS.DataBind();
}
}
}
}
Now I am getting all the records which is their in my grid.
How do i find the element and search if its their than bind the grid only with that record which is their in data set.
I am using .net Framework 2.0 so LINQ dont work her.

Select row from DataGridView and place it in a textbox

I have a windows form application which has a datagridview to display a menu full of meals.
I want to be able to allow the user to choose a seat number and then select a row and once the row is selected all the information in that row to be put in the available textbox below which will then be submitted in a new xml file titled order.xml.
So far I have the the saving to the xml part sorted, but I can't seem to get the datagridview task done.
Code to save the data in the order.xml file.
XDocument doc = XDocument.Load("order.xml");
XElement root = new XElement("MenuInfo");
root.Add(new XElement("Data1", dataGridView.DataSource));
root.Add(new XElement("Data2", _seat));
root.Add(new XElement("Data3", buttonTable1.Text));
root.Add(new XElement("Data4", lbPrice.Text));
doc.Element("Menu").Add(root);
doc.Save("order.xml");
MessageBox.Show("The order has been placed.");
You can react on the RowEnter-Event, which fires if the user enter a row. Do following then:
private void dataGridView1_RowEnter(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
{
var yourGrid = sender as DataGridView;
if (yourGrid != null)
{
var row = yourGrid.Rows[e.RowIndex].DataBoundItem as DataRowView;
if (row != null)
{
//Here you have access to the actual row.
//So simply get the string foreach column you need
string myStringForTextEdit = Convert.ToString(row["MealID"]) + Convert.ToString(row["Food"]) +
Convert.ToString(row["Price"]) + Convert.ToString(row["Time"]) +
Convert.ToString(row["Quantity"]);
myTextEdit.Text = myStringForTextEdit;
}
}
}
This simple example use the RowEnter-Event gets the current row and build a string based on all row Values.
I hope this helps.

Dynamic templates for listview/Radlistview

I am trying to accomplish the task of loading a template into a listview depending on the data coming in and what the text in the field "Type" has in it.
At the state i am at the templates will load and perform as needed but not on the record i am currently am working with, it will produce the template on the next record instead.
protected void RadListView1_OnItemDataBound(object sender, RadListViewItemEventArgs e)
{
if (e.Item.ItemType == RadListViewItemType.DataItem || e.Item.ItemType == RadListViewItemType.AlternatingItem)
{
RadListViewDataItem dataItem = (RadListViewDataItem)e.Item;
DataRowView rowView = (DataRowView)dataItem.DataItem;
string Keyword = rowView["Type"].ToString();
if (keyword == "anothertest")
{
RadListView1.ItemTemplate = LoadTemplate("Cash.ascx");
}
else if (keyword == "test")
{
RadListView1.ItemTemplate = LoadTemplate("Credits.ascx");
}
}
}
this is how i am currently achieving this, do i need to complete this method on something other than "onItemDataBound" to work on the current record? Thanks in advance.

LINQ to XML and DataGridView

Hi I’m trying to use a DataGridView for the first time and with LINQ.
Here’s what I’m trying to do:
I want to use it (although it doesn’t have to the DataGridView ) to read and display the contents of an XML file (this bit is working code below) but I want to have an add row button on the form or in the DataGridView that takes the contents of three textboxes and and populates the contents of three columns of the new row. The new row is to be appended to the existing data.
Next I would like to add a Delete button to delete the currently selected row.
And lastly I want a save button that exports the contents of the DataGridView back to an XML file overwriting/updating the existing XML file.
So I stuck at adding the additional data! but as I don't currently have a clue as to the delete or save either I thought I would ask all in one go!!
So this is the code I have to read the xml file:
XDocument xmlDoc = XDocument.Load(#"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
select new
{
QueueNumber = c.Element("Number").Value,
QueueName = c.Element("Name").Value,
QueuePCC = c.Element("QueueTag").Value
};
dataGridView1.DataSource = q.ToList();
XML document:
<?xml version="1.0" encoding="utf-8" ?>
<Queues>
<Queue>
<Number>
001
</Number>
<Name>
mytest
</Name>
<QueueTag>
xyz
</QueueTag>
</Queue>
<Queue>
<Number>
002
</Number>
<Name>
Adi2
</Name>
<QueueTag>
ABCD
</QueueTag>
</Queue>
</Queues>
ok I have now changed my code to this:
XDocument xmlDoc = XDocument.Load(#"queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
select new Queue
{
Number = c.Element("Number").Value,
Name = c.Element("Name").Value,
QueueTag= c.Element("QueueTag").Value
};
var queryAsList = new BindingList<Queue>(q.ToList());
bindingSource1.DataSource = queryAsList;
dataGridView1.DataSource = bindingSource1;
This allows me to add and delete rows and data from the dataGridView :)
But I can still find no way to writing the data back to XML either from the dataGridView or from the bindingSource1 :(
Any help please?
the bindingSource1.count changes each time I make a change to the data so I think I'm close!
Perhaps you may try working with the xmlDoc saved in the session (thus preserved with the postbacks)... Add the three elements (from the textboxes) to the XmlDoc... And rebind the gridview to the xmlDoc at each postback.
At the end, you could do xmlDoc.save().
For deleting rows, you could use the GridView event, dataGridView1_RowDeleted, to edit the xmlDoc (to remove the node relative to the selected row).
Something like:
protected void ButtonLoadGridView_Click(object sender, EventArgs e)
{
XDocument xmlDoc = XDocument.Load(#"f:\queues.xml");
var q = from c in xmlDoc.Root.Descendants("Queue")
select new
{
QueueNumber = c.Element("Number").Value,
QueueName = c.Element("Name").Value,
QueuePCC = c.Element("QueueTag").Value
};
dataGridView1.DataSource = q.ToList();
Session.Add("xmlDoc",xmlDoc);
}
public void dataGridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
{
// get some node information:
int rowIndex = e.RowIndex;
string someNodeInfo = dataGridView1.Rows[rowIndex].Cells[0].Text;
// then edit xml :
XmlDocument xmlDoc = (XmlDocument) Session["xmlDoc"];
xmlDoc.ChildNodes.Item(rowIndex).RemoveAll();
Session.Add("xmlDoc", xmlDoc);
}
protected void ButtonSave_Click(object sender, EventArgs e)
{
XmlDocument xmlDoc = (XmlDocument)Session["xmlDoc"];
xmlDoc.Save(#"f:\queues2.xml");
}

Categories