I have XML file of object.I want to get instance of that object.But i dont have class.I must read XML and create dynamically how to say class. How to do?
See this page, it can help but you need .NET 4.
I guess you want to read data from an XML into your class. If so, you can use the following way to read the data from a file.
var records = from r in XElement.Load(#"YourFile.xml").Elements("NodeName")
select r;
Related
As the question says, using the FileHelpers library I am attempting to generate a CSV file along side a report file. The report file may have different (but finite) inputs/data structures and hence my CSV generation method is not explicitly typed. The CSV contains all of the report data as well as the report's header information. For my headers, I am using the class object properties because they are descriptive enough for my end use purpose.
My relevant code snippet is below:
// File location, where the .csv goes and gets stored.
string filePath = Path.Combine(destPath, fileName);
// First, write report header details based on header list
Type type = DetermineListType(headerValues);
var headerEngine = new FileHelperEngine(type);
headerEngine.HeaderText = headerEngine.GetFileHeader();
headerEngine.WriteFile(filePath, (IEnumerable<object>)headerValues);
// Next, append the report data below the report header data.
type = DetermineListType(reportData);
var reportDataEngine = new FileHelperEngine(type);
reportDataEngine.HeaderText = reportDataEngine.GetFileHeader();
reportDataEngine.AppendToFile(filePath, (IEnumerable<object>)reportData);
When this is executed, the CSV is successfully generated however the .AppendToFile() method does not add the reportDataEngine.HeaderText. From the documentation I do not see this functionality to .AppendToFile() and I am wondering if anyone has a known work-around for this or a suggestion how to output the headers of two different class objects in a single CSV file using FileHelpers.
The desired output would look something like this however in a single CSV file (This would be a contiguous CSV obviously; not tables)
Report_Name
Operator
Timestamp
Access Report
User1
14:50:12 28 Dec 2020
UserID
Login_Time
Logout_Time
User4
09:33:23
10:45:34
User2
11:32:11
11:44:11
User4
15:14:22
16:31:09
User1
18:55:32
19:10:10
I have looked also at the MultiRecordEngine in FileHelpers and while I think this may be helpful, I cannot figure out based on the examples how to actually write a multirecord CSV file in the required fashion I have above; if it is possible at all.
Thank you!
The best way is to merge the columns and make one big table then make your classes match the columns you need to separate them out when reading. CSV only allows for the first row to define the column names and that is optional based on your use case. Look at CSVHelper https://joshclose.github.io/CsvHelper/ it has a lot of built-in features with lots of examples. Let me know if you need additional help.
I have a service that add items to a class and then serialize a class into a file.
Then I need to create a simple form C# with a textfield (multiline) showing realtime from the deserialized file.
I will have timer every second that will read my List of a property of the class and show it in a textfield.
My question here is:
Is there a way that instead of reading again and again the file I can just get the latest rows added and just append to the textfield?
var file = File.OpenRead("abc.txt");
file.Seek(1000, SeekOrigin.Begin);
Reading a file, use "Seek" to skip old data.
just a suggestion:
I think, you should rather use some sort of message queue for this purpose.
Again per your post, instead of directly fetching from file, it would be good if you rather store the deserialized content in a stack. That way, you can always get the content from top of the stack.
I am not new to WPF, but I am still a rookie. Let's say, I want to build an application which stores data about a person in an unique and separate file, and not in a database, sort of, like, Notepad. My application should do the following things.
It should be able to save a person's info in an unique file.
It should be able to open an user specified file and auto fill the properties/form.
How do I achieve this? Is the XML binding only way to achieve this, or is there an any other alternative? What I mean is, If I use XML binding I can write code which will enable the user to open and save different XML files, but I also read that binding to XML should be avoided from the architecture perspective. So, is there an alternative solution for my problem?
I think if you try doing the stuff by using a Reading and writing the things to a CSV(Comma separated values) file(If not planning to implement databases) then you can achieve what you wanted.
Also if you are planning to have a separate file for each user its not at all a good idea.
Its not possible to explain everything thing here . So please have a look to link posted below , in which it has explained in detail how to achieve Reading and Writing to a csv file .
This example has been posted from here for getting full detail please look to following link Reading and writing to a csv file
Apparently your requirement is to save person details into a unique file. If you really want to use that approach, one option is using XMLSerialization.
You can create your normal person object for data binding.
When you want to save data into the specific person's file you can serialize the object and save file with a proper name (person id or so)
When you want to get Person data back from the file, you can deserialize the it directly to a person object.
// Serialize and write to file
Person person = myPerson;
var serializer = new XmlSerializer(person.GetType());
using (var writer = XmlWriter.Create("person1.xml"))
{
serializer.Serialize(writer, person);
}
// Deserialize back to an instance
var serializer = new XmlSerializer(typeof(Person));
using (var reader = XmlReader.Create("person1.xml"))
{
var person= (Person)serializer.Deserialize(reader);
}
For saving user data, such as sessions and settings. There are plenty of ways you can do this.
Saving to data to txt files. See here.
Saving data to a database. See here.
My personal favourite, saving to the Settings file. See here.
These are only some of the ways you can save data locally.
Note that I mentioned saving data to a database because it is something that you shouldn't completely knock, especially if you will be saving lots of data.
To answer your question more directly, I would suggest that you go with option 3. For relatively small sets of data, like user info and user settings, it would be your best bet to save them to the built in Settings file. It's dead easy.
Good luck!
Is it possible to bind the contents of an xml file with a data grid view in c#? Maybe using... LINQ? can I do that? I want to display the contents of an xml file within a grid view, edit, add or delete them there and then save them in the xml file I loaded in the first place. I'd also like to be able to search through the grid and edit multiple items. I am creating a forms application. The xml file is simple it only:
<people>
<person name='John' email='John#email.com'/>
</people>
There can be lots of records of type person.
What's the best way to approach this problem?
The easiest way recommended by MSDN here http://msdn.microsoft.com/en-us/magazine/cc163669.aspx is to load it into a data set.
There is an entire set of code in Vb.Net over at DevX here and a tutorial that might help you with binding DataGridView to XML via data sets. http://www.devx.com/dotnet/Article/28678/1954
Hope this helps. It is in Vb.Net but you will get idea.
Assuming you have loaded your xml into "doc" XDocument
var persons = from item in doc.Descendants("person")
select new
{
Name = item.Element("name").Value,
Mail = item.Element("email").Value
};
myDataboundControl.DataSource = persons;
myDataboundControl.Databind();
first you have to get the path of the XML file .Then create a new data set then bind the data set with the data grid view as your wish. you can also use SQL query to update,delete the XML file.
{
Data Set dd = new Data Set();
dd.ReadXml ("XML Path");
DataTable xm = ds.Tables[0];
}
A want get all information which are in located in <forecast_conditions></forecast_conditions> tag for first ta I use
var for_cod = from currentCond in xdoc.Root.Descendants("current_conditions")
select currentCond;
I don;t know how to get information from 2,3 <forecast_conditions></forecast_conditions> tags because it have same names, maybe you have any ideas?
xml file: http://www.google.com/ig/api?weather=vilnius&hleng=eng
I am not sure what you exactly need. If you want to get all forecast_conditions for in a result set you can use this simple query
var query = from t in doc.Descendants("forecast_conditions")
select t;
You may wanna see Weather Information from Google Weather using ASP.NET and LINQ to XML, also check out this article Using Google Weather API In A C# Application. Its not using LINQ though. Also check out this thread C# Pull XML data from google's weather API