Here are the steps I have taken so far to work with the XmlDocument returned by a third-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 an example of SegmentationSummary.cs. Note that ShmResult is the root representation of the node.
/// <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 an XmlDocument and get started with classes in SegmentationSummary.cs. Here is 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 sort:
The SegmentationSummaryHandlerTest.TestMethod1 test method threw an exception: System.InvalidOperationException: Failed to create a temporary class (Result = 1). error CS0030: Could not convert 'MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail [] to' MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail 'error CS0029: implicitly convert.Meter.reloadCermulturetermnterrent AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail [] '
Now in the FAQ on http://msdn.microsoft.com/en-us/library/ms950721.aspx the following is indicated:
Q: How to serialize collections of objects?
A: XmlSerializer throws an exception when the collection contains types that were not declared by the XmlSerializer constructor. You can:
My question is: which one is the โbestโ and how can I implement this solution?
source share