473,405 Members | 2,310 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,405 software developers and data experts.

XML Deserialization Question

Lets say I have an xml document that reads:

<Book>
<ID>
<Title>SomeTitle</Title>
<ISBN>12345</ISBN>
</ID>
</Book>

But I want to deserialize it to an object that looks like:

[XmlRoot("Book")]
public class Book
{
[XmlElement(ElementName="Title")]
public string Title;

[XmlElement(ElementName="ISBN"])
public string isbn;
}

The problem appears to be the ID element in the xml document. How do
I qualify the XmlElement attributes so that the Title goes into the
Title, and ISBN goes into ISBN? Should it be something like
[XmlElement(ElementName="ID.Title")] ?
Jul 18 '08 #1
2 1095
If you are going to do this with attributes on your class, then you will
have to expose a structure with the title and ISBN through a property named
ID (or something else, if you apply the XmlElement attribute to it).

Otherwise, you have to implement IXmlSerializable, and serialize this in
a custom manner. I would create a private structure with the title and ISBN
codes in it, and then attach the xml serialization attributes to that. In
your implementation of IXmlSerializable, you would then navigate to the ID
tag, and deserialize an instance of the private structure from there.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"xlar54" <sc**********@gmail.comwrote in message
news:2c**********************************@w7g2000h sa.googlegroups.com...
Lets say I have an xml document that reads:

<Book>
<ID>
<Title>SomeTitle</Title>
<ISBN>12345</ISBN>
</ID>
</Book>

But I want to deserialize it to an object that looks like:

[XmlRoot("Book")]
public class Book
{
[XmlElement(ElementName="Title")]
public string Title;

[XmlElement(ElementName="ISBN"])
public string isbn;
}

The problem appears to be the ID element in the xml document. How do
I qualify the XmlElement attributes so that the Title goes into the
Title, and ISBN goes into ISBN? Should it be something like
[XmlElement(ElementName="ID.Title")] ?
Jul 19 '08 #2
You can grab a few ideas here about how naming of the elements work.

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry

But you're probably going to have to implement the interface (previously
mentioned in another post) if you need it exact and to your liking.
"xlar54" <sc**********@gmail.comwrote in message
news:2c**********************************@w7g2000h sa.googlegroups.com...
Lets say I have an xml document that reads:

<Book>
<ID>
<Title>SomeTitle</Title>
<ISBN>12345</ISBN>
</ID>
</Book>

But I want to deserialize it to an object that looks like:

[XmlRoot("Book")]
public class Book
{
[XmlElement(ElementName="Title")]
public string Title;

[XmlElement(ElementName="ISBN"])
public string isbn;
}

The problem appears to be the ID element in the xml document. How do
I qualify the XmlElement attributes so that the Title goes into the
Title, and ISBN goes into ISBN? Should it be something like
[XmlElement(ElementName="ID.Title")] ?

Jul 20 '08 #3

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

Similar topics

2
by: Meta-Meta | last post by:
Hi, I'm trying to deserialize a VC++ .NET class and am stumped by the following problem : In order to create the XmlSerializer you need to pass the type of the object being created. The doc...
2
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on...
3
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
2
by: andreas | last post by:
Hi, if have a object arrSdList of type SortedList for serialization and deserialization there are two subs for doing this Public Sub deser Dim Formatter As BinaryFormatter = New...
3
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
1
by: parrot toes | last post by:
I tried to post this question before, but there was an error when posting. I case it did get posted and in order to avoid duplication, I'll just repost a summary. I have written a dotnet client...
8
by: ashoksrini | last post by:
Hi All, I have the below requirement and would like to get some feeback from the group on the best way to implement: 1. I have WSDL defined exposing few web services. 2. We dont have a...
6
by: Marcus | last post by:
I have a couple of classes looking like this: ======================================== public class AdminSettingsData { public string adminSettingsPath; public string adminSettingsFile;...
1
by: Gregor D. | last post by:
Hi, I want to consume an Apache Webservice with a C# .NET Client. Everything works fine, but I'm having problem with the deserialization of the complex type "Item" that is included in the...
0
by: Gary James | last post by:
I have a question about the methodology used to Serialize / Deserialize objects using XML in .Net 2005 using the v2.0 framework. Say, for example my application creates an instance of Object A,...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.