Frage

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

War es hilfreich?

Lösung

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);
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top