Click to See Complete Forum and Search --> : object from/to XML
George2
May 7th, 2008, 08:00 AM
Hello everyone,
I want to convert an object instance to XML string, and later read the XML string to construct the object again. Any samples?
(I find some serialization samples, but all about binary format, I prefer to use XML format.)
thanks in advance,
George
cilu
May 7th, 2008, 08:55 AM
So you want serialization to XML. Could you use LINQ? It would be much simpler. Here is some example: http://mariusbancila.ro/blog/?p=32.
George2
May 7th, 2008, 09:00 AM
Thanks cilu,
LINQ is .Net 3.0 feature, right? I have to use version 2.0. So, I can not use LINQ, right?
So you want serialization to XML. Could you use LINQ? It would be much simpler. Here is some example: http://mariusbancila.ro/blog/?p=32.
regards,
George
MadHatter
May 7th, 2008, 09:06 AM
1. decorate your class and public properties with the xml serialization attributes
2. create an xml serializer by passing in the type of your custom class
3. serialize to a stream
--
1. open the stream
2. create the xml serializer by passing in the type of your custom class
3. deserialize the stream w/ the serializer to the instance of your class
using System;
using System.IO;
using System.Xml.Serialization;
class Program {
static void Main(string[] args) {
Foo f = new Foo();
f.A = "hello world";
f.B = true;
f.C = 32;
XmlSerializer xs = new XmlSerializer(typeof(Foo));
StringWriter sw = new StringWriter();
xs.Serialize(sw, f);
string xml = sw.ToString();
Console.WriteLine(xml);
StringReader sr = new StringReader(xml);
Foo b = xs.Deserialize(sr) as Foo;
Console.ReadLine();
}
}
[XmlRoot("foo")]
public class Foo {
string a;
bool b;
int c;
[XmlAttribute("a")]
public string A {
get { return a; }
set { a = value; }
}
[XmlElement("b")]
public bool B {
get { return b; }
set { b = value; }
}
[XmlElement("c")]
public int C {
get { return c; }
set { c = value; }
}
public Foo() { }
}
George2
May 7th, 2008, 09:11 AM
Cool, MadHatter!
BTW: LINQ is pronounced as? :-)
regards,
George
Shuja Ali
May 7th, 2008, 09:21 AM
MadHatter has shown you a very good example.
Just to add there are numerous articles on XML Serialization too out there. I am sure you would have landed here, if you would have tried google
http://www.dotnetjohn.com/articles.aspx?articleid=173
http://support.microsoft.com/kb/815813
And MSDN has a whole topic dedicated to Serialization, and I mean both Binary as well as XML.
Look at Serializing Objects (http://msdn.microsoft.com/en-us/library/7ay27kt9%28VS.71%29.aspx)
PS: I still believe that most of your doubts would get cleared by looking at the relevant documentation on MSDN. You should give it a try.
Edit --
LINQ - Langauge Integrated Query (http://msdn.microsoft.com/en-us/netframework/aa904594.aspx)
George2
May 7th, 2008, 09:28 AM
Thanks Shuja,
LINQ is pronounced as "LIN CUE"?
regards,
George
MadHatter
May 7th, 2008, 09:29 AM
pronounced: "link"
Arjay
May 7th, 2008, 02:06 PM
You can also use tools that will create C# classes from an Xsd file. While not perfect, they are a great way to use as a starting point for Xml decorated class generation (as well as a great learning tool).
Check out Xsd.exe and XsdObjectGen.exe.
Arjay
May 7th, 2008, 02:10 PM
Oh yeah, LINQ is pronounced as 'link' - just like one of the characters in the TV series, 'The Mod Squad'.
Brad Jones
May 8th, 2008, 07:49 AM
LINQ is .Net 3.0 feature, right? I have to use version 2.0. So, I can not use LINQ, right?
LINQ is a 3.5 feature, not 3.0.
Having said that, 3.0 and 3.5 are both simply add-ons to .NET 2.0 more so than "new versions".
Brad!
cilu
May 8th, 2008, 12:54 PM
LINQ is a 3.5 feature, not 3.0.
But it's built on features from 3.0.
For those interested, here is an explanation with the content of 3.5.
http://www.danielmoth.com/Blog/2007/06/net-framework-35.html
Brad Jones
May 8th, 2008, 01:23 PM
But it's built on features from 3.0.
For those interested, here is an explanation with the content of 3.5.
http://www.danielmoth.com/Blog/2007/06/net-framework-35.html
Worded differently --
If you install .NET 3.0, you will not have LINQ. You need to install .NET 3.5 to have LINQ.
Brad!
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.