XmlSerializer returns null - c#

I have an XML that i am trying to serialize to a class but i am unable to fathom why the object is null.
null object
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<ns0:vendResponse xmlns:ns0="http://mynamespace.co/ns0">
<ns0:sequence>234532532221</ns0:sequence>
<ns0:CId>0</ns0:CId>
<ns0:RefId>202675454545453434343</ns0:RefId>
<ns0:origAcct>20.00</ns0:origAcct>
<ns0:destAcct>00087646564</ns0:destAcct>
<ns0:responseCode>0</ns0:responseCode>
<ns0:responseMessage>Successful</ns0:responseMessage>
</ns0:vendResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
var contentStream = File.ReadAllText("response.xml");
Envelope resultEnvelope = new Envelope();
using (var stringReader = new StringReader(contentStream))
{
using (XmlReader reader = new XmlTextReader(stringReader))
{
var serializer1 = new XmlSerializer(typeof(Envelope));
resultEnvelope = serializer1.Deserialize(reader) as Envelope;
}
}
the soap Envelope class generated in VS using paste XML as classes:

Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
using System.Data;
namespace ConsoleApplication23
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
Envelope envelope = (Envelope)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement("Body")]
public Body body { get; set; }
}
public class Body
{
[XmlElement(ElementName = "vendResponse", Namespace = "http://mynamespace.co/ns0")]
public VendResponse vendResponse { get; set; }
}
public class VendResponse
{
public string sequence { get; set; }
public string CId { get; set; }
public string RefId { get; set; }
public string origAcct { get; set; }
public string destAcct { get; set; }
public int responseCode { get; set; }
public string responseMessage { get; set; }
}
}

Related

C# | How do I change the namespace in an XML document?

So im struggling to change the namespace for specific nodes in an XML.
This is what i want to achieve:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.justapage.com/webservices/">
<SOAP-ENV:Body>
<ns1:products_Update>
<ns1:login>
...
</ns1:login>
<ns1:products>
<ns1:product>
...
</ns1:product>
</ns1:products>
</ns1:products_Update>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But the results always turns out like this:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.justapage.com/webservices/">
<SOAP-ENV:Body>
<SOAP-ENV:products_Update>
<SOAP-ENV:login>
...
</SOAP-ENV:login>
<SOAP-ENV:products>
<SOAP-ENV:product>
...
</SOAP-ENV:product>
</SOAP-ENV:products>
</SOAP-ENV:products_Update>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
And this is how i serialize it:
public static async Task<string> SerializeSendMessage<T>(this T toSerialize)
{
string returnString;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
using (StringWriter sw = new StringWriter())
using (XmlWriter writer = XmlWriter.Create(sw))
{
var xmlns = new XmlSerializerNamespaces();
xmlns.Add("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
xmlns.Add("ns1", "http://www.justapage.com/webservices/");
xsSubmit.Serialize(writer, toSerialize,xmlns);
returnString = sw.ToString(); // Your XML
}
return returnString;
}
The class:
[XmlRoot(ElementName="Envelope",IsNullable = false)]
public class ArticleEnvelope
{
[XmlElement(ElementName = "Body",Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName = "SOAP-ENV")]
public string SOAPENV { get; set; }
[XmlAttribute(AttributeName = "ns1")]
public string Ns1 { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "Body")]
public class Body
{
[XmlElement(ElementName = "products_Update")]
public ProductsUpdate ProductsUpdate { get; set; }
}
[XmlRoot(ElementName = "products_Update")]
public class ProductsUpdate
{
[XmlElement(ElementName = "login")]
public Login Login { get; set; }
[XmlElement(ElementName = "products")]
public Products Products { get; set; }
}
[XmlRoot(ElementName = "products")]
public class Products
{
[XmlElement(ElementName = "product")]
public List<Product> Product { get; set; }
}
}
I have tried to find some information about this but with no success and I now hope that someone here can point me into the right direction on how to acheive this.
Try following :
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace ConsoleApp2
{
class Program
{
static ArticleEnvelope envelope { get; set; }
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = Encoding.UTF8;
XmlSerializer xsSubmit = new XmlSerializer(typeof(ArticleEnvelope));
using (StringWriter sw = new StringWriter())
using (XmlWriter writer = XmlWriter.Create(sw))
{
var xmlns = new XmlSerializerNamespaces();
xmlns.Add("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
xmlns.Add("ns1", "http://www.justapage.com/webservices/");
xsSubmit.Serialize(writer, envelope, xmlns);
}
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public class ArticleEnvelope
{
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlText]
public string Text { get; set; }
}
public class Body
{
[XmlElement(ElementName = "products_Update", Namespace = "http://www.justapage.com/webservices/")]
public ProductsUpdate ProductsUpdate { get; set; }
}
public class ProductsUpdate
{
[XmlElement(ElementName = "login", Namespace = "http://www.justapage.com/webservices/")]
public Login Login { get; set; }
[XmlArray(ElementName = "products", Namespace = "http://www.justapage.com/webservices/")]
[XmlArrayItem(ElementName = "product", Namespace = "http://www.justapage.com/webservices/")]
public List<Product> Products { get; set; }
}
public class Login
{
}
public class Product
{
}
}

Deserialize XML same items with different tag

This is an XML example I want to deserialize and use in a Desktop App, where the user could insert an XML like this one:
<Settings>
<FileName>C:\Users\myniceuser\Desktop\hello.xmldoc</FileName>
<Zones>
<Row1>
<Address>2</Address>strong text
<Zone>2</Zone>
<Installed>True</Installed>
</Row1>
<Row2>
<Address>3</Address>
<Zone>2</Zone>
<Installed>True</Installed>
</Row2>
<Row3>
<Address>4</Address>
<Zone>2</Zone>
<Installed>True</Installed>
</Row3>
<Row4>
<Address>5</Address>
<Zone>2</Zone>
<Installed>True</Installed>
</Row4>
</Zones>
<Network_IP>010.000.008.072</Network_IP>
<Ethernet_Enable>true</Ethernet_Enable>
<Language>1</Language>
</Settings>
The class I implemented is this one:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace MyApp.Models
{
[XmlRoot(ElementName = "Settings")]
public class XML_Settings
{
[XmlElement(ElementName = "FileName")]
public string FileName { get; set; }
//[XmlElement(ElementName = "Zones")]
[XmlElement(ElementName ="Zones")]
public Zones_Points[] Zones_Points { get; set; }
[XmlElement(ElementName = "Network_IP")]
public string NetworkIP { get; set; }
[XmlElement(ElementName = "Ethernet_Enable")]
public bool EthernetEnable { get; set; }
[XmlElement(ElementName = "Language")]
public int Language { get; set; }
}
[XmlRoot(ElementName = "")]
public partial class Zones_Points
{
[XmlElement(ElementName ="Address")]
public int Address { get; set; }
[XmlElement(ElementName ="Zone")]
public int PointZone { get; set; }
[XmlElement(ElementName ="Installed")]
public bool Installed { get; set; }
}
}
I deserialize like this:
Models.XML_Settings inputted_xml = new();
using StreamReader stream_xml = new StreamReader(xmlfile_path, CodePagesEncodingProvider.Instance.GetEncoding(1253));
XmlSerializer serializer = new XmlSerializer(typeof(Models.XML_Settings));
try
{
Models.XML_Settings input = (Models.XML_GR6500)serializer.Deserialize(stream_xml);
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
I want my app to input files in XML like this one, and convert their data to a class that i can use.
The object "input" created by the Deserialization got every data correct, except the Zones, those that in XML have the "Row" tags. Those "rows" could every time be more or less that 4.
What can I do to have the items with the row tags deserialized and inputted into a Zones_Points list or array?
Use IXmlSerializable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(XML_Settings));
XML_Settings settings = (XML_Settings)serializer.Deserialize(reader);
}
}
[XmlRoot("Settings")]
public class XML_Settings
{
[XmlElement(ElementName = "FileName")]
public string FileName { get; set; }
[XmlElement(ElementName = "Zones")]
public Zones_Points Zones_Point { get; set; }
[XmlElement(ElementName = "Network_IP")]
public string NetworkIP { get; set; }
[XmlElement(ElementName = "Ethernet_Enable")]
public bool EthernetEnable { get; set; }
[XmlElement(ElementName = "Language")]
public int Language { get; set; }
}
public partial class Zones_Points : IXmlSerializable
{
// Xml Serialization Infrastructure
[XmlElement(ElementName = "Zones")]
public List<Zones_Point> Points { get; set; }
public void WriteXml(XmlWriter writer)
{
}
public void ReadXml(XmlReader reader)
{
XElement zones = (XElement)XElement.ReadFrom(reader);
Points = new List<Zones_Point>();
foreach (XElement row in zones.Elements())
{
Zones_Point Point = new Zones_Point();
Points.Add(Point);
Point.Address = (int)row.Element("Address");
Point.PointZone = (int)row.Element("Zone");
Point.Installed = (Boolean)row.Element("Installed");
}
}
public XmlSchema GetSchema()
{
return (null);
}
}
public partial class Zones_Point
{
[XmlElement(ElementName = "Address")]
public int Address { get; set; }
[XmlElement(ElementName = "Zone")]
public int PointZone { get; set; }
[XmlElement(ElementName = "Installed")]
public bool Installed { get; set; }
}
}

Ds-serializing Xml to POCO objects array/list in C#

I have a xml files in following format:
<?xml version=""1.0"" encoding=""UTF-8""?>
<Options>
<Header>
<H1>A</H1>
<H2>B</H2>
<H3>C</H3>
</Header>
<Include>
<Option A1="something" A2="something">SomeValue</Option>
<Option A1="something" A2="something">SomeOtherValue</Option>
</Include>
</Options>
I managed to use a serializer to de-serialize headers, but run into trouble with options. Specifically I have the following C# objects:
[XmlRoot(ElementName="Option")]
public class Option {
[XmlAttribute(AttributeName="A1")]
public string A1 { get; set; }
[XmlAttribute(AttributeName="A2")]
public string A2 { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName="Include")]
public class OptionList {
[XmlArrayItem("Option")]
public List<Option> Options { get; set; }
}
And I use this serializer to do conversion:
public IReadOnlyList<Option> ParseInclude(string xmlDocument)
{
using (var reader = new XmlTextReader(new StringReader(xmlDocument)))
{
var serializer = new XmlSerializer(typeof(OptionList));
reader.ReadToFollowing("Include");
var collection = (OptionList) serializer.Deserialize(reader);
return collection .Options ;
}
}
But I cannot get it work - I am constantly getting NullReferenceException at the return statement because Options is null. Any ideas?
Try following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string xmlDocument = File.ReadAllText(FILENAME);
using (XmlReader reader = XmlReader.Create(new StringReader(xmlDocument)))
{
XmlSerializer serializer = new XmlSerializer(typeof(Option));
Option collection = (Option)serializer.Deserialize(reader);
}
}
}
[XmlRoot(ElementName = "Options")]
public class Option
{
public Header Header { get; set; }
[XmlArray("Include")]
[XmlArrayItem("Option")]
public List<OptionList> Options { get; set; }
}
public class Header
{
public string H1 { get; set; }
public string H2 { get; set; }
public string H3 { get; set; }
}
public class OptionList
{
[XmlAttribute(AttributeName = "A1")]
public string A1 { get; set; }
[XmlAttribute(AttributeName = "A2")]
public string A2 { get; set; }
[XmlText]
public string Text { get; set; }
}
}

How do I Deserialize this XML document in C#

This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<Fruit>
<Fruit_group name='Tropical'>
<fruit_types name ='Tropical Used'>
<fruit>bananas</fruit>
<fruit>mangoes</fruit>
</fruit_types>
</Fruit_group>
<Fruit_group name='Citrus'>
<fruit_types name ='Citruses Used'>
<fruit>orange</fruit>
<fruit>lime</fruit>
<fruit>grapefruit</fruit>
<excluded_fruits>
<fruit>mandarin</fruit>
</excluded_fruits>
</fruit_types>
</Fruit_group>
</Fruit>
And this is a sample of my XML text..
Is there a way to deserialize it and keep the name of the elements too..
I mean I would like to have like:
Tropical -> Tropical Used -> fruit -> bananas, mangoes
Citrus->Citruses Used ->
fruit = orange, lime, grapefruit
excluded = mandarin....
Something like this...
Could someone help me to understand how does this work?
You can use XML to c# website to create a class according to XML.
In this case, I've got this example
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="fruit_types")]
public class Fruit_types {
[XmlElement(ElementName="fruit")]
public List<string> Fruit { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
[XmlElement(ElementName="excluded_fruits")]
public Excluded_fruits Excluded_fruits { get; set; }
}
[XmlRoot(ElementName="Fruit_group")]
public class Fruit_group {
[XmlElement(ElementName="fruit_types")]
public Fruit_types Fruit_types { get; set; }
[XmlAttribute(AttributeName="name")]
public string Name { get; set; }
}
[XmlRoot(ElementName="excluded_fruits")]
public class Excluded_fruits {
[XmlElement(ElementName="fruit")]
public string Fruit { get; set; }
}
[XmlRoot(ElementName="Fruit")]
public class Fruit {
[XmlElement(ElementName="Fruit_group")]
public List<Fruit_group> Fruit_group { get; set; }
}
}
Also, You could read this article about XML deserialization.
I suggest using xmltocsharp to convert the XML to Model and do required alterations. For example change the string to List<string> under Excluded_fruits
[XmlRoot(ElementName = "fruit_types")]
public class Fruit_types
{
[XmlElement(ElementName = "fruit")]
public List<string> Fruit { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "excluded_fruits")]
public Excluded_fruits Excluded_fruits { get; set; }
}
[XmlRoot(ElementName = "Fruit_group")]
public class Fruit_group
{
[XmlElement(ElementName = "fruit_types")]
public Fruit_types Fruit_types { get; set; }
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "excluded_fruits")]
public class Excluded_fruits
{
[XmlElement(ElementName = "fruit")]
public List<string> Fruit { get; set; }
}
[XmlRoot(ElementName = "Fruit")]
public class Fruit
{
[XmlElement(ElementName = "Fruit_group")]
public List<Fruit_group> Fruit_group { get; set; }
}
Once the model is ready, you can use XmlSerializer to Deserialize the xml file
using (StreamReader r = new StreamReader(xmlfilepath))
{
string xmlString = r.ReadToEnd();
XmlSerializer ser = new XmlSerializer(typeof(Fruit));
using (TextReader reader = new StringReader(xmlString))
{
var fruits = (Fruit)ser.Deserialize(reader);
foreach(var fruitgroup in fruits.Fruit_group)
{
Console.Write($"{fruitgroup.Name} -> ");
Console.Write($"{fruitgroup.Fruit_types.Name} -> ");
Console.Write($"fruit = {string.Join(",", fruitgroup.Fruit_types.Fruit.ToArray())}; ");
if (fruitgroup.Fruit_types.Excluded_fruits?.Fruit?.Count() > 0)
Console.Write($"excluded = {string.Join(",", fruitgroup.Fruit_types.Excluded_fruits.Fruit.ToArray())}{Environment.NewLine}");
else
Console.WriteLine();
}
}
}
I like getting flatter results doing a custom parsing using xml linq :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<Fruit_Group> groups = doc.Descendants("Fruit_group").Select(x => new Fruit_Group()
{
groupName = (string)x.Attribute("name"),
typeName = (string)x.Element("fruit_types").Attribute("name"),
fruits = x.Elements("fruit_types").Elements("fruit").Select(y => (string)y).ToArray(),
excluded = x.Descendants("excluded_fruits").Elements("fruit").Select(y => (string)y).ToArray()
}).ToList();
}
}
public class Fruit_Group
{
public string groupName { get; set;}
public string typeName { get; set; }
public string[] fruits { get; set; }
public string[] excluded { get; set; }
}
}

Extract Data from Soap XML with Namespaces

I am trying to extract the data contained the
in the a SOAP Response XML that I have that contains multiple and variable namespaces in an SSIS Script component using C#.
My XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns3:getCacheEntryResponse xmlns:ns3="http://com.vedaadvantage/dp3/Enterprise/StandardTradeCreditCommercial/SilverChef/IndividualCommercialService" xmlns:ns2="http://com.vedaadvantage/dp3/Enterprise/StandardTradeCreditCommercial/SilverChef" xmlns:ns4="http://com.vedaadvantage/dp3/Enterprise/StandardTradeCreditCommercial/SilverChef/IndividualCommercialDecision" xmlns:ns5="http://vedaadvantage.com/dp3/service/fault" xmlns:ns6="http://com/vedaadvantage/dp3/businessDecisionResultOverride" xmlns:ns7="http://com.vedaadvantage/dp3/connectors" xmlns:ns8="http://com.vedaadvantage/dp3/connectors/vedaxml/vedascore" xmlns:ns9="http://com.vedaadvantage/dp3/connectors/vedaxml/individualalerts">
<ns3:CacheEntry>
<cacheEntryData>&lt;Response xmlns="http://com.vedaadvantage/dp3/connectors/vedaxml/vedascore" xmlns:b="http://com.vedaadvantage/dp3/connectors"&gt;&lt;enquiryReport&gt;&lt;primaryMatch&gt;&lt;bureauReference&gt;805917662&lt;/bureauReference&gt;&lt;individual&gt;&lt;individualName&gt;&lt;familyName&gt;MOHAMMADINEJAD&lt;/familyName&gt;&lt;firstGivenName&gt;ALI&lt;/firstGivenName&gt;&lt;createDate&gt;2014-07-15&lt;/createDate&gt;&lt;/individualName&gt;&lt;gender&gt;male&lt;/gender&gt;&lt;dateOfBirth&gt;1982-01-06&lt;/dateOfBirth&gt;&lt;driversLicenceNumber&gt;&lt;countryCodes&gt;AU&lt;/countryCodes&gt;&lt;value/&gt;&lt;/driversLicenceNumber&gt;&lt;b:address&gt;&lt;b:unitNumber/&gt;&lt;b:streetNumber&gt;43&lt;/b:streetNumber&gt;&lt;b:property&gt;43&lt;/b:property&gt;&lt;b:streetName&gt;KING EDWARD&lt;/b:streetName&gt;&lt;b:streetType&gt;AVE&lt;/b:streetType&gt;&lt;b:suburb&gt;ALBION&lt;/b:suburb&gt;&lt;b:city/&gt;&lt;b:state&gt;VIC&lt;/b:state&gt;&lt;b:postcode&gt;3020&lt;/b:postcode&gt;&lt;b:countryCode&gt;AU&lt;/b:countryCode&gt;&lt;b:addressType&gt;residentialCurrent&lt;/b:addressType&gt;&lt;b:createDate&gt;2015-09-04&lt;/b:createDate&gt;&lt;/b:address&gt;&lt;/individual&gt;&lt;individualConsumerCreditFile&gt;&lt;creditEnquiry&gt;&lt;accountType&gt;&lt;accountType&gt;UA&lt;/accountType&gt;&lt;value&gt;Utilities&lt;/value&gt;&lt;/accountType&gt;&lt;role&gt;&lt;consumerRoleTypeCodesResponse&gt;principal&lt;/consumerRoleTypeCodesResponse&gt;&lt;/role&gt;&lt;enquiryAmount&gt;&lt;currencyCodeType/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/enquiryAmount&gt;&lt;coBorrower/&gt;&lt;creditEnquirer&gt;AGL ENGY SALES &amp; MKTG LTD&lt;/creditEnquirer&gt;&lt;clientReference/&gt;&lt;consumerCreditEnquiryTypeResponse&gt;creditApplication&lt;/consumerCreditEnquiryTypeResponse&gt;&lt;enquiryDate&gt;2015-09-04&lt;/enquiryDate&gt;&lt;/creditEnquiry&gt;&lt;creditEnquiry&gt;&lt;accountType&gt;&lt;accountType&gt;TC&lt;/accountType&gt;&lt;value&gt;Telecommunication Service&lt;/value&gt;&lt;/accountType&gt;&lt;role&gt;&lt;consumerRoleTypeCodesResponse&gt;principal&lt;/consumerRoleTypeCodesResponse&gt;&lt;/role&gt;&lt;enquiryAmount&gt;&lt;currencyCodeType/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/enquiryAmount&gt;&lt;coBorrower/&gt;&lt;creditEnquirer&gt;VODAFONE DECISION POINT&lt;/creditEnquirer&gt;&lt;clientReference&gt;1-DCQJWZG&lt;/clientReference&gt;&lt;consumerCreditEnquiryTypeResponse&gt;creditApplication&lt;/consumerCreditEnquiryTypeResponse&gt;&lt;enquiryDate&gt;2014-07-15&lt;/enquiryDate&gt;&lt;/creditEnquiry&gt;&lt;/individualConsumerCreditFile&gt;&lt;individualCommercialCreditFile&gt;&lt;creditEnquiry&gt;&lt;accountType&gt;&lt;commercialAccountTypeCodesResponse&gt;CR&lt;/commercialAccountTypeCodesResponse&gt;&lt;value&gt;Commercial Rental&lt;/value&gt;&lt;/accountType&gt;&lt;enquiryAmount&gt;&lt;currencyCodeType/&gt;&lt;value&gt;25000&lt;/value&gt;&lt;/enquiryAmount&gt;&lt;role&gt;&lt;commercialRoleTypeCodesResponse&gt;principal&lt;/commercialRoleTypeCodesResponse&gt;&lt;/role&gt;&lt;coBorrower/&gt;&lt;creditEnquirer&gt;GOGETTA EQUIPMENT FUNDING&lt;/creditEnquirer&gt;&lt;clientReference/&gt;&lt;commercialCreditEnquiryTypeResponse&gt;creditEnquiry&lt;/commercialCreditEnquiryTypeResponse&gt;&lt;enquiryDate&gt;2016-04-04&lt;/enquiryDate&gt;&lt;/creditEnquiry&gt;&lt;/individualCommercialCreditFile&gt;&lt;matchType&gt;strong&lt;/matchType&gt;&lt;/primaryMatch&gt;&lt;summaryData&gt;&lt;summary&gt;&lt;name&gt;enquiry-amount&lt;/name&gt;&lt;summaryType&gt;amount&lt;/summaryType&gt;&lt;currencyCode&gt;AUD&lt;/currencyCode&gt;&lt;value&gt;25000&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;age-of-file&lt;/name&gt;&lt;summaryType&gt;months&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;20&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;age-of-subject&lt;/name&gt;&lt;summaryType&gt;years&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;34&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;time-at-address&lt;/name&gt;&lt;summaryType&gt;months&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;7&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;time-at-employer&lt;/name&gt;&lt;summaryType&gt;months&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value/&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;defaults&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;total-value-of-outstanding-defaults&lt;/name&gt;&lt;summaryType&gt;amount&lt;/summaryType&gt;&lt;currencyCode&gt;AUD&lt;/currencyCode&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;defaults-paid&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;defaults-12&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;defaults-12-paid&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;defaults-12-unpaid&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;defaults-24-paid&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;defaults-24-unpaid&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;defaults-36-paid&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;defaults-36-unpaid&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;time-since-last-default&lt;/name&gt;&lt;summaryType&gt;months&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value/&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;total-credit-enquiries&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;3&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;credit-enquiries-1&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;1&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;credit-enquiries-3&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;1&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;credit-enquiries-6&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;1&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;credit-enquiries-12&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;2&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;credit-enquiries-60&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;3&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;time-since-last-enquiry&lt;/name&gt;&lt;summaryType&gt;months&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;7&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;telco-and-utility-defaults&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;telco-and-utility-defaults-12&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;telco-and-utility-enquiries&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;2&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;telco-and-utility-enquiries-6&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;telco-and-utility-enquiries-12&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;1&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;authorised-agents-enquiries-12&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;authorised-agents-enquiries-60&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;directorships-current&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;directorships-previous&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;judgements&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;proprietorships&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;adverse-on-file&lt;/name&gt;&lt;summaryType/&gt;&lt;currencyCode/&gt;&lt;value&gt;No&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;file-notes&lt;/name&gt;&lt;summaryType/&gt;&lt;currencyCode/&gt;&lt;value&gt;No&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;known-identities&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;1&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;bankruptcies&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;writs-and-summons&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;summary&gt;&lt;name&gt;external-administration-director&lt;/name&gt;&lt;summaryType&gt;count&lt;/summaryType&gt;&lt;currencyCode/&gt;&lt;value&gt;0&lt;/value&gt;&lt;/summary&gt;&lt;/summaryData&gt;&lt;scoreData&gt;&lt;score&gt;&lt;scorecardModel&gt;0303&lt;/scorecardModel&gt;&lt;relativeRisk&gt;2.3&lt;/relativeRisk&gt;&lt;vedascore1_1Index&gt;4.0863&lt;/vedascore1_1Index&gt;&lt;applicantOdds&gt;26.9&lt;/applicantOdds&gt;&lt;contributingFactor&gt;&lt;impact&gt;Greatly Decreases Risk&lt;/impact&gt;&lt;value&gt;Lack of Consumer Adverse Information&lt;/value&gt;&lt;/contributingFactor&gt;&lt;contributingFactor&gt;&lt;impact&gt;Moderately Decreases Risk&lt;/impact&gt;&lt;value&gt;Number of Consumer Credit Applications&lt;/value&gt;&lt;/contributingFactor&gt;&lt;contributingFactor&gt;&lt;impact&gt;Marginally Decreases Risk&lt;/impact&gt;&lt;value&gt;Individual Shopping Pattern&lt;/value&gt;&lt;/contributingFactor&gt;&lt;contributingFactor&gt;&lt;impact&gt;Marginally Decreases Risk&lt;/impact&gt;&lt;value&gt;Current and Historic Credit Type Sought&lt;/value&gt;&lt;/contributingFactor&gt;&lt;population&gt;&lt;populationOdds&gt;11.9&lt;/populationOdds&gt;&lt;/population&gt;&lt;vedaScore&gt;675&lt;/vedaScore&gt;&lt;percentile&gt;30&lt;/percentile&gt;&lt;scoreType&gt;VS 1.1 COMMERCIAL + CONSUMER&lt;/scoreType&gt;&lt;/score&gt;&lt;/scoreData&gt;&lt;/enquiryReport&gt;&lt;/Response&gt;</cacheEntryData>
<connectorGroup>vedaxml</connectorGroup>
<connectorId>vedascore</connectorId>
<connectorVersion>2014-08-28</connectorVersion>
<request>&lt;Request xmlns:ns2="http://com.vedaadvantage/dp3/connectors" xmlns="http://com.vedaadvantage/dp3/connectors/vedaxml/vedascore"&gt;&lt;product&gt;&lt;name&gt;vedascoreFinancialCommercialPlusConsumer1_1&lt;/name&gt;&lt;summary&gt;true&lt;/summary&gt;&lt;/product&gt;&lt;individual&gt;&lt;individualName&gt;&lt;familyName&gt;Mohammadinejad&lt;/familyName&gt;&lt;firstGivenName&gt;Ali&lt;/firstGivenName&gt;&lt;otherGivenName&gt;&lt;/otherGivenName&gt;&lt;/individualName&gt;&lt;gender&gt;unknown&lt;/gender&gt;&lt;dateOfBirth&gt;1982-01-06Z&lt;/dateOfBirth&gt;&lt;driversLicenceNumber&gt;&lt;countryCodes&gt;AU&lt;/countryCodes&gt;&lt;value&gt;06011982&lt;/value&gt;&lt;/driversLicenceNumber&gt;&lt;ns2:address&gt;&lt;ns2:addressType&gt;residentialCurrent&lt;/ns2:addressType&gt;&lt;ns2:unitNumber&gt;3 U&lt;/ns2:unitNumber&gt;&lt;ns2:streetNumber&gt;43&lt;/ns2:streetNumber&gt;&lt;ns2:property&gt;43&lt;/ns2:property&gt;&lt;ns2:streetName&gt;KING EDWARD&lt;/ns2:streetName&gt;&lt;ns2:streetType&gt;AVE&lt;/ns2:streetType&gt;&lt;ns2:suburb&gt;ALBION&lt;/ns2:suburb&gt;&lt;ns2:state&gt;VIC&lt;/ns2:state&gt;&lt;ns2:postcode&gt;3020&lt;/ns2:postcode&gt;&lt;ns2:countryCode&gt;AU&lt;/ns2:countryCode&gt;&lt;/ns2:address&gt;&lt;role&gt;principal&lt;/role&gt;&lt;/individual&gt;&lt;enquiry&gt;&lt;accountType&gt;&lt;accountType&gt;CR&lt;/accountType&gt;&lt;/accountType&gt;&lt;enquiryAmount&gt;&lt;currencyCodeType&gt;AUD&lt;/currencyCodeType&gt;&lt;value&gt;25000&lt;/value&gt;&lt;/enquiryAmount&gt;&lt;enquiryType&gt;creditEnquiry&lt;/enquiryType&gt;&lt;/enquiry&gt;&lt;customerReference&gt;GoGetta&lt;/customerReference&gt;&lt;/Request&gt;</request>
</ns3:CacheEntry>
</ns3:getCacheEntryResponse>
</soap:Body>
</soap:Envelope>
I have used http://xmltocsharp.azurewebsites.net/ to generate my C# classes which look like this:
[XmlRoot(ElementName = "CacheEntry", Namespace = "http://com.vedaadvantage/dp3/Enterprise/StandardTradeCreditCommercial/SilverChef/IndividualCommercialService")]
public class CacheEntry
{
[XmlElement(ElementName = "cacheEntryData")]
public string CacheEntryData { get; set; }
[XmlElement(ElementName = "connectorGroup")]
public string ConnectorGroup { get; set; }
[XmlElement(ElementName = "connectorId")]
public string ConnectorId { get; set; }
[XmlElement(ElementName = "connectorVersion")]
public string ConnectorVersion { get; set; }
[XmlElement(ElementName = "request")]
public string Request { get; set; }
}
[XmlRoot(ElementName = "getCacheEntryResponse", Namespace = "http://com.vedaadvantage/dp3/Enterprise/StandardTradeCreditCommercial/SilverChef/IndividualCommercialService")]
public class GetCacheEntryResponse
{
[XmlElement(ElementName = "CacheEntry", Namespace = "http://com.vedaadvantage/dp3/Enterprise/StandardTradeCreditCommercial/SilverChef/IndividualCommercialService")]
public CacheEntry CacheEntry { get; set; }
[XmlAttribute(AttributeName = "ns3", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ns3 { get; set; }
[XmlAttribute(AttributeName = "ns2", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ns2 { get; set; }
[XmlAttribute(AttributeName = "ns4", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ns4 { get; set; }
[XmlAttribute(AttributeName = "ns5", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ns5 { get; set; }
[XmlAttribute(AttributeName = "ns6", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ns6 { get; set; }
[XmlAttribute(AttributeName = "ns7", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ns7 { get; set; }
[XmlAttribute(AttributeName = "ns8", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ns8 { get; set; }
[XmlAttribute(AttributeName = "ns9", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Ns9 { get; set; }
}
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Body
{
[XmlElement(ElementName = "getCacheEntryResponse", Namespace = "http://com.vedaadvantage/dp3/Enterprise/StandardTradeCreditCommercial/SilverChef/IndividualCommercialService")]
public GetCacheEntryResponse GetCacheEntryResponse { get; set; }
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; }
[XmlAttribute(AttributeName = "soap", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Soap { get; set; }
}
Here is how I am trying to extract the cacheEntryData field but it is not retrieving the node list (xnList) and it is not returning anything in my foreach statement.
public override void CreateNewOutputRows()
{
string soap_resp = Variables.getCacheEntryRspXML.ToString();
soap_resp = soap_resp.Replace("\"\"", "\"");
System.Windows.Forms.MessageBox.Show(soap_resp);
XmlDocument agv = new XmlDocument();
agv.LoadXml(soap_resp);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(agv.NameTable);
nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
nsmgr.AddNamespace("ns3", "http://com.vedaadvantage/dp3/Enterprise/StandardTradeCreditCommercial/SilverChef/IndividualCommercialService");
XmlNodeList xnList = agv.SelectNodes("//ns3:CacheEntry", nsmgr);
foreach (XmlNode xn in xnList)
{
var cacheEntryData = xn["ns3:CacheEntry"].InnerText;
System.Windows.Forms.MessageBox.Show(Convert.ToString(cacheEntryData));
}
}
This is very unusual. The code looks like in was html encoded so I had to html decode.
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Net;
namespace ConsoleApplication73
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
string xml = File.ReadAllText(FILENAME);
CreateNewOutputRows(xml);
}
static public void CreateNewOutputRows(string xml)
{
XDocument doc = XDocument.Parse(xml);
XElement cacheEntryData = doc.Descendants().Where(x => x.Name.LocalName == "cacheEntryData").FirstOrDefault();
string cacheEntryDataXml = WebUtility.HtmlDecode(cacheEntryData.ToString());
XElement cacheEntryData2 = XElement.Parse(cacheEntryDataXml);
}
}
}

Categories