473,396 Members | 1,804 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,396 software developers and data experts.

Serialize Class

I have a class that I need to serialize. For example if I had a Person class
with the properties of FirstName and LastName. Currently when I serialize the
class it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Person>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Person>

My issue is the first line (<?xml version="1.0" encoding="utf-8"?>). It
can't be included in the xml since I have to send the xml as a string to an
outside java applet. If the declaration is included it will be interpreted as
malformed by the applet. So the xml must look as follows:

<Person>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Person>
The class lookes as follows:

[XmlRoot(ElementName="Person",IsNullable=false),Ser ializable]
public class Person
{
public Person()
{

}

[XmlElement(ElementName="FirstName",IsNullable=fals e,DataType="string")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public string firstName;

[XmlIgnore]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}

[XmlElement(ElementName="LastName",IsNullable=false ,DataType="string")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public string lastName;

[XmlIgnore]
public string LastName
{
get { return lastName; }
set { lastName = value; }
}

}

Is there an attribute that i'm missing that would solve my problem? I'd
rather go the attribute route since it is an elegant solution. I don't think
string parsing is the answer.

Any ideas?

--
-Demetri
Feb 6 '06 #1
6 3036
Demetri wrote:
I have a class that I need to serialize. For example if I had a Person class
with the properties of FirstName and LastName. Currently when I serialize the
class it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Person>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Person>

My issue is the first line (<?xml version="1.0" encoding="utf-8"?>). It
can't be included in the xml since I have to send the xml as a string to an
outside java applet. If the declaration is included it will be interpreted as
malformed by the applet.


Why? Is the applet only expecting an XML element rather than a whole
document?

One thing you could do is load it as an XmlDocument then just take the
OuterXml of the root element...

Jon

Feb 6 '06 #2
I'm not sure how the applet functions under the hood. I do know that its not
SOAP and the XML declaration does not conform to the specifications of what
it accepts. Therefore I have to work with what I've got. It only accempts a
pre-defined xml string.

Yeah, I wanted to avoid loading a dom object, that's expensive for removing
one line of xml from the xml string. I also don't like the string
manipulation route, that is sort of clunky. I'm sure there is some attribute
I can use but just don't know which one. Or something I should do in the
serialization code. Im using the XmlSerializer and XmlTextWriter to serialize
the class.

--
-Demetri
"Jon Skeet [C# MVP]" wrote:
Demetri wrote:
I have a class that I need to serialize. For example if I had a Person class
with the properties of FirstName and LastName. Currently when I serialize the
class it looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Person>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Person>

My issue is the first line (<?xml version="1.0" encoding="utf-8"?>). It
can't be included in the xml since I have to send the xml as a string to an
outside java applet. If the declaration is included it will be interpreted as
malformed by the applet.


Why? Is the applet only expecting an XML element rather than a whole
document?

One thing you could do is load it as an XmlDocument then just take the
OuterXml of the root element...

Jon

Feb 6 '06 #3
You could write your own XmlTextWriter that knows how to strip out the
start of the XML document. It is not very difficult to write something
like that.

Catalin

Feb 6 '06 #4
Possibly no need; I believe that XmlWriter.Create has an overload that
accepts an XmlWriterSettings instance; one of the options on the settings is
OmitXmlDeclaration (need also to use conformance level : fragment) - could
be worth a try?

Marc
Feb 6 '06 #5
That would be an excellent thing to try. I wish we were using .Net 2.0
already but unfortunately the company will not be moving from framework 1.1
to 2.0 until later in the year.

Is there something I can do in framework 1.1?
--
-Demetri
"Marc Gravell" wrote:
Possibly no need; I believe that XmlWriter.Create has an overload that
accepts an XmlWriterSettings instance; one of the options on the settings is
OmitXmlDeclaration (need also to use conformance level : fragment) - could
be worth a try?

Marc

Feb 6 '06 #6
Ahh... sorry... not afaik

Marc
Feb 6 '06 #7

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

Similar topics

1
by: Michael | last post by:
Hi I anyone have a clue or can solve my problem I would be glad :-) Regards Michael I have a problem with creating an XML-document where the returning data from the webservice, have been...
3
by: CLEAR-RCIC | last post by:
Hi, I have a class that I am trying to serialize. To simplify, I've changed the names to Baseball, Teams and Players. Here is what the XML should look like: <BaseBall> <Teams> <Team>...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
10
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName;...
3
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type...
3
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
7
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.