I have generated classes based on an Xml schema file (xsd) using the XSD.exe tool.
My goal is to:
- write an application that can parse XML documents
- fill objects (based on the generated classes) with the XML data in element nodes or attributes
- assemble the objects so they are contained in their parent
- add the resulting object to an arrayList
The problem I’m having is that I am not sure how to go about assigning an object to an object variable in the base class. I keep getting conversion errors when I compile indicating "Cannot implicitly convert type Part.[Object variable] to [Object of Same Type as Object Variable]"
The hierarchy of the xml schema is as follows:
<Part nbr=”73894” rev=”9847”>
<Location fac=”Detroit” station=”9782”>
<Step seq=”09”>
<BillOfMaterial><BillOfMaterial>
<Task></Task>
<Illustration></Illustration>
</Step>
</Location>
</Part>
For each element/node in the xml doc,
- I instantiate the corresponding XSD generated object
- assign attribute values to corresponding class property/field
- Next, I attempt to assign child objects to the parent object variables
- LASTLY, I add the object to an arraylist
The syntax/code for one of the generated classes is as follows:
public class PartLocationStep {
public PartLocationStepBillOfMaterials BillOfMaterials;
public PartLocationStepIllustration Illustration;
[System.Xml.Serialization.XmlElementAttribute("Task ")]
public PartLocationStepTask[] Task;
[System.Xml.Serialization.XmlAttributeAttribute
Form=System.Xml.Schema.XmlSchemaForm.Qualified, DataType="integer")]
public string sequence;
}
The syntax I am using is as follows:
partLocStep.BillOfMaterials = partLocStepBOM;
partLocStep.Illustration = partLocStepIllustration;
partLocStep.Task = partLocStepTask;
partLoc.Step = partLocStep;
part.Location = partLoc;
//Add Part Object to Parts collection
partList.Add(part)
Is it my syntax for assigning the individual objects back to the parent object property/variable? Do I need to modify code inside the generated classes? Is there an easier way to do what I am trying to do?
I am fishing for answers? Hopefully, someone out there can help me or point me in the right direction.
Thanks