Saving Xml to a Document C# - c#

Unlike what I've been able to find on here I wand to maintain syntax within my xml document, and serialization doesn't touch on that. I want to be able to add another "task" tag to the xml document...Loading the information isn't a problem, I've had to deal with that before... but this is.
Main Program:
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.Xml.Serialization;
using System.IO;
namespace ToDoList
{
public partial class Form1 : Form
{
string title; //the variable for the title textbox value to be stored in
string details; //the variable for the details textbox value to be stored in
string itemstr; //the variable for title and details to be merged in
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
optionsbtn.Text = "Options"; //make the options button's text options
var items = ToDochkbox.Items; //create a private "var" items symbolizing the Checkbox's items array
XDocument xmlDoc = XDocument.Load("tasksdoc.xml"); //load the xml document (in bin or release)
var q = from c in xmlDoc.Descendants("root") //go "within" the <root> </root> tag in the file
select (string)c.Element("task"); //find the first <task></task> tag
foreach (string N in q) //now cycle through all the <task></task> tags and per cycle save them to string "N"
{
items.Add(N); //add the item to the checkbox list
}
}
public void addbtn_Click(object sender, EventArgs e)
{
var items = ToDochkbox.Items; //create a private "var" items symbolizing the Checkbox's items array
title = Addtb.Text; //set the title string to equal the title textbox's contents
details = detailstb.Text; //set the details string to equal the detail textbox's contents
itemstr = title +" - " + details; //set a variable to equal the title string, a - with spaces on each end, and then the details string
items.Add(itemstr); //add the variable itemstr (above) to the the checkbox list
}
private void optionsbtn_Click(object sender, EventArgs e)
{
new options().Show();//show the options form
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
new options().Show();//show the options form
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
}
public void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
optionsbtn.Text = "Options"; //make the options button's text options
var items = ToDochkbox.Items; //create a private "var" items symbolizing the Checkbox's items array
XDocument xmlDoc = XDocument.Load("tasksdoc.xml"); //load the xml document (in bin or release)
var q = from c in xmlDoc.Descendants("root") //go "within" the <root> </root> tag in the file
select (string)c.Element("task"); //find the first <task></task> tag
foreach (string N in q) //now cycle through all the <task></task> tags and per cycle save them to string "N"
{
items.Add(N); //add the item to the checkbox list
}
}
}
}
And My XML Document:
<root>
<task>First Task - Create a Task</task>
</root>

The class that you could use to serialize:
public class MyClass
{
[XmlElement("task")]
public List<string> Tasks { get; set; }
}
Placing the XmlElementAttribute on a collection type will cause each element to be serialized without being placed a node for the list.
Xml with XmlElementAttribute:
<root>
<task>First Task - Create a Task</task>
<task>SecondTask - Create a Task</task>
<task>ThirdTask - Create a Task</task>
</root>
Xml without XmlElementAttribute:
<root>
<Tasks>
<Task>First Task - Create a Task</Task>
<Task>SecondTask - Create a Task</Task>
<Task>ThirdTask - Create a Task</Task>
</Tasks>
</root>
I answered another question about serializing lists in a similar way a few days ago. Check out his question and then the answer, it might be what you are trying to do.

Related

Cant get Innertexts from webpage using html agility pack xpath in c#

So this is my code guys.
Im trying to get the text inside a span and storage it locally. Im using html agility pack and trying to retrieve the text using xpath but the nodes dont retrieve anything and appear as null.
This is the page im trying to get the text from: https://siat.sat.gob.mx/app/qr/faces/pages/mobile/validadorqr.jsf?D1=10&D2=1&D3=15030267855_SDS150309FC7
Specifically the "Denominación o razón social" text.
namespace ObtencionDatosSatBeta
{
public partial class Form1 : Form
{
DataTable table;
public Form1()
{
InitializeComponent();
}
private void InitTable()
{
table = new DataTable("tabladedatosTable");
table.Columns.Add("Variable", typeof(string));
table.Columns.Add("Contenido", typeof(string));
//table.Rows.Add("Super Mario 64", "84%");
tabladedatos.DataSource = table;
}
private async void Form1_Load(object sender, EventArgs e)
{
InitTable();
HtmlWeb web = new HtmlWeb();
var doc = await Task.Factory.StartNew(() => web.Load("https://siat.sat.gob.mx/app/qr/faces/pages/mobile/validadorqr.jsf?D1=10&D2=1&D3=15030267855_SDS150309FC7"));
var nodes = doc.DocumentNode.SelectNodes("//*[#id=\"ubicacionForm: j_idt12:0:j_idt13: j_idt17_data\"]//tr//td//span");
var innerTexts = nodes.Select(node => node.InnerText);
}
private void tabladedatos_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
Any idea?
var nodes = doc.DocumentNode.SelectNodes("//*[#id=\"ubicacionForm: j_idt12:0:j_idt13: j_idt17_data\"]//tr//td//span");
The line of code above is the one that appears as null.
Use this Xpath which gets the first span under the element with the following ID: ubicacionForm:j_idt10:0:j_idt11:j_idt14_data
(//*[#id='ubicacionForm:j_idt10:0:j_idt11:j_idt14_data']//span)[1]
You can select the element using multiple different ways by copying the HTML in chrome (Ctrl + Option + J)
And then paste the HTML in Xpather where you can play around with your Xpath. Xpather.com

C# CheckedListBox how to manipulate checked data?

Hey guys new to C# and I am trying to setup a GUI, all I want the GUI to do is have a simple file explorer with a CheckedListBox to represent selected files.
I can get the CheckedListBox to show up and click on files but I'm not sure how to continue from here, most tutorials stop here, or go too advanced with tree view and other things that seem unnecessary for what I am trying to do.
Here is my code:
Any help is appreciated and if you guys could point me in the right direction that would be awesome.
EDIT:
To rephrase my question:
I want the user to select files through the CheckedListBox (user input stops here), and for those selected files to be put in a list that my code can manipulate.
Not sure how to accomplish this after my first foreach loop (which adds all files in the selected directory to the CheckedListBox for user selection).
The second foreach loop is an attempt at this, manipulating the files so that they output their filenames after being selected. However no Messagebox shows up and I assume that their is a disconnect between the user selecting files and the codes attempt at manipulating said files.
Second Edit:
I think I figured it out I made a second button and from here it looks like I can manipulate the chosen files however I want.
Currently the code is working the way I would expect it to work.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace SelectFiles
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
checkedListBox1.CheckOnClick = true;
}
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MessageBox.Show(fbd.SelectedPath);
checkedListBox1.Items.Clear();
string[] files = Directory.GetFiles(fbd.SelectedPath);
foreach (string file in files)
{
checkedListBox1.Items.Add(file);
}
}
private void button2_Click_1(object sender, EventArgs e)
{
List<string> list_all_excelfiles = new List<string>();
foreach (string item in checkedListBox1.CheckedItems)
{
list_all_excelfiles.Add(item);
MessageBox.Show(Path.GetFileName(item));
}
}
}
}
First i advice you to assign Value member and Display member for each item.
Display Member will be visible to user
Value Member will we use in code
To do this first create simple custom class
public class Int_String
{
public int _int { get; set; }
public string _string { get; set; }
}
Be careful because get; set; part is important to be there since if not code will not work
Now what you need to do is create list of items with custom class like this
class YourForm : Form
{
List<Int_String> myList = new List<Int_String>(); //Create list of our custom class
public YourForm()
{
PopulateMyList();
}
private void PopulateMyList()
{
//Here read from database or get data somehow and populate our list like this
//I will populate it manually but you do it in foreach loop
myList.Add(new Int_String { _int = 0, _string = "First Item" });
myList.Add(new Int_String { _int = 1, _string = "Second Item" });
myList.Add(new Int_String { _int = 2, _string = "Third Item" });
}
}
After that you need to assign this list to your checkedListBox which you will do like this:
public YourForm()
{
PopulateMyList();
checkedListBox1.DataSource = myList;
checkedListBox1.DisplayMember = "_string";
checkedListBox1.ValueMember = "_int";
}
And now when you can manipulate with checked items like this:
for(int i = 0; i < checkedListBox1.Items.Count; i++)
{
if(checkedListBox1.Items[i].CheckedState == CheckState.Checked)
{
int itemValueMember = (checkedListBox1.Items[i] as Int_String)._int;
int itemDisplayMember = (checkedListBox1.Items[i] as Int_String)._string;
//Use these two vars for whatever you need
}
}
TWO IMPORTANT TIPS:
I am not sure for this one since i am writing all this from head but i think that visual studio will not show you that there is DisplayMember or ValueMember for checkedBox component BUT also it will not show error. Reason is that they have hidden in intentionally for idk what reason but it will work.
You are able to assign Display and Value member to a lot of components in winforms BUT for some reason checkedListBox is specific. It is specific because you MUST first assign DataSource to it and then tell it checkedListBox.DisplayMember = "_string" ...... For new guy you will ask why it is important. Simple answer is create custom list for test and add 10k items inside it and then first declare datasource and after it Display and Value member. Test how long form will need to load (get out of freeze state). After that do everything same but first declare Display and Value member and then assign datasource and test again. I am telling this from head without testing but before when i needed about 5k rows with 1st solution it took me about 30 sec and second < 1 sec. If you want to know more about it google it but for now this is pretty much info for you.

How to save a listview of data to a .txt in C# so it can be accessed the next time the form is open?

How can I save the the data in a listview?
For example if the user has put something into the listview and then closes the application, all that information is then lost. Is there any way of saving the information as soon as they put something into the listView?
This is the code I use to input the information from a textbox to the listView1:
string[] items = { titletxt.Text, statustxt.Text };
ListViewItem lvi = new ListViewItem(items);
listView1.Items.Add(lvi);
UPDATE (29/12/2012)
Thanks for all the help! But I can't seem to get it working. I have created a really simple form to try and get it working. It has 3 textboxes (NameTxt, AgeTxt, HairColourTxt), a button, and the listview(ListOfPeople).
Here is the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace listview
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] items = { NameTxt.Text, AgeTxt.Text, HairColourTxt.Text };
ListViewItem lvi = new ListViewItem(items);
ListOfPeople.Items.Add(lvi);
}
}
}
I have tried to add the code posted by #user574632 & #Joe & #DmitryKvochkin and I have changed bits but still cant get it to work. I'm not sure what I'm doing wrong? I have tried to add this:
private void saveListOfPeopleItems(string path, ListView lv)
{
var delimeteredListviewData = new List<string>();
foreach (ListViewItem lvi in lv.Items)
{
string delimeteredItems = string.Empty;
foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)
{
delimeteredItems += lvsi.Text + "#";
}
delimeteredListviewData.Add(delimeteredItems);
}
System.IO.File.WriteAllLines(path, delimeteredListviewData.ToArray());
}
private void loadListOfPeopleItems(string path, ListView lv)
{
foreach (string line in System.IO.File.ReadAllLines(path))
{
lv.Items.Add(new ListViewItem(line.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries)));
}
}
You need to create the save logic, there's no automatic method.
The easiest way is to use Serialization (look for ISerializable) to save to a file of your choice (name it whatever you like): You can save entire objects this way. Here is a simple tutorial on serialization.
The other method is to parse the listview content into strings (keep only what you need), and you save the strings to a text file (TextReader and TextWriter).
If you want to save application settings (and not user data), have a look at this link, or this one which might be easier to read.
Finally, if you need to interact with the data you save regularly, or if you have a lot of data to store, use a database (SQL, mySQL, etc). The latter method is the longest one to implement.
if you want to save to a text file, you could use the following:
private void saveListViewItems(string path, ListView lv)
{
var delimeteredListviewData = new List<string>();
foreach (ListViewItem lvi in lv.Items)
{
string delimeteredItems = string.Empty;
foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)
{
delimeteredItems += lvsi.Text + "#";
}
delimeteredListviewData.Add(delimeteredItems);
}
System.IO.File.WriteAllLines(path, delimeteredListviewData.ToArray());
}
private void loadListViewItems(string path, ListView lv)
{
foreach (string line in System.IO.File.ReadAllLines(path))
{
lv.Items.Add(new ListViewItem(line.Split(new char[]{'#'}, StringSplitOptions.RemoveEmptyEntries)));
}
}
You can use integrated visual studio settings and than bind it to your listview, even in designer. After you changed your listview you can save them
I would say the answer is : use .txt files
Also you can create an SQL database, and save it there...

Applications changing the language at runtime

I have read the tutorials about how to make a multilingual program in .Net, and it works well, but here I need an idea to make all the things at run-time easier.
At run-time when the user click on the language. I change the culture to the proper language chosen for example:
Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en");
and then calling a function which set the Text for the layout of my form:
private System.Resources.ResourceManager rm;
fileToolStripMenuItem1.Text = rm.GetString("fileToolStripMenuItem1.Text");
settingsToolStripMenuItem.Text = rm.GetString("settingsToolStripMenuItem.Text");
As it seems the look-up table which has been built by .Net when I set the text for each component of my program is equals to the property which should be set into it. In other word, the "fileToolStripMenuItem1.Text" is passing to the GetString() function and the result should be set to fileToolStripMenuItem1.Text, so I don't know how can I do it or even with which tool it is possible to iterate on every property of the rm and then by reflection or something else assign the value of the key to the key. That is to say, suppose "fileToolStripMenuItem1.Text" is the key in the lookup table and the value is "A" so how can I do this: Assigning the value of "fileToolStripMenuItem1.Text" which is "A" to fileToolStripMenuItem1.Text
I've written some test winforms app and try it and can change controls Text property dynamicly fine. You can extend this solution if need.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace ConsoleApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//main logic of switching language of UI
void ChangeCulture_Handler(CultureInfo culture)
{
//getting relative path of resource file for specific culture
var resourcePath = GetLocalizedResourceFile(culture);
//initialize new reader of resource file
var reader = new ResXResourceReader(resourcePath);
//getting enumerator
var resourceEnumerator = reader.GetEnumerator();
//enumerate each record in resource file
while (resourceEnumerator.MoveNext())
{
string resKey = Convert.ToString(resourceEnumerator.Key);
//we can add here some check if need
//(for example if in resource file exists not only controls resources with format <Control Name>.<Property>
//if( resKey.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length == 2)
string resValue = Convert.ToString(resourceEnumerator.Value);
//actually update property
UpdateControl(resKey, resValue);
}
}
//main logic of updating property of one control
private void UpdateControl(string resKey, string resValue)
{
//we suppose that format of keys in resource file is <Control Name>.<Property>
var strs = resKey.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
var controlName = strs[0];
var controlProp = strs[1];
//find control of form by its name
var controls = this.Controls.Find(controlName, true);
if (controls.Length > 0)
{
//select first control
var control = controls[0];
//getting type of it
var t = control.GetType();
//getting property
var props = t.GetProperty(controlProp);
if (props != null)
{
//setting localized value to property
props.SetValue(control, resValue, null);
}
}
}
//build resource file path
string GetLocalizedResourceFile(CultureInfo ci)
{
string cultureCode = ci.TwoLetterISOLanguageName;
//for english language is default, so we don't have a need to add "en" part in path
return cultureCode != "en" ? string.Format("Resource1.{0}.resx", cultureCode) : "Resource1.resx";
}
private void button1_Click(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("es-MX");
ChangeCulture_Handler(Thread.CurrentThread.CurrentCulture);
}
private void button2_Click(object sender, EventArgs e)
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
ChangeCulture_Handler(Thread.CurrentThread.CurrentCulture);
}
}
}
Resource for english (Resource1.resx)
button1.Text Change language to es
button2.Text Change language to en
label1.Text label1
label2.Text label2
Resource for spanish (Resource1.es.resx)
button1.Text cambiar el idioma to es
button2.Text cambiar el idioma to en
label1.Text lalble1
label2.Text lalble2

C# in Visual Studio: How to display a list in a DataGridView? Getting weird stuff

Been a while and I've volunteered to teach myself Windows programming at my company. Started writing vbs scripts and suddenly realized how incredibly useful this programming thing is ;-)
Anyway, I'm a total newbie at C# AND Visual Studio, I kind of get how it works, you drag and drop interface pieces in the design side then wire them together in the back/program side.
I'm trying to write a program that will (ultimately) read in a (very specific kind of) csv file and give the user a friendlier way to edit and sort through it than Excel. Should be simple stuff, and I'm excited about it.
I started this morning and, with the help of the internet, got as far as reading in and parsing the CSV (which is actually a TSV, since they use tabs not commas but hey).
I've been trying to figure out the best way to display the information, and, for now at least, I'm using a DataGridView. But the data isn't displaying. Instead, I'm seeing a long grid of values with column headers of Length, LongLength, Rank, SyncRoot, IsReadOnly, IsFixedSize, and IsSynchronized.
I don't know what any of these mean or where they come from, and unfortunately I don't know how change them either. Maybe somebody can help?
Here is my code:
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.IO;
namespace readInCSV
{
public partial class readInCSV : Form
{
public readInCSV()
{
InitializeComponent();
}
public List<string[]> parseCSV(string path)
{
List<string[]> parsedData = new List<string[]>();
try
{
using (StreamReader readfile = new StreamReader(path))
{
string line;
string[] row;
while ((line = readfile.ReadLine()) != null)
{
row = line.Split('\t');
parsedData.Add(row);
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return parsedData;
}
//PRIVATE METHODS FROM HERE ON DOWN
private void btnLoadIn_Click(object sender, EventArgs e)
{
int size = -1;
DialogResult csvResult = openCSVDialog.ShowDialog();
if (csvResult == DialogResult.OK)
{
string file = openCSVDialog.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
}
catch (IOException)
{
}
}
dgView.Dock = DockStyle.Top;
dgView.EditMode = DataGridViewEditMode.EditOnEnter;
dgView.AutoGenerateColumns = true;
dgView.DataSource = parseCSV(openCSVDialog.FileName);
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void openCSVDialog_FileOk(object sender, CancelEventArgs e)
{
}
}
}
Thanks in advance!
What's happening here is that the DataGridView is trying to display all the information for each of the string arrays in your parsedData List.
When you set a DataGridView's DataSource as a List of objects, it attempts to interpret each of the objects as a row to display. parsedData is a List of string array objects, so the grid is showing you all the displayable properties for an array object.
What we can do is parse each TSV row into a custom class (call it TsvRow) which has all the relevant data exposed. The TsvRow objects are then placed in a List and passed to the DataGridView. An example of this approach is explained in this article.
For example:
public class TsvRow
{
// Properties to hold column data
public string Column1 { get; set; }
public string Column2 { get; set; }
}
...
public List<TsvRow> parseCSV(string path)
{
List<TsvRow> parsedData = new List<TsvRow>();
try
{
using (StreamReader readfile = new StreamReader(path))
{
string line;
string[] row;
while ((line = readfile.ReadLine()) != null)
{
row = line.Split('\t');
// Here we assume we know the order of the columns in the TSV
// And we populate the object
TsvRow tsvRow = new TsvRow();
tsvRow.Column1 = row[0];
tsvRow.Column2 = row[1];
parsedData.Add(myObject);
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return parsedData;
}
Since all your column data is exposed as properties (i.e. "Column1" and "Column2"), they should be reflected in the DataGridView automatically.
Hope that helps! Please let me know if this needs clarification.
The DataGridView tries to display the Properties of your string-Array. You should set AutoGenerateColumns = false and create the columns by yourself.
Would the first line of the CSV/TSV contain the column names? Is so, you shouldn't pass them as DataSource.
dgView.AutoGenerateColumns = false;
foreach(string name in columnNames)
{
dgView.Columns.Add(name, name);
}

Categories

Resources