473,387 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

XmlSerializing array of objects (but ignoring the array name itself).

The code below will serialize the Catalog object something like:
<Catalog>
<CatalogTitle>MyCatalog</CatalogTitle>
<Books>
<Book>
<Title>Book1</Title>
</Book>
<Book>
<Title>Book2</Title>
</Book>
</Books>
</Catalog>

But I don't want the 'Books' element to be included in my xml, I want
the Book elements directly under Catalog:
<Catalog>
<CatalogTitle>MyCatalog</CatalogTitle>
<Book>
<Title>Book1</Title>
</Book>
<Book>
<Title>Book2</Title>
</Book>
</Catalog>

If I use the XmlIgnoreAttribute on the Books array, the array will
completely be ignored. I also tried using the XmlArrayAttribute and
XmlArrayItemAttribute, but I couldn't get it to work.

How can I achieve this?

Thank you in advance,
Taco.

--- code: ---

using System;
using System.Xml.Serialization;

namespace ConsoleApplication1 {

class App {
static void Main(string[] args) {

Catalog c = new Catalog();
c.CatalogTitle = "MyCatalog";
Book b1 = new Book("Book1");
Book b2 = new Book("Book1");
c.Books = new Book[] { b1, b2 };

XmlSerializer ser = new XmlSerializer(c.GetType());
ser.Serialize(Console.Out, c);
}
}

public class Catalog {
public string CatalogTitle;
public Book[] Books;
}

public class Book {
public string Title;

public Book() {}

public Book(string pTitle) {
Title = pTitle;
}
}
}
Nov 12 '05 #1
1 1700


Taco wrote:
The code below will serialize the Catalog object something like:
<Catalog>
<CatalogTitle>MyCatalog</CatalogTitle>
<Books>
<Book>
<Title>Book1</Title>
</Book>
<Book>
<Title>Book2</Title>
</Book>
</Books>
</Catalog>

But I don't want the 'Books' element to be included in my xml, I want
the Book elements directly under Catalog:
<Catalog>
<CatalogTitle>MyCatalog</CatalogTitle>
<Book>
<Title>Book1</Title>
</Book>
<Book>
<Title>Book2</Title>
</Book>
</Catalog>

If I use the XmlIgnoreAttribute on the Books array, the array will
completely be ignored. I also tried using the XmlArrayAttribute and
XmlArrayItemAttribute, but I couldn't get it to work.

How can I achieve this? public class Catalog {
public string CatalogTitle;
Can you try
[System.Xml.Serialization.XmlElementAttribute("Book ",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public Book[] Books;


If that doesn't help then run your XML instance through xsd.exe to infer
a schema and then run that schema through xsd.exe to infer a C# class
and look at the attributes generated.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
6
by: surrealtrauma | last post by:
i have a trouble about that: i want to ask user to enter the employee data (employee no., name, worked hour, etc.), but i dont know how to sort the data related to a particular employee as a...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
6
by: Gustaf Liljegren | last post by:
I ran into this problem today: I got an array with Account objects. I need to iterate through this array to supplement the accounts in the array with more data. But the compiler complains when I...
1
by: Morten Dyndgaard | last post by:
Hi, I want to do the following: I have to XmlSerialize a HashTable, but since it inherits from IDictionary it cannot be XmlSerialized, and I prefer not to make my own implementation of...
0
by: Simon Gregory | last post by:
I'm trying to override the default elementnames in the (seemingly) simple case of serializing an array of GUIDs into XML. Here's the basic code I started with: Dim arrGuids(3) as Guid ...
2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
2
by: wlevine | last post by:
Can someone give me an example of how to construct an array of objects? for example, I have: function objFacility(id,name,adr,city,state,zip) { this.id = id this.name=name this.adr=adr...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.