Serialize Class

G

Guest

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),Serializable]
public class Person
{
public Person()
{

}

[XmlElement(ElementName="FirstName",IsNullable=false,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?
 
J

Jon Skeet [C# MVP]

Demetri said:
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
 
G

Guest

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.
 
C

catalin.tomescu

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
 
M

Marc Gravell

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
 
G

Guest

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top