Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I need help on the following. I have 2 classes generated from 2 schemas using xsd.exe. The object of one class is an embedded tag in the other class.

 

When i try to deserialize the xml file into local object, i only managed to deserialize the tags for the outer class but failed on the objects for embedded tags.

 

The problem lies in class didl.cs (the outer tag class). Within the statement class, the property Any is recognized as XMLElement. But i want it to be deserialized into DIA (inner class) object.

 

How do i achieve this? Can anyone help me on this? Thank you in advance.

 

 

 

 

Method invoked on the form load:

private void Form1_Load(object sender, System.EventArgs e)
{
   Type[] extraTypes = new Type[1];
   extraTypes[0] = typeof(DIA);
   DIDL didlObject = new DIDL();

   XmlSerializer s = new XmlSerializer( typeof( DIDL ), 
extraTypes );
   TextReader r = new StreamReader( 
@"C:\MultiSchema\akiyo_did.xml" );
   didlObject = (DIDL)s.Deserialize( r );
   r.Close();
}

 

Sample of xml to be deserialized:

(C:\MultiSchema\akiyo_did.xml)

<?xml version="1.0" encoding="UTF-8"?>
<did:DIDL xmlns="urn:mpeg:mpeg21:2003:01-DIA-NS" 
xmlns:mpeg7="urn:mpeg:mpeg7:schema:2001" 
xmlns:dii="urn:mpeg:mpeg21:2002:01-DII-NS" 
xmlns:did="urn:mpeg:mpeg21:2002:01-DIDL-NS" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="urn:mpeg:mpeg21:2002:01-DIDL-
NS ../schema/didl.xsd urn:mpeg:mpeg21:2002:01-DII-
NS ../schema/dii.xsd urn:mpeg:mpeg21:2003:01-DIA-
NS ../schema/BSDLink.xsd urn:mpeg:mpeg21:2003:01-DIA-
NS ../schema/AQoS.xsd urn:mpeg:mpeg21:2003:01-DIA-gBSD-
NS ../schema/gBSSchema.xsd">
<did:Item>
	<did:Component>
		<did:Descriptor>
			<did:Statement mimeType="text/xml">
				<DIA>
					<DescriptionMetadata>
					
<ClassificationSchemeAlias alias="SFO" 
href="urn:mpeg:mpeg21:2003:01-DIA-StackFunctionOperatorCS-NS"/>
					
<ClassificationSchemeAlias alias="AQS" 
href="urn:mpeg:mpeg21:2003:01-DIA-AdaptationQoSCS-NS"/>
				
</DescriptionMetadata>
				</DIA>
			</did:Statement>
		</did:Descriptor>
	</did:Component>
</did:Item>
</did:DIDL>

 

 

 

Class of outer object:

(didl.cs)

namespace MultiSchema 
{
using System.Xml.Serialization;
[system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS")]
[system.Xml.Serialization.XmlRootAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS", IsNullable=false)]
public class DIDL 
{
	[system.Xml.Serialization.XmlElementAttribute
("Item", typeof(Item))]
	public object Item;
}

[system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS")]
[system.Xml.Serialization.XmlRootAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS", IsNullable=false)]
public class Descriptor 
{ 
	[system.Xml.Serialization.XmlElementAttribute
("Component", typeof(Component))]
	[system.Xml.Serialization.XmlElementAttribute
("Statement", typeof(Statement))]
	public object Item;
}
   
[system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS")]
[system.Xml.Serialization.XmlRootAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS", IsNullable=false)]
public class Component 
{
	[system.Xml.Serialization.XmlElementAttribute
("Descriptor")]
	public Descriptor[] Descriptor;
}
   
[system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS")]
[system.Xml.Serialization.XmlRootAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS", IsNullable=false)]
public class Statement 
{
	[system.Xml.Serialization.XmlAnyElementAttribute()]
	public System.Xml.XmlNode[] Any;
	//[system.Xml.Serialization.XmlElementAttribute
("DIA",typeof(DIA))]
	//public MultiSchema.DIA Any;
}
   
[system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS")]
[system.Xml.Serialization.XmlRootAttribute
(Namespace="urn:mpeg:mpeg21:2002:01-DIDL-NS", IsNullable=false)]
public class Item 
{
	[system.Xml.Serialization.XmlElementAttribute
("Descriptor")]
	public Descriptor[] Descriptor;

	[system.Xml.Serialization.XmlElementAttribute
("Component", typeof(Component))]
	[system.Xml.Serialization.XmlElementAttribute
("Item", typeof(Item))]
	public object[] Items;
}
}

 

Class of inner embedded object:

(dia.cs)

namespace MultiSchema 
{
   using System.Xml.Serialization;
   
   [system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2003:01-DIA-NS")]
   [system.Xml.Serialization.XmlRootAttribute
(Namespace="urn:mpeg:mpeg21:2003:01-DIA-NS", IsNullable=false)]
   public class DIA 
{
       public DescriptionMetadataType DescriptionMetadata;
       
       [system.Xml.Serialization.XmlElementAttribute("Reference", 
typeof(ReferenceType))]
       [system.Xml.Serialization.XmlElementAttribute("Description", 
typeof(DIADescriptionType))]
       public object[] Items;
   }
   
   [system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2003:01-DIA-NS")]
   public class DescriptionMetadataType : DIABaseType 
{
       [system.Xml.Serialization.XmlElementAttribute
("ClassificationSchemeAlias")]
       public DescriptionMetadataTypeClassificationSchemeAlias[] 
ClassificationSchemeAlias;
   }
   
   [system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2003:01-DIA-NS")]
   public class DescriptionMetadataTypeClassificationSchemeAlias : 
DIABaseType 
{
       [system.Xml.Serialization.XmlAttributeAttribute
(DataType="NMTOKEN")]
       public string alias;
       
       [system.Xml.Serialization.XmlAttributeAttribute
(DataType="anyURI")]
       public string href;
   }
   
   [system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2003:01-DIA-NS")]
   [system.Xml.Serialization.XmlIncludeAttribute(typeof
(DIADescriptionType))]
   [system.Xml.Serialization.XmlIncludeAttribute(typeof
(DescriptionMetadataTypeClassificationSchemeAlias))]
   [system.Xml.Serialization.XmlIncludeAttribute(typeof
(DescriptionMetadataType))]
   [system.Xml.Serialization.XmlRootAttribute("DIADescriptionUnit", 
Namespace="urn:mpeg:mpeg21:2003:01-DIA-NS", IsNullable=false)]
   public abstract class DIABaseType 
{
   }
   
   [system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2003:01-DIA-NS")]
   public class ReferenceType 
{
       [system.Xml.Serialization.XmlAttributeAttribute
(DataType="anyURI")]
       public string uri;
   }

   [system.Xml.Serialization.XmlTypeAttribute
(Namespace="urn:mpeg:mpeg21:2003:01-DIA-NS")]
   public abstract class DIADescriptionType : DIABaseType 
{
   }
}

Edited by PlausiblyDamp
Posted

Post your schemas

 

Can you post the relevant parts of your schemas (enclosed in [code][/code] tags)? From what the xsd tool has generated, it looks like Statement is defined in your schema as containing only "any element" - if this is the case, then the Statement object cannot contain elements of a specific type. To make the code behave as you want, you need to modify the schema to specify DIA elements specifically. Something like:

 

<xs:element name="Statement" type="typeStatement"/>
 
<xs:complexType name="typeStatement">
 <xs:sequence>
   <xs:element name="DIA" type="typeDIA"/>
   <xs:any minOccurs="0"/>
 </xs:sequence>
</xs:complexType>

 

If you post the schemas themselves it would be easier to give more specific advice.

 

Good luck :cool:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...