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

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

0
12/1/2005 1:36:53 PM
dotnet.xml 7266 articles. 0 followers. Follow

6 Replies
1596 Views

Similar Articles

[PageSpeed] 52

And the value is it a property of type object?

Gabriel

"geoffrobinson" <geoffrobinson@gmail.com> wrote in message
news:1133444213.486091.214810@g43g2000cwa.googlegroups.com...
> 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
>


0
glozano (4)
12/1/2005 3:04:07 PM
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

0
12/1/2005 3:47:13 PM
[XmlAttribute("type", Namespace =
"http://www.w3.org/2001/XMLSchema-instance")]
public string Type;

Gabriel Lozano-Mor´┐Żn
http://www.realdn.net


0
glozano (4)
12/1/2005 4:17:39 PM
Many thanks. Worked like a charm.

0
12/1/2005 4:54:02 PM
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]?

0
11lrhap02 (5)
12/6/2005 5:07:14 PM
test
"geoffrobinson" <geoffrobinson@gmail.com> ????
news:1133444213.486091.214810@g43g2000cwa.googlegroups.com...
> 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
>


0
kingshang (1)
1/9/2006 7:48:02 AM
Reply:

Similar Artilces:

changing the xsi:type
I have an arraylist with up to 4 different classes that can be contained. e.g.... <carpark> <car /> <truck /> <motorcycle /> <rv /> </carpark> It's largely working. The output is:- <carpark> <anytype xsi:type="xmlcar" /> <anytype xsi:type="xmltruck" /> <anytype xsi:type="xmlmotorcycle" /> <anytype xsi:type="xmlrv" /> </carpark> I've tried changing the element name, the arrayitem name, but I can't get it to rename from "xmlcar" to ...

Redundant qualified xsi:type's
I have created a schema to write out some configuration data, and the OutPort element uses an abstract complex type (it can be a Message, Discrete, Continuous, Array, etc...). I use the provided XSD.exe tool with the /c option to generate the XML-attribute'd classes. When the data is written to disk is looks like this: <OutPort xmlns:q1="urn:MicroMax:MxVDev:3.x" xsi:type="q1:SignalDiscrete" Name="brd0/di0"> <Minimum>0</Minimum> <Maximum>1</Maximum> </OutPort...

"xsi:type" missing from serialization of object- XmlSerializer
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_param...

XmlValidatingReader invalid xsi:type
I have an abstract base class and two derived classes that I want to serialize and deserialize with schema validation. When I serialize instances of the derived classes the XmlSerializer adds the xsi:type="DerivedClass" attribute and the Instance Namespace. When I attempt to validate the xml upon deserialization the XmlValidatingReader chokes on this attribute value. I can't seem to find a way around this. Any suggestions? Could you post your final XSD and XML you are trying to validate using XmlValidatingReader ? "Jason Cartwright" <jason@cartwrightkids.com> w...

serialize c# class with xsi:type attribute
This may be a simple question, but I'm struggling to find the answer to it, so any suggestions would be greatly appreciated.... I want to know if there's some way to ensure that the xsi:type attribute is included when serializing a c# class. The basic situation is this: I have a class, ClassB which derives from ClassA. ClassC contains a list of ClassB, which I want to serialize as an XmlArray of 'bListItem's as per the code below: [Serializable] [XmlType("classA")] public class ClassA { private string _id; [XmlAttribute("id&quo...

understanding use of xsi:type
I have a bit of xml that I am trying to create an xsd schema from. A very simplified version is as follows: <?xml version="1.0" encoding="UTF-8"?> <centre xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:ucles.cbtas.model.Centre"> <number>25279</number> <customer-id>16</customer-id> </centre> The real xml is much more complex but the basic problem is the same. If I use xsd.exe to create a schema from this I get the following: <?xml version="1.0" encoding="utf-8"...

"xsi:type" from Dataset.GetXML
Hello, Is it possible to make the dataset give the "xsi:type" data as well?. I need result from the dataset.GetXML method something like this for the column "a": <a xsi:type="int">12</a> Is it possible?. I would appreciate any pointers/code help. Thanks. Ganesh, I use the XmlTypeAttribute for this. [XmlTypeAttribute("int")] My problem is the opposite. I don't want it to be there. Apparently, when you serialized derived classes the serialization framework takes it upon itself to specify the actual specialized class that the da...

Reading the value of an "xsi:type" attribute [unfortunate, but necessary cross-posting]
*Cross-posting from microsoft.public.dotnet.languages.csharp, since I believe the question is better suited in this XML group* Hello all, I'm having some problems understanding all the ins and outs with datasets and datatables (and navigating through the filled datatable)... Just when I thought I had gotten the hang of it, another problem arose: I can't seem to access the "xsi:type" attribute. That is, the XML file looks something like this: .... <situation> <objectID>123</objectId> <situationElement xsi:type="td:roadWorksType"> ....

xsi:type infopath
Hi, I have an XML Schema containing inherited complex types, and xs:elements using @type of the top complex type, meaning, any child complex type can be an instance of that element by declaring the complex type name using xsi:type . If I write the xml by hand, and validate against the xml schema, works really well. Using infopath I can import the schema and the data structure is displayed in the form designer, although there is no apparent way to allow users to select the xsi:type the instance of that element will be using. Has anybody used xsi:type with infopath before, or know any way to...

Deserializing with xsi:type="xsd:string"
I have a custom hashtable (a Dictionary<string, object>) for type-varying (database-derived) values with which I've implemented IXmlSerializable using XmlSerializer. The output of the value of a string typed as System.Object is like so: <anyType xsi:type="xsd:string" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">it works</anyType> When I use more or less the same XmlSerializer to deserialize this value, I get an error: System.InvalidOperationException: Namespace prefix 'xsd' is not defined. I see what's going on, bu...