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

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
{
}
}

Related

XmlSerializer returns null

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; }
}
}

How to change schemas in an XML document with c#

I have an XML-file that looks like this:
<?xml version="1.0"?>
<paxml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<header>
<format>LÖNIN</format>
<version>2</version>
</header>
<tidtransaktioner>
<tidtrans anstid="1418">
<tidkod>SJK</tidkod>
<datum>2022-02-02T15:20:00</datum>
<timmar>0</timmar>
</tidtrans>
<tidtrans anstid="1418">
<tidkod>SJK</tidkod>
<datum>2022-02-21T10:40:00</datum>
<timmar>0</timmar>
</tidtrans>
</tidtransaktioner>
</paxml>
I need to change the second line:
<paxml xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
To be like this:
<paxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.paxml.se/2.0/paxml.xsd">
Does anyone have any idea how this can be done?
Here is my class for the XML:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace Logic.Shared.Models
{
// using System.Xml.Serialization;
// XmlSerializer serializer = new XmlSerializer(typeof(Paxml));
[XmlRoot(ElementName = "header", Namespace = "")]
public class Header
{
[XmlElement(ElementName = "format", Namespace = "")]
public string Format { get; set; }
[XmlElement(ElementName = "version", Namespace = "")]
public double Version { get; set; }
}
[XmlRoot(ElementName = "dimension", Namespace = "")]
public class Dimension
{
[XmlAttribute(AttributeName = "dim", Namespace = "")]
public int Dim { get; set; }
[XmlAttribute(AttributeName = "namn", Namespace = "")]
public string Namn { get; set; }
}
[XmlRoot(ElementName = "dimensioner", Namespace = "")]
public class Dimensioner
{
[XmlElement(ElementName = "dimension", Namespace = "")]
public List<Dimension> Dimension { get; set; }
}
[XmlRoot(ElementName = "resultatenhet", Namespace = "")]
public class Resultatenhet
{
[XmlAttribute(AttributeName = "dim", Namespace = "")]
public int Dim { get; set; }
[XmlAttribute(AttributeName = "id", Namespace = "")]
public int Id { get; set; }
[XmlAttribute(AttributeName = "namn", Namespace = "")]
public int Namn { get; set; }
}
[XmlRoot(ElementName = "resultatenheter", Namespace = "")]
public class Resultatenheter
{
[XmlElement(ElementName = "resultatenhet", Namespace = "")]
public List<Resultatenhet> Resultatenhet { get; set; }
}
[XmlRoot(ElementName = "tidtrans", Namespace = "")]
public class Tidtrans
{
[XmlElement(ElementName = "tidkod", Namespace = "")]
public string Tidkod { get; set; }
[XmlElement(ElementName = "datum", Namespace = "")]
public DateTime Datum { get; set; }
[XmlElement(ElementName = "timmar", Namespace = "")]
public double Timmar { get; set; }
[XmlAttribute(AttributeName = "anstid", Namespace = "")]
public int Anstid { get; set; }
[XmlText]
public string Text { get; set; }
[XmlElement(ElementName = "resenheter", Namespace = "")]
public Resenheter Resenheter { get; set; }
}
[XmlRoot(ElementName = "resenhet", Namespace = "")]
public class Resenhet
{
[XmlAttribute(AttributeName = "dim", Namespace = "")]
public int Dim { get; set; }
[XmlAttribute(AttributeName = "id", Namespace = "")]
public int Id { get; set; }
}
[XmlRoot(ElementName = "resenheter", Namespace = "")]
public class Resenheter
{
[XmlElement(ElementName = "resenhet", Namespace = "")]
public List<Resenhet> Resenhet { get; set; }
}
[XmlRoot(ElementName = "tidtransaktioner", Namespace = "")]
public class Tidtransaktioner
{
[XmlElement(ElementName = "tidtrans", Namespace = "")]
public List<Tidtrans> Tidtrans { get; set; }
}
[XmlRoot(ElementName = "dag", Namespace = "")]
public class Dag
{
[XmlAttribute(AttributeName = "datum", Namespace = "")]
public DateTime Datum { get; set; }
[XmlAttribute(AttributeName = "timmar", Namespace = "")]
public double Timmar { get; set; }
}
[XmlRoot(ElementName = "schema", Namespace = "")]
public class Schema
{
[XmlElement(ElementName = "dag", Namespace = "")]
public List<Dag> Dag { get; set; }
[XmlAttribute(AttributeName = "anstid", Namespace = "")]
public int Anstid { get; set; }
}
[XmlRoot(ElementName = "schematransaktioner", Namespace = "")]
public class Schematransaktioner
{
[XmlElement(ElementName = "schema", Namespace = "")]
public List<Schema> Schema { get; set; }
}
[XmlRoot(ElementName = "paxml", Namespace = "")]
public class Paxml
{
[XmlElement(ElementName = "header", Namespace = "")]
public Header Header { get; set; }
[XmlElement(ElementName = "dimensioner", Namespace = "")]
public Dimensioner Dimensioner { get; set; }
[XmlElement(ElementName = "resultatenheter", Namespace = "")]
public Resultatenheter Resultatenheter { get; set; }
[XmlElement(ElementName = "tidtransaktioner", Namespace = "")]
public Tidtransaktioner Tidtransaktioner { get; set; }
[XmlElement(ElementName = "schematransaktioner", Namespace = "")]
public Schematransaktioner Schematransaktioner { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "noNamespaceSchemaLocation", Namespace = "http://www.paxml.se/2.0/paxml.xsd")]
public string NoNamespaceSchemaLocation { get; set; }
[XmlText]
public string Text { get; set; }
}
}
This class is generated by an XML-file generator. As you can see I have been trying to add XML attributes to manually change the namespaces.
And this is the class for the logic:
using Logic.Mobigo.Services;
using Logic.Shared.Models;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace Logic.Services
{
public class XMLService
{
private readonly MobigoTimeService _mobigoTimeService;
private readonly MobigoUserService _mobigoUserService;
public XMLService(MobigoTimeService mobigoTimeService, MobigoUserService mobigoUserService)
{
_mobigoTimeService = mobigoTimeService;
_mobigoUserService = mobigoUserService;
}
public async Task<bool> Xml()
{
var timeRows = await _mobigoTimeService.List();
var xml = new Paxml();
xml.Header = new Header();
xml.Header.Format = "LÖNIN";
xml.Header.Version = 2.0;
var tidTrans = new List<Tidtrans>();
foreach (var timeRow in timeRows)
{
var user = await _mobigoUserService.GetByUserSign(timeRow.UserSign);
;
tidTrans.Add(new Tidtrans
{
Anstid = 1418, //int.Parse(user.EmployeeNr),
Tidkod = "SJK", //timeRow.TimeType.TimeCode,
//hur göra med datum vid månadsöverskridelse????
Datum = timeRow.StopDate,
Timmar = timeRow.TimeAmount,
});
}
xml.Tidtransaktioner = new Tidtransaktioner
{
Tidtrans = tidTrans,
};
var writer = new XmlSerializer(typeof(Paxml));
var path = "C:\\Work\\Git\\LIM\\PDF\\" + "testfile.xml";
System.IO.FileStream file = System.IO.File.Create(path);
writer.Serialize(file, xml);
file.Close();
return true;
}
}
}
You're not too far off, but you're muddling namespaces and values. You want an attribute called noNamespaceSchemaLocation in the namespace http://www.w3.org/2001/XMLSchema-instance with the value http://www.paxml.se/2.0/paxml.xsd. That would look like this:
[XmlAttribute(AttributeName = "noNamespaceSchemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
public string NoNamespaceSchemaLocation { get; set; } = "http://www.paxml.se/2.0/paxml.xsd";
And the root XML produced would look like this by default:
<paxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="http://www.paxml.se/2.0/paxml.xsd" />
This is semantically the same as what you require, and shouldn't cause any issues as a result. But if you really want to get rid of the namespace declaration for xsd, you can supply an XmlSerializerNamespaces that only has the namespace you need:
var ns = new XmlSerializerNamespaces();
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
var serializer = new XmlSerializer(typeof(Paxml));
serializer.Serialize(file, xml, ns);

Serializing XML With Same Element Name

I have been having some trouble serializing the following XML...
<Activity mc:Ignorable="sap sap2010 sads" x:Class="Main"
xmlns="http://schemas.microsoft.com/netfx/2009/xaml/activities"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sco="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<TextExpression.NamespacesForImplementation>
<sco:Collection x:TypeArguments="x:String">
<x:String>System.Activities</x:String>
<x:String>System.Activities.Statements</x:String>
<x:String>System.Activities.Expressions</x:String>
</sco:Collection>
</TextExpression.NamespacesForImplementation>
<TextExpression.ReferencesForImplementation>
<sco:Collection x:TypeArguments="AssemblyReference">
<AssemblyReference>System.Activities</AssemblyReference>
<AssemblyReference>Microsoft.VisualBasic</AssemblyReference>
<AssemblyReference>mscorlib</AssemblyReference>
</sco:Collection>
</TextExpression.ReferencesForImplementation>
</Activity>
I have created the following classes to serialize the first <TextExpression.NamespacesForImplementation> element and created similar classes to serialize the <TextExpression.ReferencesForImplementation> which work individually...
[XmlRoot(Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
public class Activity
{
[XmlAttribute(AttributeName = "Ignorable", Namespace = "http://schemas.openxmlformats.org/markup-compatibility/2006")]
public string Ignorable { get; set; }
[XmlAttribute(AttributeName = "Class", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
public string Class { get; set; }
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Xmlns { get; set; }
[XmlElement(ElementName = "TextExpression.NamespacesForImplementation", Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
public NamespacesForImplementation NamespacesForImplementation { get; set; }
[XmlElement(ElementName = "TextExpression.ReferencesForImplementation", Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
public ReferencesForImplementation ReferencesForImplementation { get; set; }
}
public class NamespacesForImplementation
{
[XmlElement(ElementName = "Collection", Namespace = "clr-namespace:System.Collections.ObjectModel;assembly=mscorlib")]
public StringCollection Collection { get; set; }
}
public class ReferencesForImplementation
{
[XmlElement(ElementName = "Collection", Namespace = "clr-namespace:System.Collections.ObjectModel;assembly=mscorlib")]
public ReferencesCollection Collection { get; set; }
}
public class StringCollection
{
[XmlAttribute(AttributeName = "TypeArguments", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
public string TypeArguments { get; set; }
[XmlElement(ElementName = "String", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
public List<string> String { get; set; }
}
public class ReferencesCollection
{
[XmlAttribute(AttributeName = "TypeArguments", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
public string TypeArguments { get; set; }
[XmlElement(ElementName = "AssemblyReference", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
public List<string> AssemblyReference { get; set; }
}
The above XML is valid with appropriate namespaces. The issue arises when attempting to serialize both Collection elements, since they both have different inner elements but have the same element name. Any suggestions? I should also mention I have tried using the special paste option 'XML to C#' in Visual Studio 2017, but the result captured does not provide the input result once serialized and deserialized immediately after.
You do not need for each property in a class a value when serializing. See code below :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
Activity activity = new Activity() {
Ignorable = "sap sap2010 sads",
Class = "Main",
NamespacesForImplementation = new NamespacesForImplementation() {
Collection = new StringCollection() {
TypeArguments = "x:String",
String = new List<string>() {
"System.Activities", "System.Activities.Statements", "System.Activities.Expressions"
}
}
},
ReferencesForImplementation = new ReferencesForImplementation() {
Collection = new StringCollection() {
TypeArguments = "AssemblyReference",
AssemblyReference = new List<string>() {
"System.Activities", "Microsoft.VisualBasic", "mscorlib"
}
}
}
};
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(FILENAME, settings);
XmlSerializer serializer = new XmlSerializer(typeof(Activity));
serializer.Serialize(writer, activity);
}
}
[XmlRoot(Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
public class Activity
{
[XmlAttribute(AttributeName = "Ignorable", Namespace = "http://schemas.openxmlformats.org/markup-compatibility/2006")]
public string Ignorable { get; set; }
[XmlAttribute(AttributeName = "Class", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
public string Class { get; set; }
[XmlNamespaceDeclarations]
public XmlSerializerNamespaces Xmlns { get; set; }
[XmlElement(ElementName = "TextExpression.NamespacesForImplementation", Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
public NamespacesForImplementation NamespacesForImplementation { get; set; }
[XmlElement(ElementName = "TextExpression.ReferencesForImplementation", Namespace = "http://schemas.microsoft.com/netfx/2009/xaml/activities")]
public ReferencesForImplementation ReferencesForImplementation { get; set; }
}
public class NamespacesForImplementation
{
[XmlElement(ElementName = "Collection", Namespace = "clr-namespace:System.Collections.ObjectModel;assembly=mscorlib")]
public StringCollection Collection { get; set; }
}
public class ReferencesForImplementation
{
[XmlElement(ElementName = "Collection", Namespace = "clr-namespace:System.Collections.ObjectModel;assembly=mscorlib")]
public StringCollection Collection { get; set; }
}
public class StringCollection
{
[XmlAttribute(AttributeName = "TypeArguments", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
public string TypeArguments { get; set; }
[XmlElement(ElementName = "String", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
public List<string> String { get; set; }
[XmlElement(ElementName = "AssemblyReference", Namespace = "http://schemas.microsoft.com/winfx/2006/xaml")]
public List<string> AssemblyReference { 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);
}
}
}

Error when Deserializing XML into c# class derived from xsd.exe with namespace

I will start off with I have read all of the other answers to this question and all of them (albeit good solutions) did not work in my case
I created a c# class from my xsd file with
xsd.exe /c neworder.xsd
It generated a class of 7000+ lines so I'll post relevant parts of it.
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Xml;
using System.Collections.Generic;
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.foo.com/schemas/w/v1.0")]
[System.Xml.Serialization.XmlRootAttribute("new-order", Namespace = "http://www.foo.com/schemas/w/v1.0")]
public partial class neworder
{
private List<customertype> customersField;
private string suppliercodeField;
private string versionField;
private static System.Xml.Serialization.XmlSerializer serializer;
public neworder()
{
this.customersField = new List<customertype>();
}
[System.Xml.Serialization.XmlArrayAttribute(Order = 0)]
[System.Xml.Serialization.XmlArrayItemAttribute("customer",IsNullable = false)]
public List<customertype> customers
{
get
{
return this.customersField;
}
set
{
this.customersField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute("supplier-code")]
public string suppliercode
{
get
{
return this.suppliercodeField;
}
set
{
this.suppliercodeField = value;
}
}
[System.Xml.Serialization.XmlAttributeAttribute("version")]
public string version
{
get
{
return this.versionField;
}
set
{
this.versionField = value;
}
}
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(neworder));
}
return serializer;
}
}
public static neworder Deserialize(string xml)
{
System.IO.StringReader stringReader = null;
try
{
stringReader = new System.IO.StringReader(xml);
return ((neworder)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
}
finally
{
if ((stringReader != null))
{
stringReader.Dispose();
}
}
}
That is just a small snippet, the rest isn't that important right now as I feel like if this gets solved, I can solve the rest.
This is the beginning part of the XML file
<new-order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.foo.com/schema/w/v1.0" version="1.0" supplier-code="FAKECODE" schemaLocation="http://www.foo.com/schemas/w/v1.0/TransmissionEnvelope.xsd">
<customers>
<customer w-number="123456" customer-number="12345-12345">
<client-info>
<client-name>John Doe</client-name>
<w-id>433348</w-id>
</client-info>
<transferee-name>
<first>John</first>
<last>Doe</last>
</transferee-name>
<spouse-name>
<first />
<last />
</spouse-name>
<o-address>
<street1>123 Fake st</street1>
<city>Fakeville</city>
<state>CA</state>
<postal-code>90210</postal-code>
<country>USA</country>
</o-address>
<d-address>
<street1 />
<city>harbour</city>
<state>VA</state>
<postal-code>55555</postal-code>
<country>USA</country>
</d-address>
<contact-info>
<phone>
<phone-type>CELL</phone-type>
<phone-number>555-555-5555</phone-number>
</phone>
<phone>
<phone-type>HOME</phone-type>
<phone-number>555-555-5555</phone-number>
</phone>
<phone>
<phone-type>WORK</phone-type>
<phone-number />
</phone>
<email>johndoe#email.com</email>
<comments>Just any comments here</comments>
</contact-info>
</customer>
</customers>
</new-order>
I try to deserialize it with the following
XmlSerializer ser = new XmlSerializer(typeof(neworder));
neworder feed = (neworder)ser.Deserialize(new FileStream(filePath, FileMode.Open)) ;
The error that I get is the infamous:
There is an error in XML document (1, 2).
http://www.foo.com/schema/w/v1.0'> was not expected.
I've read over and over again about making sure the root note as attribute XMLROOT which the above does. And it has the right namespace.
I've tried changing XmlRootAttribute to XmlRoot. Nothing.
I've tried removing the namespace and re-doing the class and nothing.
Wrecking my brain on what could be wrong here because everything seems legit.
Try this.... (for the XML that you have posted)
Using.....
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
Classes.....
[XmlRoot(ElementName = "client-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Clientinfo
{
[XmlElement(ElementName = "client-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Clientname { get; set; }
[XmlElement(ElementName = "w-id", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Wid { get; set; }
}
[XmlRoot(ElementName = "transferee-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Transfereename
{
[XmlElement(ElementName = "first", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string First { get; set; }
[XmlElement(ElementName = "last", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Last { get; set; }
}
[XmlRoot(ElementName = "spouse-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Spousename
{
[XmlElement(ElementName = "first", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string First { get; set; }
[XmlElement(ElementName = "last", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Last { get; set; }
}
[XmlRoot(ElementName = "o-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Oaddress
{
[XmlElement(ElementName = "street1", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Street1 { get; set; }
[XmlElement(ElementName = "city", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string City { get; set; }
[XmlElement(ElementName = "state", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string State { get; set; }
[XmlElement(ElementName = "postal-code", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Postalcode { get; set; }
[XmlElement(ElementName = "country", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Country { get; set; }
}
[XmlRoot(ElementName = "d-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Daddress
{
[XmlElement(ElementName = "street1", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Street1 { get; set; }
[XmlElement(ElementName = "city", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string City { get; set; }
[XmlElement(ElementName = "state", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string State { get; set; }
[XmlElement(ElementName = "postal-code", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Postalcode { get; set; }
[XmlElement(ElementName = "country", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Country { get; set; }
}
[XmlRoot(ElementName = "phone", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Phone
{
[XmlElement(ElementName = "phone-type", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Phonetype { get; set; }
[XmlElement(ElementName = "phone-number", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Phonenumber { get; set; }
}
[XmlRoot(ElementName = "contact-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Contactinfo
{
[XmlElement(ElementName = "phone", Namespace = "http://www.foo.com/schema/w/v1.0")]
public List<Phone> Phone { get; set; }
[XmlElement(ElementName = "email", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Email { get; set; }
[XmlElement(ElementName = "comments", Namespace = "http://www.foo.com/schema/w/v1.0")]
public string Comments { get; set; }
}
[XmlRoot(ElementName = "customer", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Customer
{
[XmlElement(ElementName = "client-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Clientinfo Clientinfo { get; set; }
[XmlElement(ElementName = "transferee-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Transfereename Transfereename { get; set; }
[XmlElement(ElementName = "spouse-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Spousename Spousename { get; set; }
[XmlElement(ElementName = "o-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Oaddress Oaddress { get; set; }
[XmlElement(ElementName = "d-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Daddress Daddress { get; set; }
[XmlElement(ElementName = "contact-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Contactinfo Contactinfo { get; set; }
[XmlAttribute(AttributeName = "w-number")]
public string Wnumber { get; set; }
[XmlAttribute(AttributeName = "customer-number")]
public string Customernumber { get; set; }
}
[XmlRoot(ElementName = "customers", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Customers
{
[XmlElement(ElementName = "customer", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Customer Customer { get; set; }
}
[XmlRoot(ElementName = "new-order", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Neworder
{
[XmlElement(ElementName = "customers", Namespace = "http://www.foo.com/schema/w/v1.0")]
public Customers Customers { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "supplier-code")]
public string Suppliercode { get; set; }
[XmlAttribute(AttributeName = "schemaLocation")]
public string SchemaLocation { get; set; }
}
Code.....
string strXML = File.ReadAllText("xml.xml");
byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
MemoryStream ms1 = new MemoryStream(bufXML);
// Deserialize to object
XmlSerializer serializer = new XmlSerializer(typeof(Neworder));
try
{
using (XmlReader reader = new XmlTextReader(ms1))
{
Neworder deserializedXML = (Neworder)serializer.Deserialize(reader);
}// put a break point here and mouse-over deserializedXML….
}
catch (Exception ex)
{
throw;
}
I am reading your XML in to a string from a file in the application build folder called xml.xml... you will need to get the XML string from somewhere else or create the xml.xml file and save your XML for the code above to work

Categories