Sunday, 19 October 2014

Deserialize XML - Multiple Namespaces



I’m trying to deserialize an XML with multiple namespaces to an object in C#, and seem to be going round and round in circles, and not quite working with the XSD tool.


After creating the XSD files, I removed the xs:element for the related namespaces to generate the classes using XSD.


When I deserialize this to an object, I can get the main details (CD GUID, CD Title,CD Artist) fine, but can’t get it to include the , sections, nor the section with each individual


I understand that I need to add the namespaces back into the classes, else how will it know to pick them up?


But I am struggling to get the syntax right to deserialize the xml into an object, below I can use the modified class to get , but it comes through as null...


Would love some assistance with this one.


Example XML:



<?xml version="1.0" encoding="utf-8"?>
<CD GUID="c6f92727-cb2c-498d-b7cc-b702a1764fa4" Title="Thriller" Artist="Michael Jackson">
<Released Year="1982" xmlns="ns.CD" />
<Produced xmlns="ns.CD">
<Production Producer="Quincy Jones" Writer="Michael Jackson" />
<Label>Epic</Label>
<Year>1982</Year>
</Produced>
<Tracks xmlns="ns.CD">
<Track GUID="68c85d1e-f4ab-4fea-b9ca-b2c347e41778" Title="Wanna Be Startin' Somethin'" xmlns="ns.Track">
<Details Length="6:03" />
<Writer Main="Michael Jackson">
<Producer Main="Quincy Jones" />
</Writer>
</Track>
<Track GUID="90cb5489-77e7-4bab-ac03-28f0cde3a7ce" Title="Baby Be Mine" xmlns="ns.Track">
<Details Length="4:02" />
<Writer Main="Rod Temperton">
<Producer Main="Quincy Jones" />
</Writer>
</Track>
<Track GUID="c1b91bd2-abb3-48b4-952e-886321fb8cc6" Title="The Girl Is Mine" xmlns="ns.Track">
<Details Length="3:42" />
<Writer Main="Michael Jackson">
<Producer Main="Quincy Jones" />
</Writer>
</Track>
<ReleaseDetails GUID="773dc8d8-9d8d-45d1-affb-575de095d70f" xmlns="ns.Track">
<Releases xmlns="ns.Releases">
<ReleaseInformation GUID="192e28b4-e286-471a-b054-ceb180b1e9dd">
<Track GUID="c1b91bd2-abb3-48b4-952e-886321fb8cc6" />
<Detail Title="The Girl Is Mine" Version="Radio Edit" />
</ReleaseInformation>
</Releases>
</ReleaseDetails>
</Tracks>
</CD>


Class:



//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18444
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class CD {

private string gUIDField;
private string titleField;
private string artistField;
private NewDataSet m_Details;


/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string GUID {
get {
return this.gUIDField;
}
set {
this.gUIDField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Title {
get {
return this.titleField;
}
set {
this.titleField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Artist {
get {
return this.artistField;
}
set {
this.artistField = value;
}
}

[XmlElement(Namespace = "ns.CD")]
public NewDataSet _Details
{
get
{
return this.m_Details;
}
set
{
this.m_Details = value;
}
}

// Read Details:
private Released m_Released;
[XmlElement(Namespace = "ns.CD")]
public Released _Released
{
get { return this.m_Released; }
set { this.m_Released = value; }
}

// Read Tracks:
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class NewDataSet {

private CD[] itemsField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("CD")]
public CD[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}


}

No comments:

Post a Comment