13 Replies - 2201 Views - Last Post: 22 May 2010 - 12:20 PM Rate Topic: -----

#1 sjdashing  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-May 10

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#.......
:whistling:

Is This A Good Question/Topic? 0
  • +

Replies To: working with xml files in c#

#2 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

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.
Was This Post Helpful? 0
  • +
  • -

#3 Mion  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-April 10

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
Was This Post Helpful? 1
  • +
  • -

#4 Choscura  Icon User is offline

  • D.I.C Lover


Reputation: 463
  • View blog
  • Posts: 2,227
  • Joined: 18-October 08

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.
Was This Post Helpful? 2
  • +
  • -

#5 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

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.

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

Was This Post Helpful? 3
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

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
Was This Post Helpful? 2
  • +
  • -

#7 sjdashing  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-May 10

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......

Was This Post Helpful? 0
  • +
  • -

#8 EvLSnoopY  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 17
  • View blog
  • Posts: 93
  • Joined: 24-November 09

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.
Was This Post Helpful? 0
  • +
  • -

#9 Mion  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-April 10

Re: working with xml files in c#

Posted 21 May 2010 - 05:47 AM

View Poststapia.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;}



View Posteclipsed4utoo, 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 :)
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: working with xml files in c#

Posted 21 May 2010 - 07:39 AM

View Postsjdashing, 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......


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?
Was This Post Helpful? 2
  • +
  • -

#11 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

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".
Was This Post Helpful? 1
  • +
  • -

#12 Choscura  Icon User is offline

  • D.I.C Lover


Reputation: 463
  • View blog
  • Posts: 2,227
  • Joined: 18-October 08

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.
Was This Post Helpful? 0
  • +
  • -

#13 Shivern  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 27
  • Joined: 20-May 10

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
Was This Post Helpful? 0
  • +
  • -

#14 sjdashing  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-May 10

Re: working with xml files in c#

Posted 22 May 2010 - 12:20 PM

well i hav got what i wanted and thnx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1