0

<?xml version="1.0" encoding="UTF-8"?>
<EfxTransmit
 xmlns="http://www.....abc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www....def http://www....ghi">
 <EfxReport requestNumber="1" reportId="CNCONSUMERCREDITFILE">
  <CNConsumerCreditReports>
   ....I only care about these
   ....I only care about these
   ....I only care about these
  </CNConsumerCreditReports>
 </EfxReport>
</EfxTransmit>

I created an object schema that resemble this:

[XmlRoot("CNConsumerCreditReports")]
public class Data
{
    [XmlElement("CNConsumerCreditReport")]
    public CNConsumerCreditReports CNConsumerCreditReports { get; set; }
}

But in order for that schema to work, I have to manually delete the following stuffs from the XML string as well as its ending closing tags

<EfxTransmit
 xmlns="http://www.....abc"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www....def http://www....ghi">
 <EfxReport requestNumber="1" reportId="CNCONSUMERCREDITFILE">

But, I found that manually deleting those tags is troublesome and I am hoping to find a way to work around. I just don't know what or how. But I am sure there is a better way.

Any suggestion or snipped code is appreciated!

Thank you.

CB4
  • 690
  • 3
  • 13
  • 25
  • Check out http://stackoverflow.com/a/20479554/2779990 – Stinky Towel Dec 07 '16 at 15:40
  • Possible duplicate of [How do I Deserialize xml document with namespaces using XmlSerializer?](http://stackoverflow.com/questions/7795958/how-do-i-deserialize-xml-document-with-namespaces-using-xmlserializer) – Heretic Monkey Dec 07 '16 at 16:21

1 Answers1

1

Given there's barely anything else, it would be easier to deserialize the whole document than to try and extract the fragment you want.

You also need to match the namespaces in your model attributes to the namespaces in your XML.

So something like:

[XmlRoot(Namespace="http://www.....abc")]
public class EfxTransmit
{
    public EfxReport EfxReport { get; set; }
}

public class EfxReport
{
    public CNConsumerCreditReports CNConsumerCreditReports { get; set; }
}

public class CNConsumerCreditReports
{
    public CNConsumerCreditReport CNConsumerCreditReport { get; set; }
}

public class CNConsumerCreditReport
{
    // ...
}
CB4
  • 690
  • 3
  • 13
  • 25
Charles Mager
  • 25,735
  • 2
  • 35
  • 45