Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with XML Deserialization into XSD generated classes
    primarykey
    data
    text
    <p>I have a rather detailed xml file. Below is the top level nodes (I have included the ellipse as the lower level nodes are all well formed and properly filled with data):</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;config&gt; &lt;Models&gt;...&lt;/Models&gt; &lt;Data&gt;...&lt;/Data&gt; &lt;/config&gt; </code></pre> <p>I have created an xsd file from using the Visual Studio 2008 command prompt:</p> <pre><code>xsd sample.xml </code></pre> <p>This generates the xsd file just fine. I then auto generate classes from the xsd with the command:</p> <pre><code>xsd sample.xsd /classes </code></pre> <p>For the deserialization of the xml file into a class object, I'm using the read function in the helper class:</p> <pre><code>public class XmlSerializerHelper&lt;T&gt; { public Type _type; public XmlSerializerHelper() { _type = typeof(T); } public void Save(string path, object obj) { using (TextWriter textWriter = new StreamWriter(path)) { XmlSerializer serializer = new XmlSerializer(_type); serializer.Serialize(textWriter, obj); } } public T Read(string path) { T result; using (TextReader textReader = new StreamReader(path)) { XmlSerializer deserializer = new XmlSerializer(_type); result = (T)deserializer.Deserialize(textReader); } return result; } } </code></pre> <p>When attempting the deserialization with:</p> <pre><code>var helper = new XmlSerializerHelper&lt;configModels&gt;(); var obj = new configModels(); obj = helper.Read(filepath); </code></pre> <p>I receive an error that I have deduced is because the deserializer is looking for the 'Models' node but the corresponding class name was generated as a combination of the root node and the 'Model' node (configModels). Why are the class names generated like this?</p> <p>I tried to deserialize from the top node using:</p> <pre><code>var helper = new XmlSerializerHelper&lt;config&gt;(); var obj = new config(); obj = helper.Read(filepath); </code></pre> <p>Unfortunately, this the results in a slew of errors like the following:</p> <pre><code>System.InvalidOperationException was unhandled by user code Message="Unable to generate a temporary class (result=1). error CS0030: Cannot convert type 'Application.Lease[]' to 'Application.Lease' error CS0030: Cannot convert type 'Application.CashFlow[]' to 'Application.CashFlow' ...ect. </code></pre> <p>Can somebody steer me towards what I might be doing wrong with my xsd auto-generating?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload