|
-
May 7th, 2008, 08:00 AM
#1
object from/to XML
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
-
May 7th, 2008, 08:55 AM
#2
Re: object from/to XML
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.
-
May 7th, 2008, 09:00 AM
#3
Re: object from/to XML
Thanks cilu,
LINQ is .Net 3.0 feature, right? I have to use version 2.0. So, I can not use LINQ, right?
 Originally Posted by cilu
regards,
George
-
May 7th, 2008, 09:06 AM
#4
Re: object from/to XML
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
Code:
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() { }
}
-
May 7th, 2008, 09:11 AM
#5
Re: object from/to XML
Cool, MadHatter!
BTW: LINQ is pronounced as? :-)
regards,
George
-
May 7th, 2008, 09:21 AM
#6
Re: object from/to XML
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
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
-
May 7th, 2008, 09:28 AM
#7
Re: object from/to XML
Thanks Shuja,
LINQ is pronounced as "LIN CUE"?
regards,
George
-
May 7th, 2008, 09:29 AM
#8
-
May 7th, 2008, 02:06 PM
#9
Re: object from/to XML
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.
-
May 7th, 2008, 02:10 PM
#10
Re: object from/to XML
Oh yeah, LINQ is pronounced as 'link' - just like one of the characters in the TV series, 'The Mod Squad'.
-
May 8th, 2008, 07:49 AM
#11
Re: object from/to XML
 Originally Posted by George2
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!
-----------------------------------------------
Brad! Jones,
Yowza Publishing
LotsOfSoftware, LLC
-----------------------------------------------
-
May 8th, 2008, 12:54 PM
#12
Re: object from/to XML
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/...mework-35.html
-
May 8th, 2008, 01:23 PM
#13
Re: object from/to XML
 Originally Posted by cilu
Worded differently --
If you install .NET 3.0, you will not have LINQ. You need to install .NET 3.5 to have LINQ.
Brad!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|