Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

I need some assistance please
I am trying to simply an XML import and need some advice

i have an XML which has parent nodes and some child nodes.
when my import runs i currently import my Parent node items into my C# object by using the
xmlSerializer.Deserialize(XMLString)


The object i am doing my load into is the client object


In my client object i have a property for order which is another class object in my Project

sample of my XML is

<client>
<name>x
<surname>y
<orders>
<order>
<orderno>QA122
<order>
<order>
<orderno>QA125
<order>



so currently the Client object loads the values fine but the order object is null
i would like to know how could i get the Order object to load from the XML.

What I have tried:

StringReader stringreader = new StringReader(XMLString);
                    XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
                    Policy pol = (Policy)xmlSerializer.Deserialize(stringreader);
Posted
Updated 23-Jun-19 23:35pm
Comments
BillWoodruff 23-Jun-19 20:49pm    
without seeing the structure of 'Policy, and sample actual XML: I don't think we can assist you.

1 solution

The answer is obvious: your xml structure is invalid!

Proper xml should looks like:
XML
<clients>
  <client>
    <name>x</name>
    <surname>y</surname>
    <orders>
      <order>
        <orderno>QA122</orderno>
      </order>
      <order>
        <orderno>QA125</orderno>
      </order>
    </orders>
  </client>
</clients>

or:
XML
<clients>
  <client name="x" surname="y">
    <orders>
      <order orderno="QA122" />
      <order orderno="QA125" />
    </orders>
  </client>
</clients>


Depending on that, a set of classes have to be created. Then you'll be able to serialize/deserialize data.

For further details, please see:
Introducing XML Serialization | Microsoft Docs[^]
XmlSerializer Class (System.Xml.Serialization) | Microsoft Docs[^]
XML Serialization and Deserialization: Part 1[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900