Groups | Blog | Home
all groups > dotnet xml > february 2005 >

dotnet xml : XML Serialization:: Too many ArrayList elements


Codex Twin
2/19/2005 2:20:08 PM
I'm using XML Serialization to try and get this structure:

<LibraryCategories>
<LibraryCatgeory>1</LibraryCatgeory>
<LibraryCatgeory>2</LibraryCatgeory>
<LibraryCatgeory>3</LibraryCatgeory>
</LibraryCategories>

My class has a public property called LibraryCategories, of type ArrayList:

[XmlElement(Type = typeof(LibraryCategoryItem))]
public ArrayList LibraryCategories;

In addition, I have class which defines the LibraryCategory item:

public class LibraryCategoryItem
{
public string LibraryCategory;
}


The problem is, the Serialized XML takes this structure

<LibraryCategories>
<LibraryCatgeory>1</LibraryCatgeory>
</LibraryCategories>
<LibraryCategories>
<LibraryCatgeory>2</LibraryCatgeory>
</LibraryCategories>
<LibraryCategories>
<LibraryCatgeory>3</LibraryCatgeory>
</LibraryCategories>


What attributes do I need to place on the LibraryCategories and/or the
LibraryCategoryItem class to enforce the desired XML?

Derek Harmon
2/19/2005 11:47:44 PM
[quoted text, click to view]

This should have the following attributes on it to achieve your desired outcome,

[XmlArray("LibraryCategories")]
[XmlArrayItem("LibraryCategory", typeof(LibraryCategoryItem))]
public ArrayList LibraryCategories;

I observe there was a consistent typo in the target XML you've requested (it
says LibraryCatGEory instead of LibraryCatEGory). If that is intentional, then
use [XmlArrayItem("LibraryCatgeory", typeof(LibraryCategoryItem))] here
instead.

[quoted text, click to view]

Further, to prevent the LibraryCategory field from being wrapped in an
element itself you must make it a text node with the following attribute,

[XmlText( )]
public string LibraryCategory;


Derek Harmon

Codex Twin
2/20/2005 2:16:41 PM

[quoted text, click to view]

Thanks very much for a great, timely response.

AddThis Social Bookmark Button