and want to learn how to work with xml files in c#.......
working with xml files in c#
Page 1 of 113 Replies - 1643 Views - Last Post: 22 May 2010 - 12:20 PM
#1
working with xml files in c#
Posted 21 May 2010 - 01:11 AM
well m new to the very language c#
and want to learn how to work with xml files in c#.......
and want to learn how to work with xml files in c#.......
Replies To: working with xml files in c#
#2
Re: working with xml files in c#
Posted 21 May 2010 - 01:22 AM
That's kind of an advanced topic there. Start with way way more basic stuff.
Take care.
Take care.
#3
Re: working with xml files in c#
Posted 21 May 2010 - 01:47 AM
Like MentalFloss says, not the easiest thing if your new to C#, though if you've programmed much Java you shouldn't have much trouble picking up C#... So yea... maybe start with something a bit lower, maybe play with StreamReaders and StreamWriters a bit to do some text file IO before jumping right in to XML's...
On the other hand I know your probably gonna ignore that so heres a pretty decent XML in C# tutorial Linky link of Linkyness
On the other hand I know your probably gonna ignore that so heres a pretty decent XML in C# tutorial Linky link of Linkyness
#4
Re: working with xml files in c#
Posted 21 May 2010 - 02:30 AM
A linky link of linkyness...?
Anyway: This and the second part have both been very helpful for me lately.
Anyway: This and the second part have both been very helpful for me lately.
#5
Re: working with xml files in c#
Posted 21 May 2010 - 04:06 AM
One simple way to do this would be to create .NET classes that you put the data in and then use XmlSerializer to serialize the data to a file and then later deserialize back into an instance of the class and re-populate the form.
AS an example, if you have a form with customer data.
So save the data as XML your code will look something like this.
And loading the data back would be something like the following
AS an example, if you have a form with customer data.
public class CustomerData
{
public string FirstName;
public string LastName;
}
So save the data as XML your code will look something like this.
// Create an instance of the CustomerData class and populate
// it with the data from the form.
CustomerData customer = new CustomerData();
customer.FirstName = txtFirstName.Text;
customer.LastName = txtLastName.Text;
// Create and XmlSerializer to serialize the data to a file
XmlSerializer xs = new XmlSerializer(typeof(CustomerData));
using (FileStream fs = new FileStream("Data.xml", FileMode.Create))
{
xs.Serialize(fs, customer);
}
And loading the data back would be something like the following
CustomerData customer;
XmlSerializer xs = new XmlSerializer(typeof(CustomerData));
using (FileStream fs = new FileStream("Data.xml", FileMode.Open))
{
// This will read the XML from the file and create the new instance
// of CustomerData
customer = xs.Deserialize(fs) as CustomerData;
}
// If the customer data was successfully deserialized we can transfer
// the data from the instance to the form.
if (customer != null)
{
txtFirstName.Text = customer.FirstName;
txtLastName.Text = customer.LastName;
}
This post has been edited by stapia.gutierrez: 21 May 2010 - 04:11 AM
#6
Re: working with xml files in c#
Posted 21 May 2010 - 04:39 AM
If you are using .Net 3.5 or higher, you could look into LINQ-To-XML. In my opinion, it makes working with XML data a lot easier.
http://msdn.microsof...y/bb387098.aspx
http://msdn.microsof...y/bb387098.aspx
#7
Re: working with xml files in c#
Posted 21 May 2010 - 04:52 AM
Well m familiar with the java language.......
and have been practicing c# from 4 days ....
and now i have problem with the xml part so......
and have been practicing c# from 4 days ....
and now i have problem with the xml part so......
#8
Re: working with xml files in c#
Posted 21 May 2010 - 04:53 AM
Just to piggyback off of what stapia.gutierrez said. It would behoove you to put serialization and deserialization code inside try-catch blocks because there is a chance that something could go wrong while processing the data.
#9
Re: working with xml files in c#
Posted 21 May 2010 - 05:47 AM
stapia.gutierrez, on 21 May 2010 - 03:06 AM, said:
One simple way to do this would be to create .NET classes that you put the data in and then use XmlSerializer to serialize the data to a file and then later deserialize back into an instance of the class and re-populate the form.AS an example, if you have a form with customer data.
public class CustomerData{ public string FirstName; public string LastName;}
So save the data as XML your code will look something like this.
// Create an instance of the CustomerData class and populate// it with the data from the form.CustomerData customer = new CustomerData();customer.FirstName = txtFirstName.Text;customer.LastName = txtLastName.Text;// Create and XmlSerializer to serialize the data to a fileXmlSerializer xs = new XmlSerializer(typeof(CustomerData));using (FileStream fs = new FileStream("Data.xml", FileMode.Create)){ xs.Serialize(fs, customer);}
And loading the data back would be something like the following
CustomerData customer;XmlSerializer xs = new XmlSerializer(typeof(CustomerData));using (FileStream fs = new FileStream("Data.xml", FileMode.Open)){ // This will read the XML from the file and create the new instance // of CustomerData customer = xs.Deserialize(fs) as CustomerData;}// If the customer data was successfully deserialized we can transfer// the data from the instance to the form.if (customer != null){ txtFirstName.Text = customer.FirstName; txtLastName.Text = customer.LastName;}
eclipsed4utoo, on 21 May 2010 - 03:39 AM, said:
If you are using .Net 3.5 or higher, you could look into LINQ-To-XML. In my opinion, it makes working with XML data a lot easier.http://msdn.microsoft.com/en-us/library/bb387098.aspx
Nice posts guys, I learnt something from both of those
#10
Re: working with xml files in c#
Posted 21 May 2010 - 07:39 AM
sjdashing, on 21 May 2010 - 05:52 AM, said:
Well m familiar with the java language.......
and have been practicing c# from 4 days ....
and now i have problem with the xml part so......
and have been practicing c# from 4 days ....
and now i have problem with the xml part so......
So......WHAT???? Are you going to ask a question? Are you going to post code? Or are you just using this like a LiveJournal?
You've been given good links and posts from which to learn. Is there something ELSE for which you're looking?
#11
Re: working with xml files in c#
Posted 21 May 2010 - 08:58 AM
I agree; ask a question.
What are you having troubles with precisely. Don't say: "Need help with the xml part".
What are you having troubles with precisely. Don't say: "Need help with the xml part".
#12
Re: working with xml files in c#
Posted 21 May 2010 - 01:37 PM
I've found one other good resource. This is a series of short walkthroughs on pretty much anything you'd need to do in XML (or so it seems). it doesn't cover serialization of classes that I saw, but it does cover creating and deleting nodes and elements, appending them, inserting them, etc.
#13
Re: working with xml files in c#
Posted 22 May 2010 - 07:25 AM
some very good links here, and thanks stapia.gutierrez, i've been trying something similar (just a lot more complex), gonna start over again with the example you provided, and work my way from there 
~Shivern
~Shivern
#14
Re: working with xml files in c#
Posted 22 May 2010 - 12:20 PM
well i hav got what i wanted and thnx
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote







|