Deserializing to .NET objects fail (C#)
Question asked by Siavash Mortazavi - 2/25/2020 at 11:13 PM
Answered
Hi, anyone knows why this code is failing in deserializing a piece of XML to my .NET objects? 
* The .NET models are created by LiquidXML, so they should be compatible.
* The same piece of code is successful in desrializing many other XML pieces to their .NET types.

// C# code:
string objectXml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><art:details xmlns:udf=\"url\" xmlns:file=\"url\" xmlns:art=\"url\"><art:artifact uri=\"url\" limsid=\"2-281\"><name>JeffsDNASample</name><type>Analyte</type><output-type>Analyte</output-type><parent-process uri=\"url\" limsid=\"24-356\"/><qc-flag>UNKNOWN</qc-flag><working-flag>true</working-flag><sample uri=\"url\" limsid=\"MCK13A1\"/><workflow-stages/></art:artifact><art:artifact uri=\"url\" limsid=\"BAS17A1PA1\"><name>ZTest</name><type>Analyte</type><output-type>Analyte</output-type><qc-flag>UNKNOWN</qc-flag><location><container uri=\"url\" limsid=\"27-101\"/><value>A:1</value></location><working-flag>true</working-flag><sample uri=\"url\" limsid=\"BAS17A1\"/><artifact-group name=\"Infinium LCG PGX V2\" uri=\"url\"/><workflow-stages><workflow-stage status=\"COMPLETE\" name=\"Batching V4\" uri=\"url\"/><workflow-stage status=\"QUEUED\" name=\"Batching V4\" uri=\"url\"/></workflow-stages></art:artifact></art:details>";

LxSerializer<DetailsCt> serializer = new LxSerializer<DetailsCt>();

using (StringReader stringReader = new StringReader(objectXml))
{
    LxReaderSettings lxReaderSettings = new LxReaderSettings();
    
    var results = serializer.Deserialize(stringReader);                
}
Thanks

7 Replies

Reply to Thread
1
Liquid Support Replied
Employee Post
What is the error message in the Exception (and any inner exception)?
0
Siavash Mortazavi Replied
Thanks for the reply. There is no exception, but the child Artifiacts collection is not populated, and instead, UnprocessedElements has the XML objects:


I was wondering if the chilren artifact elements have bad XML format, so I tried to deserialize one of them like this:

string detailsXml = "..."; // exactly same as objectXml above
string artifactXml;
DetailsElm details;
ArtifactElm artifact;

LxSerializer<DetailsElm> detailsSerializer = new LxSerializer<DetailsElm>();
LxSerializer<ArtifactElm> artifactSerializer = new LxSerializer<ArtifactElm>();

using (StringReader detailsReader = new StringReader(detailsXml))
{
    details = detailsSerializer.Deserialize(detailsReader);
    artifactXml = details.UnprocessedElements[0].ToString();
}

using (StringReader artifactReader = new StringReader(artifactXml))
{
    artifact = artifactSerializer.Deserialize(artifactReader);
}

Interestingly, it gets deserialized correctly:


Any idea?

Thanks,
Sia

0
Siavash Mortazavi Replied
p.s. Although I can use the method above to retrieve all the artifacts I want, but it's not a robust solution. 
0
Liquid Support Replied
Employee Post
Hi Sia, this usually happens when the XML is not valid against the XSD. Additional items will then get put into the UnprocessedElements collection.

Can you please provide the XSD so I can take a look?
0
Siavash Mortazavi Replied
Hi, sorry for the delayed reply, I was off for a few days.

That's interesting! This zipped file contains 2 files: https://1drv.ms/u/s!Ak352iEtdoEmioJLXM8I0Qfqltl4HQ?e=bJakN0 

  1. Both art:details and art:artifact are defined in 'artifact.xsd'.
  2. The returned XML is also attached as 'returned-result.xml' for easier navigation.
Thanks a lot! 
0
Siavash Mortazavi Replied
Ok, thanks for the tip, I used Liquid Studio and figured out that the XML is actually invalid:


The error message says 'artifact is not valid, and objects of type 'artifact' is expected! LOL
I guess it's a namespace conflict, right? 

Thanks.
1
Liquid Support Replied
Employee Post Marked As Answer
Yes it looks like a namespace issue.

if you have associated the XML document with your XSD, then the intellisense should be able to add the correct item in for you.

Also, you can right click the parent Element in the XML editor, and select 'Navigate to Definition' to jump to the exact place in the XSD which defines the element. Yous should then be able to see what the child item should look like.

Reply to Thread