473,698 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Element Name="Person",I sNullable=false ),Serializable]
public class Person
{
public Person()
{

}

[XmlElement(Elem entName="FirstN ame",IsNullable =false,DataType ="string")]
[Browsable(false ), EditorBrowsable (EditorBrowsabl eState.Never)]
public string firstName;

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

[XmlElement(Elem entName="LastNa me",IsNullable= false,DataType= "string")]
[Browsable(false ), EditorBrowsable (EditorBrowsabl eState.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 3053
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.Creat e has an overload that
accepts an XmlWriterSettin gs instance; one of the options on the settings is
OmitXmlDeclarat ion (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.Creat e has an overload that
accepts an XmlWriterSettin gs instance; one of the options on the settings is
OmitXmlDeclarat ion (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
2947
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 serialized. In my program I am calling a webservice which then return the data to the calling program. The program then have to serialize the data and create an XML-document.
3
2161
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> <Name /> <City />
14
14299
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
24710
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 s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
10
4155
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; string s_FinalFileName; try
3
10382
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 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll) . Thx in advance. Here is the part of the code: Regards MAY
3
2385
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
2
8882
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
3540
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 TypeConverter other than ReferenceConverter to be put in viewstate." I've included the <Serializable()> attribute, but I'm still getting the same error. The class is below ... as you can see it contains a Collection, two
4
7133
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 Building._Name to Building._BName in case the duplicate name was the issue, but that didn't help. Is there a native way to serialize nested objects, or will I have to write my own? public class LocationCell
0
8676
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8608
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7732
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6522
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.