IXmlSerialise

E

elgrego

Hi,

I have been implementing the IXmlSerializer.

This is the class definition:
[Serializable]
[XmlSchemaProvider("GetSchema")]
[XmlRoot("Company")]
public class Company : BusinessDataBase, IXmlSerializable


With the WriteXml:


/// <summary>
/// Writes this object as Xml.
/// </summary>
/// <param name="writer"></param>
public override void WriteXml(XmlWriter writer)
{
writer.WriteStartElement(SHBXmlSchema.GetRootElementName(this.GetType()));
writer.WriteAttributeString("name", _Name.ToString());
writer.WriteAttributeString("countryId", _CountryId.ToString());
writer.WriteAttributeString("sectorId", _SectorId.ToString());
writer.WriteAttributeString("id", _Id.ToString());
writer.WriteEndElement();
}



The question is;

When I use the XmlSerializer to serialize <Company> comes out twice.

BUT! When I get the same object over a web service I only get it once!?
Is the XmlRoot ignored? Why?

What I am looking for is a consistent way to serialize/deserialize my objects.

By the way, the generated objects in the web reference is something I delete and then add my
namespaces in "using"



Any advice is good. I am beginning to gnaw my arm off....


Regards / Greger
 
S

Steven Cheng[MSFT]

Hello Greger,

Thank you for posting in the MSDN newsgroup.

As for the custom class which implements IXmlSerializable interface's
serializing behavior, here are some of my understanding:

As for the IXmlSerializable.WriteXml method, it start from the inner
content of an existing wrapper element(as indicated in the MSDN document)

#IXmlSerializable.WriteXml Method
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.ixmlserial
izable.writexml.aspx

here is the statement I scratch from the document:
==================
The WriteXml implementation you provide should write out the XML
representation of the object. The framework writes a wrapper element and
positions the XML writer after its start. Your implementation may write its
contents, including child elements. The framework then closes the wrapper
element.
==================

That's why when we using xmlseriailzer to serialize a custom class
implementing the IXmlserializable interface, the output xml will has an
additional wrapper element in additional to the content we flush in
WriteXml method. And the name of this element can be controlled by the
"XmlRootAttribute" as you've done.

When the serializing happens in webservice, it still follow the rule MSDN
doc indicates(there is an additional wrapper element), however, the wrapper
element's name is not controlled by the "XmlRootAttribute"(I think you're
right on that it ignore the XmlRootAttribute), and we should specify the
element name for the seriailzed class object in WebMethod(on parameter or
return result ). e.g.

=======================
[WebMethod]
[return:XmlElement("CompanyResultRoot")]
public Company
GetOneCompany([XmlElement("CompanyParameterRoot")]Company incomp)
{
Company comp = new Company();
comp.ID = DateTime.Now.Ticks;
comp.Name = incomp.Name;

return comp;
}
=====================

The syntax is abit different depend on whether the object is a parameter or
return object.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hello Greger,

How are you doing on this issue or does my last reply helps you a little?
If there is anything else we can help, please feel free to post here.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
G

Greger

Hi Steven,


Strange, I wrote quite a big reply. the webclient must have timed out since
its not posted.

Your solution worked perfectly!
Big thanxs. Im running a tight ship right now thanxs to the IXmlSerializable
interface.


Cheers // Greger
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top