471,570 Members | 937 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,570 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 2968
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Michael | last post: by
3 posts views Thread by CLEAR-RCIC | last post: by
14 posts views Thread by vince | last post: by
5 posts views Thread by David Sworder | last post: by
10 posts views Thread by Dan | last post: by
3 posts views Thread by MAY | last post: by
2 posts views Thread by films | last post: by
7 posts views Thread by Ben Amada | last post: by
4 posts views Thread by =?Utf-8?B?Qnlyb24=?= | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

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.