Here are the steps I've taken so far to work with an XmlDocument being returned by a 3rd party DLL.
- I saved the XmlDocument as SegmentationSummary.xml.
- I used XSD.exe to create SegmentationSummary.xsd.
- I used XSD.exe to create SegmentationSummary.cs.
Here is a sample of SegmentationSummary.cs. Note that ShmResult is the root node representation.
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "omitted")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "omitted", IsNullable = false)]
public partial class ShmResult
{
private ShmResultDownloadDetail[] downloadDetailField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DownloadDetail")]
public ShmResultDownloadDetail[] DownloadDetail
{
get
{
return this.downloadDetailField;
}
set
{
this.downloadDetailField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "omitted")]
public partial class ShmResultDownloadDetail
{
private string modelCodeField;
/// <remarks/>
public string ModelCode
{
get
{
return this.modelCodeField;
}
set
{
this.modelCodeField = value;
}
}
}
Now, I wanted to use this to read the XmlDocument and begin working with the classes in SegmentationSummary.cs. Here's the code I wrote:
private XmlDocument _document;
SegmentationSummary.ShmResult _Result;
private void LoadXML()
{
XmlReader xmlRdr = new XmlNodeReader(_document);
System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(SegmentationSummary.ShmResult));
_Result = (SegmentationSummary.ShmResult)s.Deserialize(xmlRdr);
}
When LoadXML() is executed, I get exceptions of this variety:
Test method SegmentationSummaryHandlerTest.TestMethod1 threw exception: System.InvalidOperationException: Unable to generate a temporary class (result=1). error CS0030: Cannot convert type 'MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail[]' to 'MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail' error CS0029: Cannot implicitly convert type 'MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail' to 'MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail[]'
Now, the FAQ at http://msdn.microsoft.com/en-us/library/ms950721.aspx states the following:
Q: How do I serialize collections of objects?
A: The XmlSerializer throws an exception when the collection contains types that were not declared to the constructor of the XmlSerializer. You can:
Declare the types to the serializer by passing in a Type[] with the types to expect within the collection.
OR
- Implement a strongly-typed collection derived from System.Collections.CollectionBase with an indexer matching the Add() method.
My question is: Which of these is "best" and how do I go about implementing the solution?