Deserialize xml files into local objects

intersysop

Newcomer
Joined
Dec 24, 2006
Messages
1
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:
C#:
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)
Code:
<?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)
C#:
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)
C#:
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 
	{
    }
}
 
Last edited by a moderator:
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:

Code:
<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:
 
Back
Top