Question

I have derived a class which will take an XML file and generate ExpandoObject dynamically for the XML being passed in utilizing Lists of ExpandoObjects and recursion.

Passing this back to be processed to retrieve data works, but the XML that I am retrieving from a legacy system is inconsistent. I can either get back a single node as such

<test>
 <insured>
  <a>BLAH BLAH BLAH</a>
  <b>BLAH BLAH BLAH</b>
  <c>BLAH BLAH BLAH</c>
 </insured>
</test>

or

<test>
 <insured>
  <a>BLAH BLAH BLAH</a>
  <b>BLAH BLAH BLAH</b>
  <c>BLAH BLAH BLAH</c>
 </insured>
 <insured>
  <a>BLAH BLAH BLAH</a>
  <b>BLAH BLAH BLAH</b>
  <c>BLAH BLAH BLAH</c>
 </insured>
 <insured>
  <a>BLAH BLAH BLAH</a>
  <b>BLAH BLAH BLAH</b>
  <c>BLAH BLAH BLAH</c>
 </insured>
</test>

So for the key 'insured' I am either getting an ExpandoObject or List<ExpandoObject>. I am trying to determine the simplest solution to determine the type being returned.

Ideally just something like x.insured.OfType<List<ExpandoObject>>() to return a bool or something of that nature.

Cheers

Was it helpful?

Solution

Looks like you may have resolve this with Jon Skeet's help, but in case you're wondering, you can use the is operator for this:

if(x.insured is List<ExpandoObject>) {
    foreach(dynamic item in x.insured) {
        DoSomething(item);
    }
} else {
    DoSomething(x.insured);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top