Groups | Blog | Home
all groups > dotnet xml > december 2005 >

dotnet xml : "xsi:type" missing from serialization of object- XmlSerializer


geoffrobinson
12/1/2005 5:36:53 AM
Hi,

I'm serializing an object using XmlSerializer. It is serializing, but
we are getting errors upon deserialization.

We use the following code to serialize:

FileStream fs = new FileStream(NavCmdFile, FileMode.Create,
FileAccess.Write, FileShare.None);
XmlSerializer xmlFmt = new XmlSerializer(someObject.GetType());
xmlFmt.Serialize(fs, someObject);

One of the objects is a parameter and this is what you get:

<someObject_parameter>
<name>name</name>
<source>soucename</source>
<value type="xsd:int">1</value>
</someObject_parameter>

When we deserialize, this creates an error. When we edit one line it
deserializes correctly:
<value xsi:type="xsd:int">1</value>

Is there any other way to do this or any workaround which won't omit
the "xsi:" portion?

I appreciate any help in advance.

thanks,
Geoff
geoffrobinson
12/1/2005 7:47:13 AM
Here is a little more detail.

In the thing referenced in the object during the serialization is the
following:
[XmlElement(ElementName = "value")]
public ParameterValues Value;

which references this class:
[Serializable]
public class ParameterValues
{
[XmlAttribute("type")]
public string Type;

[XmlText]
public int Value;

public XParameterValues(int val)
{
Type = "xsd:int";
Value=val;
}

}

____

I'm thinking there may be some issues because an XML element references
another class. But everything seems to be serializing fine except for
the lack of the 'xsi:' before the 'type' tag.

thanks again,
Geoff
geoffrobinson
12/1/2005 8:54:02 AM
Many thanks. Worked like a charm.
Gabriel Lozano-Morán
12/1/2005 4:04:07 PM
And the value is it a property of type object?

Gabriel

[quoted text, click to view]

Gabriel Lozano-Morán
12/1/2005 5:17:39 PM
[XmlAttribute("type", Namespace =
"http://www.w3.org/2001/XMLSchema-instance")]
public string Type;

Gabriel Lozano-Morán
http://www.realdn.net

JMD
12/6/2005 9:07:14 AM
When you manually edit in the xsi: prefix, does it actually deserialize
correctly to the Type field in the object?

This bothers me because you specified a local (not in a namespace)
attribute named 'type', which is totally distinct from the global
xsi:type which has a special meaning in XML Schemas.

So it bothers me that you never got to the bottom of why it would not
deserialize with an attribute that happens to be named 'type'. How is
that different from all the other code out there that uses
[XmlAttribute]?
kingshang
1/9/2006 3:48:02 PM
test
"geoffrobinson" <geoffrobinson@gmail.com> ????
news:1133444213.486091.214810@g43g2000cwa.googlegroups.com...
[quoted text, click to view]

AddThis Social Bookmark Button