|
-
Jul 22nd, 2008, 08:04 PM
#1
XmlSerialization Questoin
I have a class that contains several string and integer fields. I can serialize this fine using XmlSerializer.
Now I've added an object array as a field to my class (the object being a user defined class)
My question is how can i have the parent class serialize the object array as well?
here's a dummy example. Pretend like i can serialize an instance of the Settings class, except it won't serialize the ItemData[] array inside it. How can i serialize ItemData[] as well
Code:
[System.Xml.Serialization.XmlTypeAttribute(Namespace =
"urn:xmlns:CrReport:settings")]
[System.Xml.Serialization.XmlRootAttribute("settings",
Namespace = "urn:xmlns:app:settings", IsNullable = false)]
Class Settings
{
public string filename;
public int count;
//How can i have it serialize this as well?
public ItemData[] items;
}
class ItemData
{
public string title
public int size;
}
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jul 22nd, 2008, 11:20 PM
#2
Re: XmlSerialization Questoin
I haven't really delved into the specifics of serialisation too much but I think you'll find that you'd need to at least decorate the ItemData class with the Serializable attribute and, perhaps, the Items property too.
-
Jul 24th, 2008, 11:28 AM
#3
Fanatic Member
Re: XmlSerialization Questoin
This worked for me:
Code:
[System.Xml.Serialization.XmlTypeAttribute(Namespace =
"urn:xmlns:CrReport:settings")]
[System.Xml.Serialization.XmlRootAttribute("settings",
Namespace = "urn:xmlns:app:settings", IsNullable = false)]
public class Settings
{
public string filename;
public int count;
//How can i have it serialize this as well?
public ItemData[] items;
}
public class ItemData
{
public string title;
public int size;
}
... In some test harness...
Settings set = new Settings();
set.count = 5;
set.filename = "fds";
set.items = new ItemData[2];
set.items[0] = new ItemData();
set.items[0].size = 3;
set.items[0].title = "test";
set.items[1] = new ItemData();
set.items[1].title = "test2";
string xml = Serialize<Settings>(set);
... Serialize<T> method: ...
private string Serialize<T>(T entity)
{
StringBuilder outputXml = new StringBuilder();
XmlSerializer ser = new XmlSerializer(typeof(T));
using (TextWriter stream = new StringWriter(outputXml))
{
ser.Serialize(stream, entity);
}
return outputXml.ToString();
}
This was the result:
Code:
<?xml version="1.0" encoding="utf-16"?>
<settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:xmlns:app:settings">
<filename xmlns="urn:xmlns:CrReport:settings">fds</filename>
<count xmlns="urn:xmlns:CrReport:settings">5</count>
<items xmlns="urn:xmlns:CrReport:settings">
<ItemData>
<title>test</title>
<size>3</size>
</ItemData>
<ItemData>
<title>test2</title>
<size>0</size>
</ItemData>
</items>
</settings>
Last edited by MetalKid; Jul 24th, 2008 at 03:58 PM.
If your problem is solved, please use the Mark Thread As Resolved under Thread Tools!
Show Appreciation. Rate Posts!
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
|