InvalidOperationException when deserializing custom XML (lack of namespace) - c#

I have an XML document that starts with the following root:
<?xml version="1.0" encoding="UTF-8"?>
<JPK
xmlns="http://jpk.mf.gov.pl/wzor/2019/09/27/09271/"
xmlns:etd="http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2018/08/24/eD/DefinicjeTypy/">
<Naglowek>
<KodFormularza kodSystemowy="SomeCode" wersjaSchemy="1-0">SomeCode</KodFormularza>
<WariantFormularza>3</WariantFormularza>
<CelZlozenia>1</CelZlozenia>
<DataWytworzeniaJPK>2021-06-30T15:57:53</DataWytworzeniaJPK>
<DataOd>2021-05-01</DataOd>
<DataDo>2021-05-31</DataDo>
<KodUrzedu>0000</KodUrzedu>
</Naglowek>
<Podmiot1>
<IdentyfikatorPodmiotu>
<etd:NIP>111111</etd:NIP>
<etd:PelnaNazwa>SomeName</etd:PelnaNazwa>
</IdentyfikatorPodmiotu>
<AdresPodmiotu>
<etd:Wojewodztwo>voivodeship</etd:Wojewodztwo>
<etd:KodKraju>PL</etd:KodKraju>
<etd:Powiat>Danzig</etd:Powiat>
<etd:Gmina>Danzig</etd:Gmina>
<etd:Ulica>SomeStreet</etd:Ulica>
<etd:NrDomu>81</etd:NrDomu>
<etd:NrLokalu>1</etd:NrLokalu>
<etd:Miejscowosc>Danzig</etd:Miejscowosc>
<etd:KodPocztowy>10-101</etd:KodPocztowy>
</AdresPodmiotu>
</Podmiot1>
<!-- These can be many in the same element, there's no root for this list -->
<Faktura>
<KodWaluty>PLN</KodWaluty>
<P_1>2021-05-04</P_1>
<P_2A>11 / 1111</P_2A>
<P_3A>Some Company</P_3A>
<P_3B>Some Address</P_3B>
<P_3C>Some Name</P_3C>
<P_3D>Some Other Address</P_3D>
<P_4B>Phone1</P_4B>
<P_5B>Phone2</P_5B>
<P_13_1>1.00</P_13_1>
<P_14_1>1.25</P_14_1>
<P_15>SomeDecimalNumber</P_15>
<P_16>false</P_16>
<P_17>false</P_17>
<P_18>false</P_18>
<P_18A>false</P_18A>
<P_19>false</P_19>
<P_20>false</P_20>
<P_21>false</P_21>
<P_22>false</P_22>
<P_23>false</P_23>
<P_106E_2>false</P_106E_2>
<P_106E_3>false</P_106E_3>
<RodzajFaktury>InvoiceType</RodzajFaktury>
</Faktura>
<!-- These can be many in the same element, there's no root for this list -->
<FakturaCtrl>
<LiczbaFaktur>1</LiczbaFaktur>
<WartoscFaktur>2.00</WartoscFaktur>
</FakturaCtrl>
<!-- These can be many in the same element, there's no root for this list -->
<FakturaWiersz>
<P_2B>04/123</P_2B>
<P_7>Text</P_7>
<P_8B>1.000000</P_8B>
<P_9A>7.00</P_9A>
<P_11>7.00</P_11>
<P_12>11</P_12>
</FakturaWiersz>
<FakturaWierszCtrl>
<LiczbaWierszyFaktur>11</LiczbaWierszyFaktur>
<WartoscWierszyFaktur>11.2</WartoscWierszyFaktur>
</FakturaWierszCtrl>
</JPK>
It has capital letters. I have no influence on its definition, I need to adjust myself.
I've written a class for it:
[XmlRoot(ElementName = "JPK", Namespace = "http://jpk.mf.gov.pl/wzor/2019/09/27/09271/", IsNullable = false)]
public class Jpk
{
public Jpk() { }
[XmlElement(ElementName = "Naglowek")]
public JpkHeader Header { get; set; }
[XmlElement(ElementName = "Podmiot1")]
public JpkSubject Subject { get; set; }
[XmlElement(ElementName = "Faktura")]
public JpkInvoice[] Invoices { get; set; }
[XmlElement(ElementName = "FakturaCtrl")]
public JpkInvoiceControl[] InvoiceControls { get; set; }
[XmlElement(ElementName = "FakturaWiersz")]
public JpkInvoiceRow[] InvoiceRows { get; set; }
[XmlElement(ElementName = "FakturaWierszCtrl")]
public JpkInvoiceRowControl InvoiceRowControl { get; set; }
}
public class JpkHeader
{
[XmlElement(ElementName = "KodFormularza")]
public string FormCode { get; set; }
[XmlElement(ElementName = "WariantFormularza")]
public string Variant { get; set; }
[XmlElement(ElementName = "CelZlozenia")]
public int Purpose { get; set; }
[XmlElement(ElementName = "DataWytworzeniaJPK")]
public DateTime CreationDate { get; set; }
[XmlElement(ElementName = "DataOd")]
public DateTime DateFrom { get; set; }
[XmlElement(ElementName = "DataDo")]
public DateTime DateTo { get; set; }
[XmlElement(ElementName = "KodUrzedu")]
public string OfficeCode { get; set; }
}
public class JpkInvoice
{
[XmlElement(ElementName = "KodWaluty")]
public string CurrencyCode { get; set; }
public DateTime P_1 { get; set; }
public string P_2A { get; set; }
public string P_3A { get; set; }
public string P_3B { get; set; }
public string P_3C { get; set; }
public string P_3D { get; set; }
public string P_4B { get; set; }
public string P_5B { get; set; }
public decimal P_13_1 { get; set; }
public decimal P_14_1 { get; set; }
public decimal P_15 { get; set; }
public bool P_16 { get; set; }
public bool P_17 { get; set; }
public bool P_18 { get; set; }
public bool P_18A { get; set; }
public bool P_19 { get; set; }
public bool P_20 { get; set; }
public bool P_21 { get; set; }
public bool P_22 { get; set; }
public bool P_23 { get; set; }
public bool P_106E_2 { get; set; }
public bool P_106E_3 { get; set; }
[XmlElement(ElementName = "RodzajFaktury")]
public string InvoiceType { get; set; }
}
public class JpkInvoiceControl
{
[XmlElement(ElementName = "LiczbaFaktur")]
public int InvoiceAmount { get; set; }
[XmlElement(ElementName = "WartoscFaktur")]
public decimal InvoiceValue { get; set; }
}
public class JpkInvoiceRow
{
public string P_2B { get; set; }
public string P_7 { get; set; }
public double P_8B { get; set; }
public decimal P_9A { get; set; }
public decimal P_11 { get; set; }
public int P_12 { get; set; }
}
public class JpkInvoiceRowControl
{
[XmlElement(ElementName = "LiczbaWierszyFaktur")]
public int InvoiceRowAmount { get; set; }
[XmlElement(ElementName = "WartoscWierszyFaktur")]
public decimal InvoiceRowSum { get; set; }
}
public class JpkSubject
{
[XmlElement(ElementName = "IdentyfikatorPodmiotu")]
public SubjectID SubjectId { get; set; }
[XmlElement(ElementName = "AdresPodmiotu")]
public SubjectAddress Address { get; set; }
}
public class SubjectID
{
[XmlElement(ElementName = "etd:NIP")]
public string NIP { get; set; }
[XmlElement(ElementName = "etd:PelnaNazwa")]
public string FullName { get; set; }
}
public class SubjectAddress
{
[XmlElement(ElementName = "etd:KodKraju")]
public string CountryCode { get; set; }
[XmlElement(ElementName = "etd:Wojewodztwo")]
public string Province { get; set; }
[XmlElement(ElementName = "etd:Powiat")]
public string District { get; set; }
[XmlElement(ElementName = "etd:Gmina")]
public string Community { get; set; }
[XmlElement(ElementName = "etd:Ulica")]
public string StreetName { get; set; }
[XmlElement(ElementName = "etd:NrDomu")]
public int HouseNumber { get; set; }
[XmlElement(ElementName = "etd:NrLokalu")]
public int FlatNumber { get; set; }
[XmlElement(ElementName = "etd:Miejscowosc")]
public string City { get; set; }
[XmlElement(ElementName = "etd:KodPocztowy")]
public string PostalCode { get; set; }
}
And I have my code that deserializes it:
var serializer = new XmlSerializer(typeof(Etc), new XmlRootAttribute("JPK"));
var streamReader = new StreamReader(#"C:\_NotInGit\sample.xml");
var smth = (Jpk)serializer.Deserialize(streamReader);
When I run this code I get
System.InvalidOperationException: 'There is an error in XML document (2, 2).'
InvalidOperationException: was not expected.
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
at MyProject.Program.Main(String[] args) in C:\MyProject\Program.cs:line 15
I run my queries through the Internet:
I ensured that the XmlRoot element has the exact capitalized version I encounter in the file
I put the namespace in
I tried adding Serializable, no changes
I ensured I pass the XmlRootAttribute to the serializer constructor
My spider-sense tells me it might have to do with the second definition, but I'm not sure how to approach it. I tried to remove it (for science purposes), but it did not change the response.
Is there anything else I am missing?
Spoiler alert: It was (also) about the second namespace definition. Thankfully the accepted answer showed me how to declare it, and what to do with the etd part. That, and the removal of new XmlRootAttribute("JPK") from code.

When attempting to debug a XML deserialization issue, I find it helpful to serialize the class that I've created using some sample data. Then, I check if the data that I serialized (saved to a XML file) is the same as the XML file that I'm trying to deserialize.
In the XML data you posted, within Faktura, is the following:
<P_15>SomeDecimalNumber</P_15>
Then in class JpkInvoice is the following:
public decimal P_15 { get; set; }
This is an issue because the value in the XML file is a string, whereas the property is declared as decimal. Either the property data type needs to be string or the value in the XML file needs to be changed to a valid decimal value. For testing, in the XML, I replaced <P_15>SomeDecimalNumber</P_15> with <P_15>0</P_15>.
I've tested the following code with the XML that you provided - with the XML data modification described above. I changed the property names to match what's in the XML file as I find it easier that way. You can rename them if you like. Also, I prefer to use List instead of an array, so you'll notice that in the code as well.
In the code below, the following using statements are necessary:
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
Create a class (name: Jpk)
Class: Jpk
[XmlRoot(ElementName = "JPK", Namespace = "http://jpk.mf.gov.pl/wzor/2019/09/27/09271/", IsNullable = false)]
public class Jpk
{
[XmlElement(ElementName = "Naglowek")]
public JpkNaglowek Naglowek { get; set; } = new JpkNaglowek(); //Header
[XmlElement(ElementName = "Podmiot1")]
public JpkPodmiot1 Podmiot1 { get; set; } = new JpkPodmiot1(); //Subject
[XmlElement(ElementName = "Faktura")]
public List<JpkFaktura> Faktura { get; set; } = new List<JpkFaktura>(); //Invoices
[XmlElement(ElementName = "FakturaCtrl")]
public List<JpkFakturaCtrl> FakturaCtrl { get; set; } = new List<JpkFakturaCtrl>(); //InvoiceControls
[XmlElement(ElementName = "FakturaWiersz")]
public List<JpkFakturaWiersz> FakturaWiersz { get; set; } = new List<JpkFakturaWiersz>(); //InvoiceRows
[XmlElement(ElementName = "FakturaWierszCtrl")]
public List<JpkFakturaWierszCtrl> FakturaWierszCtrl { get; set; } = new List<JpkFakturaWierszCtrl>(); //InvoiceRowControl
}
Class: JpkFaktura
public class JpkFaktura
{
private string p_1 = string.Empty;
[XmlIgnore]
public DateTime P_1Dt { get; private set; } = DateTime.MinValue; //value of P_1 as DateTime
[XmlElement]
public string KodWaluty { get; set; }
[XmlElement]
public string P_1
{
get
{
return this.p_1;
}
set
{
this.p_1 = value;
//try to convert to DateTime
DateTime dt = DateTime.MinValue;
DateTime.TryParseExact(value, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
//set value
this.P_1Dt = dt;
}
}
[XmlElement]
public string P_2A { get; set; }
[XmlElement]
public string P_3A { get; set; }
[XmlElement]
public string P_3B { get; set; }
[XmlElement]
public string P_3C { get; set; }
[XmlElement]
public string P_3D { get; set; }
[XmlElement]
public string P_4B { get; set; }
[XmlElement]
public string P_5B { get; set; }
[XmlElement]
public decimal P_13_1 { get; set; }
[XmlElement]
public decimal P_14_1 { get; set; }
[XmlElement]
public decimal P_15 { get; set; }
[XmlElement]
public bool P_16 { get; set; } = false;
[XmlElement]
public bool P_17 { get; set; } = false;
[XmlElement]
public bool P_18 { get; set; } = false;
[XmlElement]
public bool P_18A { get; set; } = false;
[XmlElement]
public bool P_19 { get; set; } = false;
[XmlElement]
public bool P_20 { get; set; } = false;
[XmlElement]
public bool P_21 { get; set; } = false;
[XmlElement]
public bool P_22 { get; set; } = false;
[XmlElement]
public bool P_23 { get; set; } = false;
[XmlElement]
public bool P_106E_2 { get; set; } = false;
[XmlElement]
public bool P_106E_3 { get; set; } = false;
[XmlElement]
public string RodzajFaktury { get; set; }
}
Note: In the code above you'll notice a property named P_1Dt. It's a public property that holds the value of P_1 as DateTime. By specifying [XmlIgnore] this property will be ignored during deserialization/serialization. In the XML, the format of the data is yyyy-MM-dd (ex: 2021-05-01), which is why it's necessary to specify the data type as a string. If the data type is specified as DateTime, when serialized the data in the XML file would look like yyyy-MM-ddTHH:mm:ss (ex: 2021-05-01T00:00:00)
Class: JpkFakturaCtrl
public class JpkFakturaCtrl
{
[XmlElement]
public decimal LiczbaFaktur { get; set; }
[XmlElement]
public decimal WartoscFaktur { get; set; }
}
Class: JpkFakturaWiersz
public class JpkFakturaWiersz
{
[XmlElement]
public string P_2B { get; set; }
[XmlElement]
public string P_7 { get; set; }
[XmlElement]
public decimal P_8B { get; set; }
[XmlElement]
public decimal P_9A { get; set; }
[XmlElement]
public decimal P_11 { get; set; }
[XmlElement]
public int P_12 { get; set; }
}
Class: JpkFakturaWierszCtrl
public class JpkFakturaWierszCtrl
{
[XmlElement]
public decimal LiczbaWierszyFaktur { get; set; }
[XmlElement]
public decimal WartoscWierszyFaktur { get; set; }
}
Class: JpkNaglowek
public class JpkNaglowek
{
private string dataOd = string.Empty;
private string dataDo = string.Empty;
[XmlIgnore]
public DateTime DataDoDt { get; private set; } //value of DataDo as DateTime
[XmlIgnore]
public DateTime DataOdDt { get; private set; } //value of DataDo as DateTime
[XmlElement(ElementName = "KodFormularza")]
public JpkNaglowekKodFormularza KodFormularza { get; set; } = new JpkNaglowekKodFormularza(); //FormCode
[XmlElement(ElementName = "WariantFormularza")]
public string WariantFormularza { get; set; } //Variant
[XmlElement(ElementName = "CelZlozenia")]
public int CelZlozenia { get; set; } //Purpose
[XmlElement(ElementName = "DataWytworzeniaJPK")]
public DateTime DataWytworzeniaJPK { get; set; } //CreationDate - DateTime
//DateFrom
[XmlElement(ElementName = "DataOd")]
public string DataOd
{
get
{
return this.dataOd;
}
set
{
this.dataOd = value;
//try to convert to DateTime
DateTime dt = DateTime.MinValue;
DateTime.TryParseExact(value, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
//set value
this.DataOdDt = dt;
}
}
//DateTo
[XmlElement(ElementName = "DataDo")]
public string DataDo
{
get
{
return this.dataDo;
}
set
{
this.dataDo = value;
//try to convert to DateTime
DateTime dt = DateTime.MinValue;
DateTime.TryParseExact(value, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dt);
//set value
this.DataDoDt = dt;
}
}
[XmlElement(ElementName = "KodUrzedu")]
public string KodUrzedu { get; set; }
}
Note: In the code above you'll notice properties: DataDoDt and DataOdDt. They are public properties that hold the value of DataDo and DataOd respectively, as DateTime. By specifying [XmlIgnore] this property will be ignored during deserialization/serialization. In the XML, the format of the data is yyyy-MM-dd (ex: 2021-05-01), which is why it's necessary to specify the data type as a string. If the data type is specified as DateTime, when serialized the data in the XML file would look like yyyy-MM-ddTHH:mm:ss (ex: 2021-05-01T00:00:00)
Class: JpkNaglowekKodFormularza
public class JpkNaglowekKodFormularza
{
[XmlAttribute(AttributeName = "kodSystemowy")]
public string kodSystemowy { get; set; }
[XmlAttribute(AttributeName = "wersjaSchemy")]
public string wersjaSchemy { get; set; }
[XmlText]
public string Value { get; set; }
}
Class: JpkPodmiot1
public class JpkPodmiot1
{
[XmlElement(ElementName = "IdentyfikatorPodmiotu")]
public JpkPodmiot1IdentyfikatorPodmiotu IdentyfikatorPodmiotu { get; set; } = new JpkPodmiot1IdentyfikatorPodmiotu(); //SubjectId
[XmlElement(ElementName = "AdresPodmiotu")]
public JpkPodmiot1AdresPodmiotu AdresPodmiotu { get; set; } = new JpkPodmiot1AdresPodmiotu(); //Address
}
Class: JpkPodmiot1IdentyfikatorPodmiotu
[System.Xml.Serialization.XmlType(Namespace = "http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2018/08/24/eD/DefinicjeTypy/")]
public class JpkPodmiot1IdentyfikatorPodmiotu
{
[XmlElement(ElementName = "NIP")]
public string NIP { get; set; }
[XmlElement(ElementName = "PelnaNazwa")]
public string PelnaNazwa { get; set; } //FullName
}
In the code above, notice that the namespace was specified.
Class: JpkPodmiot1AdresPodmiotu
[System.Xml.Serialization.XmlType(Namespace = "http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2018/08/24/eD/DefinicjeTypy/")]
public class JpkPodmiot1AdresPodmiotu
{
[XmlElement(ElementName = "Wojewodztwo")]
public string Wojewodztwo { get; set; } //Province
[XmlElement(ElementName = "KodKraju")]
public string KodKraju { get; set; } //CountryCode
[XmlElement(ElementName = "Powiat")]
public string Powiat { get; set; } //District
[XmlElement(ElementName = "Gmina")]
public string Gmina { get; set; } //Community
[XmlElement(ElementName = "Ulica")]
public string Ulica { get; set; } //StreetName
[XmlElement(ElementName = "NrDomu")]
public Int32 NrDomu { get; set; } //HouseNumber
[XmlElement(ElementName = "NrLokalu")]
public Int32 NrLokalu { get; set; } //FlatNumber
[XmlElement(ElementName = "Miejscowosc")]
public string Miejscowosc { get; set; } //City
[XmlElement(ElementName = "KodPocztowy")]
public string KodPocztowy { get; set; } //PostalCode
}
In the code above, notice that the namespace was specified.
Here's a method that can be used to populate an instance of Jpk with some test data -- it's the data from the XML file that you specified above. It's useful for testing XML serialization.
CreateTestData
private Jpk CreateTestData()
{
Jpk jpk = new Jpk();
//Naglowek
jpk.Naglowek.KodFormularza.kodSystemowy = "SomeCode";
jpk.Naglowek.KodFormularza.wersjaSchemy = "1-0";
jpk.Naglowek.KodFormularza.Value = "SomeCode";
jpk.Naglowek.WariantFormularza = "3";
jpk.Naglowek.CelZlozenia = 1;
jpk.Naglowek.DataWytworzeniaJPK = DateTime.ParseExact("2021-06-30T15:57:53", "yyyy-MM-ddTHH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); //"2021-06-30T15:57:53"; //ToDo: change to DateTime
jpk.Naglowek.DataOd = "2021-05-01";
jpk.Naglowek.DataDo = "2021-05-31";
jpk.Naglowek.KodUrzedu = "0000";
//Podmiot1
jpk.Podmiot1.IdentyfikatorPodmiotu.NIP = "111111";
jpk.Podmiot1.IdentyfikatorPodmiotu.PelnaNazwa = "SomeName";
jpk.Podmiot1.AdresPodmiotu.Wojewodztwo = "voivodeship";
jpk.Podmiot1.AdresPodmiotu.KodKraju = "PL";
jpk.Podmiot1.AdresPodmiotu.Powiat = "Danzig";
jpk.Podmiot1.AdresPodmiotu.Gmina = "Danzig";
jpk.Podmiot1.AdresPodmiotu.Ulica = "SomeStreet";
jpk.Podmiot1.AdresPodmiotu.NrDomu = 81;
jpk.Podmiot1.AdresPodmiotu.NrLokalu = 1;
jpk.Podmiot1.AdresPodmiotu.Miejscowosc = "Danzig";
jpk.Podmiot1.AdresPodmiotu.KodPocztowy = "10-101";
//Faktura
JpkFaktura jpkFaktura = new JpkFaktura();
jpkFaktura.KodWaluty = "PLN";
jpkFaktura.P_1 = "2021-05-04";
jpkFaktura.P_2A = "11 / 1111";
jpkFaktura.P_3A = "Some Company";
jpkFaktura.P_3B = "Some Address";
jpkFaktura.P_3C = "Some Name";
jpkFaktura.P_3D = "Some Other Address";
jpkFaktura.P_4B = "Phone1";
jpkFaktura.P_5B = "Phone2";
jpkFaktura.P_13_1 = 1.00m; //need to use 'm' for decimal number
jpkFaktura.P_14_1 = 1.25m; //need to use 'm' for decimal number
jpkFaktura.P_15 = 0m; //need to use 'm' for decimal number
jpkFaktura.P_16 = false;
jpkFaktura.P_17 = false;
jpkFaktura.P_18 = false;
jpkFaktura.P_18A = false;
jpkFaktura.P_19 = false;
jpkFaktura.P_20 = false;
jpkFaktura.P_21 = false;
jpkFaktura.P_22 = false;
jpkFaktura.P_23 = false;
jpkFaktura.P_106E_2 = false;
jpkFaktura.P_106E_3 = false;
jpkFaktura.RodzajFaktury = "InvoiceType";
//add
jpk.Faktura.Add(jpkFaktura);
//FakturaCtrl
JpkFakturaCtrl jpkFakturaCtrl = new JpkFakturaCtrl();
jpkFakturaCtrl.LiczbaFaktur = 1m; //need to use 'm' for decimal number
jpkFakturaCtrl.WartoscFaktur = 2.00m; //need to use 'm' for decimal number
//add
jpk.FakturaCtrl.Add(jpkFakturaCtrl);
//FakturaWiersz
JpkFakturaWiersz jpkFakturaWiersz = new JpkFakturaWiersz();
jpkFakturaWiersz.P_2B = "04/123";
jpkFakturaWiersz.P_7 = "Text";
jpkFakturaWiersz.P_8B = 1.000000m; //need to use 'm' for decimal number
jpkFakturaWiersz.P_9A = 7.00m; //need to use 'm' for decimal number
jpkFakturaWiersz.P_11 = 7.00m; //need to use 'm' for decimal number
jpkFakturaWiersz.P_12 = 11;
//add
jpk.FakturaWiersz.Add(jpkFakturaWiersz);
//FakturaWierszCtrl
JpkFakturaWierszCtrl jpkFakturaWierszCtrl = new JpkFakturaWierszCtrl();
jpkFakturaWierszCtrl.LiczbaWierszyFaktur = 11m; //need to use 'm' for decimal number
jpkFakturaWierszCtrl.WartoscWierszyFaktur = 11.2m; //need to use 'm' for decimal number
//add
jpk.FakturaWierszCtrl.Add(jpkFakturaWierszCtrl);
return jpk;
}
Usage:
Jpk jpk1 = CreateTestData();
For XML serialization, use the following:
public static void SerializeObjectToXMLFile(object obj, string xmlFilename)
{
try
{
if (string.IsNullOrEmpty(xmlFilename))
{
return;
}//if
using (System.IO.TextWriter xmlStream = new System.IO.StreamWriter(xmlFilename))
{
//specify namespaces
System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces();
ns.Add(string.Empty, "http://jpk.mf.gov.pl/wzor/2019/09/27/09271/");
ns.Add("etd", "http://crd.gov.pl/xml/schematy/dziedzinowe/mf/2018/08/24/eD/DefinicjeTypy/");
//create new instance
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(obj.GetType());
//write XML to file
serializer.Serialize(xmlStream, obj, ns);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
throw ex;
}
}
Usage:
Jpk jpk1 = CreateTestData();
SerializeObjectToXMLFile(jpk1, #"C:\Temp\Test.xml");
For deserialization, use the following code:
public static T DeserializeXMLFileToObject<T>(string xmlFilename)
{
T rObject = default(T);
try
{
if (string.IsNullOrEmpty(xmlFilename))
{
return default(T);
}
using (System.IO.StreamReader xmlStream = new System.IO.StreamReader(xmlFilename))
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
rObject = (T)serializer.Deserialize(xmlStream);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
throw ex;
}
return rObject;
}
Usage:
Jpk jpk1 = DeserializeXMLFileToObject<Jpk>(#"C:\Temp\Test.xml");

Related

How do I deserialize an XML API response in ASP.NET?

I am working on a project that calls an API (using C# ASP.NET), and the API returns an XML document. I know how to deserialize an response in JSON, but am running into issues with deserializing an XML response. Currently I am getting the below error when attempting to deserialize the XML:
InvalidOperationException: response xmlns="" was not expected.
The error code displayed just the empty quotes in the error message. I have the code samples below, and I would greatly appreciate any constructive feedback or advice on making this better, and any advice on where I went wrong on attempting to deserialize this!
Here is the XML response from the API call (This API only returns either CML or a .csv):
<response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:noNamespaceSchemaLocation="http://www.aviationweather.gov/static/adds/schema/metar1_2.xsd">
<request_index>554967903</request_index>
<data_source name="metars"/>
<request type="retrieve"/>
<errors/>
<warnings/>
<time_taken_ms>7</time_taken_ms>
<data num_results="3">
<METAR>
<raw_text>KDEN 102353Z 15007KT 10SM FEW220 07/M08 A3038 RMK AO2 SLP285 T00721083 10133 20072 58015</raw_text>
<station_id>KDEN</station_id>
<observation_time>2022-01-10T23:53:00Z</observation_time>
<latitude>39.85</latitude>
<longitude>-104.65</longitude>
<temp_c>7.2</temp_c>
<dewpoint_c>-8.3</dewpoint_c>
<wind_dir_degrees>150</wind_dir_degrees>
<wind_speed_kt>7</wind_speed_kt>
<visibility_statute_mi>10.0</visibility_statute_mi>
<altim_in_hg>30.380905</altim_in_hg>
<sea_level_pressure_mb>1028.5</sea_level_pressure_mb>
<quality_control_flags>
<auto_station>TRUE</auto_station>
</quality_control_flags>
<sky_condition sky_cover="FEW" cloud_base_ft_agl="22000"/>
<flight_category>VFR</flight_category>
<three_hr_pressure_tendency_mb>-1.5</three_hr_pressure_tendency_mb>
<maxT_c>13.3</maxT_c>
<minT_c>7.2</minT_c>
<metar_type>METAR</metar_type>
<elevation_m>1656.0</elevation_m>
</METAR>
<METAR>
<raw_text>KSEA 102353Z 34003KT 6SM -RA BR FEW015 BKN035 OVC045 08/06 A3035 RMK AO2 SLP288 P0000 60001 T00780056 10083 20044 50003</raw_text>
<station_id>KSEA</station_id>
<observation_time>2022-01-10T23:53:00Z</observation_time>
<latitude>47.45</latitude>
<longitude>-122.32</longitude>
<temp_c>7.8</temp_c>
<dewpoint_c>5.6</dewpoint_c>
<wind_dir_degrees>340</wind_dir_degrees>
<wind_speed_kt>3</wind_speed_kt>
<visibility_statute_mi>6.0</visibility_statute_mi>
<altim_in_hg>30.351377</altim_in_hg>
<sea_level_pressure_mb>1028.8</sea_level_pressure_mb>
<quality_control_flags>
<auto_station>TRUE</auto_station>
</quality_control_flags>
<wx_string>-RA BR</wx_string>
<sky_condition sky_cover="FEW" cloud_base_ft_agl="1500"/>
<sky_condition sky_cover="BKN" cloud_base_ft_agl="3500"/>
<sky_condition sky_cover="OVC" cloud_base_ft_agl="4500"/>
<flight_category>VFR</flight_category>
<three_hr_pressure_tendency_mb>0.3</three_hr_pressure_tendency_mb>
<maxT_c>8.3</maxT_c>
<minT_c>4.4</minT_c>
<precip_in>0.005</precip_in>
<pcp6hr_in>0.01</pcp6hr_in>
<metar_type>METAR</metar_type>
<elevation_m>115.0</elevation_m>
</METAR>
<METAR>
<raw_text>PHNL 102353Z 19009KT 10SM FEW025 FEW035 SCT050 26/21 A2997 RMK AO2 SLP147 T02560206 10261 20200 58017</raw_text>
<station_id>PHNL</station_id>
<observation_time>2022-01-10T23:53:00Z</observation_time>
<latitude>21.33</latitude>
<longitude>-157.93</longitude>
<temp_c>25.6</temp_c>
<dewpoint_c>20.6</dewpoint_c>
<wind_dir_degrees>190</wind_dir_degrees>
<wind_speed_kt>9</wind_speed_kt>
<visibility_statute_mi>10.0</visibility_statute_mi>
<altim_in_hg>29.970472</altim_in_hg>
<sea_level_pressure_mb>1014.7</sea_level_pressure_mb>
<quality_control_flags>
<auto_station>TRUE</auto_station>
</quality_control_flags>
<sky_condition sky_cover="FEW" cloud_base_ft_agl="2500"/>
<sky_condition sky_cover="FEW" cloud_base_ft_agl="3500"/>
<sky_condition sky_cover="SCT" cloud_base_ft_agl="5000"/>
<flight_category>VFR</flight_category>
<three_hr_pressure_tendency_mb>-1.7</three_hr_pressure_tendency_mb>
<maxT_c>26.1</maxT_c>
<minT_c>20.0</minT_c>
<metar_type>METAR</metar_type>
<elevation_m>2.0</elevation_m>
</METAR>
</data>
</response>
This is the code to call the API:
private static readonly HttpClient client = new HttpClient();
static async Task Main(string[] args)
{
client.DefaultRequestHeaders.Accept.Clear();
string baseUrl = "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&stationString=KMSP&hoursBeforeNow=2";
client.BaseAddress = new Uri(baseUrl);
HttpResponseMessage result = client.GetAsync(baseUrl).Result;
XmlSerializer serialObject = new XmlSerializer(typeof(MetarReport));
List<MetarReport> MetarCollection = new List<MetarReport>();
if (result.IsSuccessStatusCode)
{
using (Stream reader = result.Content.ReadAsStreamAsync().Result)
{
MetarReport metar = new MetarReport();
metar = (MetarReport)serialObject.Deserialize(reader);
MetarCollection.Add(metar);
}
}
// Test deserializer
foreach(var item in MetarCollection)
{
Console.WriteLine(item.rawText);
}
Console.ReadLine();
}
And this is the object I am attempting to deserialize the XML into
[XmlRoot("METAR")]
[XmlType("METAR")]
public class MetarReport
{
[XmlElement("raw_text")]
public string rawText { get; set; }
[XmlElement("station_id")]
public string stationId { get; set; }
[XmlElement("latitude")]
public double latitiude { get; set; }
[XmlElement("longitude")]
public double longitude { get; set; }
[XmlElement("temp_c")]
public double tempCelsius { get; set; }
[XmlElement("dewpoint_c")]
public double dewpoint { get; set; }
[XmlElement("wind_dir_degree")]
public int windDirection { get; set; }
[XmlElement("wind_speed_kt")]
public double windspeed { get; set; }
[XmlElement("visibility_statute_mi")]
public double visbilityMiles { get; set; }
[XmlElement("altim_in_hg")]
public double altimeter { get; set; }
//[XmlElement("sky_condition")]
//public List<SkyCondition> skyConditions {get; set;}
[XmlElement("flight_category")]
public string flightCategory { get; set; }
[XmlElement("metar_type")]
public string metarType { get; set; }
[XmlElement("elevation_m")]
public double elevationMeters { get; set; }
}
try this, it was tested in Visual Studio
HttpResponseMessage response= client.GetAsync(baseUrl).Result;
string xml;
if (response.IsSuccessStatusCode)
{
xml = response.Content.ReadAsStringAsync().Result;
}
Response result;
XmlSerializer serializer = new XmlSerializer(typeof(Response));
using (StringReader reader = new StringReader(xml))
{
result = (Response)serializer.Deserialize(reader);
}
classes
[XmlRoot(ElementName = "response")]
public class Response
{
[XmlElement(ElementName = "request_index")]
public int RequestIndex { get; set; }
[XmlElement(ElementName = "data_source")]
public DataSource DataSource { get; set; }
[XmlElement(ElementName = "request")]
public Request Request { get; set; }
[XmlElement(ElementName = "errors")]
public object Errors { get; set; }
[XmlElement(ElementName = "warnings")]
public object Warnings { get; set; }
[XmlElement(ElementName = "time_taken_ms")]
public int TimeTakenMs { get; set; }
[XmlElement(ElementName = "data")]
public Data Data { get; set; }
[XmlAttribute(AttributeName = "xsd")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName = "xsi")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "version")]
public string Version { get; set; }
[XmlAttribute(AttributeName = "noNamespaceSchemaLocation")]
public string NoNamespaceSchemaLocation { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "quality_control_flags")]
public class QualityControlFlags
{
[XmlElement(ElementName = "auto_station")]
public string AutoStation { get; set; }
}
[XmlRoot(ElementName = "sky_condition")]
public class SkyCondition
{
[XmlAttribute(AttributeName = "sky_cover")]
public string SkyCover { get; set; }
[XmlAttribute(AttributeName = "cloud_base_ft_agl")]
public int CloudBaseFtAgl { get; set; }
}
[XmlRoot(ElementName = "METAR")]
public class METAR
{
[XmlElement(ElementName = "sky_condition")]
public List<SkyCondition> SkyCondition { get; set; }
[XmlElement(ElementName = "flight_category")]
public string FlightCategory { get; set; }
[XmlElement(ElementName = "three_hr_pressure_tendency_mb")]
public double ThreeHrPressureTendencyMb { get; set; }
[XmlElement(ElementName = "maxT_c")]
public decimal MaxTC { get; set; }
[XmlElement(ElementName = "minT_c")]
public decimal MinTC { get; set; }
[XmlElement(ElementName = "precip_in")]
public double PrecipIn { get; set; }
[XmlElement(ElementName = "pcp6hr_in")]
public double Pcp6hrIn { get; set; }
[XmlElement(ElementName = "metar_type")]
public string MetarType { get; set; }
[XmlElement(ElementName = "elevation_m")]
public double ElevationM { get; set; }
[XmlElement(ElementName = "raw_text")]
public string RawText { get; set; }
[XmlElement(ElementName = "station_id")]
public string StationId { get; set; }
[XmlElement(ElementName = "observation_time")]
public DateTime ObservationTime { get; set; }
[XmlElement(ElementName = "latitude")]
public double Latitude { get; set; }
[XmlElement(ElementName = "longitude")]
public double Longitude { get; set; }
[XmlElement(ElementName = "temp_c")]
public double TempC { get; set; }
[XmlElement(ElementName = "dewpoint_c")]
public double DewpointC { get; set; }
[XmlElement(ElementName = "wind_dir_degrees")]
public int WindDirDegrees { get; set; }
[XmlElement(ElementName = "wind_speed_kt")]
public int WindSpeedKt { get; set; }
[XmlElement(ElementName = "visibility_statute_mi")]
public double VisibilityStatuteMi { get; set; }
[XmlElement(ElementName = "altim_in_hg")]
public double AltimInHg { get; set; }
[XmlElement(ElementName = "sea_level_pressure_mb")]
public decimal SeaLevelPressureMb { get; set; }
[XmlElement(ElementName = "quality_control_flags")]
public QualityControlFlags QualityControlFlags { get; set; }
}
[XmlRoot(ElementName = "data")]
public class Data
{
[XmlElement(ElementName = "METAR")]
public List<METAR> METAR { get; set; }
[XmlAttribute(AttributeName = "num_results")]
public int NumResults { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "data_source")]
public class DataSource
{
[XmlAttribute(AttributeName = "name")]
public string Name { get; set; }
}
[XmlRoot(ElementName = "request")]
public class Request
{
[XmlAttribute(AttributeName = "type")]
public string Type { get; set; }
}

Getting error "System.InvalidOperationException. There is an error in XML document (1, 2)." during Deserialization in C#

Trying to Deserialise XML response to C#. but getting error as specified below.
My code:
IRestResponse result = Client.Execute<StudentPersonal>(new RestRequest(Method.POST)
.AddHeader("Accept", " application/xml")
.AddHeader("Authorization", "Basic Y2xaTWNqZFFlbFF4YVU1TlFtUnZOV3hXTkhadmNHaFhPSGxTT0Rka1VIcFhhRTl5TWtWQ05rRjNUMlp5VlUxWlNYa3hjbEU1V1RGUmMxTkNOMlZWVlRvNk9nPT06eUkxdERIanNVcGxLUUpzUlNXNTVacXpRVUFGQ3JrZzFwUGxUbDhUTmRQaFU0Z0xsTFJlQkVxTmhzZml3TnpaVA==")
.AddHeader("Content-Type", "application/xml")
.AddParameter("application/xml", body, ParameterType.RequestBody));
StudentPersonal firstResponse = new StudentPersonal();
StringReader srt = new StringReader(result.Content);
XmlSerializer serializer = new XmlSerializer(typeof(StudentPersonal), new XmlRootAttribute("StudentPersonal"));
firstResponse = (StudentPersonal)serializer.Deserialize(srt);
Console.WriteLine(firstResponse);
Assert.AreEqual("Created", result.StatusCode.ToString());
My classes:
[XmlRoot(ElementName = "StudentPersonal")]
public class StudentPersonal
{
//// XmlSerializer serializer = new XmlSerializer(typeof(StudentPersonal));
//// using (StringReader reader = new StringReader(xml))
//// {
//// var test = (StudentPersonal)serializer.Deserialize(reader);
//// }
[XmlElement(ElementName = "LocalId")]
public double LocalId { get; set; }
[XmlElement(ElementName = "StateProvinceId")]
public int StateProvinceId { get; set; }
[XmlElement(ElementName = "ElectronicIdList")]
public object ElectronicIdList { get; set; }
[XmlElement(ElementName = "OtherIdList")]
public OtherIdList OtherIdList { get; set; }
[XmlElement(ElementName = "PersonInfo")]
public PersonInfo PersonInfo { get; set; }
[XmlElement(ElementName = "GiftedTalented")]
public string GiftedTalented { get; set; }
[XmlElement(ElementName = "EconomicDisadvantage")]
public string EconomicDisadvantage { get; set; }
[XmlElement(ElementName = "ESL")]
public string ESL { get; set; }
[XmlElement(ElementName = "YoungCarersRole")]
public string YoungCarersRole { get; set; }
[XmlElement(ElementName = "Disability")]
public string Disability { get; set; }
[XmlElement(ElementName = "IntegrationAide")]
public string IntegrationAide { get; set; }
[XmlElement(ElementName = "EducationSupport")]
public string EducationSupport { get; set; }
[XmlElement(ElementName = "Sensitive")]
public string Sensitive { get; set; }
[XmlAttribute(AttributeName = "xsd")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName = "xsi")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "RefId")]
public string RefId { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "OtherId")]
public class OtherId
{
[XmlAttribute(AttributeName = "Type")]
public string Type { get; set; }
[XmlText]
public int Text { get; set; }
}
[XmlRoot(ElementName = "OtherIdList")]
public class OtherIdList
{
[XmlElement(ElementName = "OtherId")]
public List<OtherId> OtherId { get; set; }
}
[XmlRoot(ElementName = "Name")]
public class Name
{
[XmlElement(ElementName = "Title")]
public string Title { get; set; }
[XmlElement(ElementName = "FamilyName")]
public string FamilyName { get; set; }
[XmlElement(ElementName = "GivenName")]
public string GivenName { get; set; }
[XmlElement(ElementName = "MiddleName")]
public string MiddleName { get; set; }
[XmlElement(ElementName = "FamilyNameFirst")]
public string FamilyNameFirst { get; set; }
[XmlElement(ElementName = "PreferredFamilyName")]
public string PreferredFamilyName { get; set; }
[XmlElement(ElementName = "PreferredFamilyNameFirst")]
public string PreferredFamilyNameFirst { get; set; }
[XmlElement(ElementName = "PreferredGivenName")]
public string PreferredGivenName { get; set; }
[XmlElement(ElementName = "FullName")]
public string FullName { get; set; }
[XmlAttribute(AttributeName = "Type")]
public string Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "EnglishProficiency")]
public class EnglishProficiency
{
[XmlElement(ElementName = "Code")]
public int Code { get; set; }
}
[XmlRoot(ElementName = "Language")]
public class Language
{
[XmlElement(ElementName = "Code")]
public int Code { get; set; }
[XmlElement(ElementName = "LanguageType")]
public int LanguageType { get; set; }
}
[XmlRoot(ElementName = "LanguageList")]
public class LanguageList
{
[XmlElement(ElementName = "Language")]
public List<Language> Language { get; set; }
}
[XmlRoot(ElementName = "DwellingArrangement")]
public class DwellingArrangement
{
[XmlElement(ElementName = "Code")]
public int Code { get; set; }
}
[XmlRoot(ElementName = "Religion")]
public class Religion
{
[XmlElement(ElementName = "Code")]
public int Code { get; set; }
}
[XmlRoot(ElementName = "ReligiousEvent")]
public class ReligiousEvent
{
[XmlElement(ElementName = "Type")]
public string Type { get; set; }
[XmlElement(ElementName = "Date")]
public DateTime Date { get; set; }
}
[XmlRoot(ElementName = "ReligiousEventList")]
public class ReligiousEventList
{
[XmlElement(ElementName = "ReligiousEvent")]
public ReligiousEvent ReligiousEvent { get; set; }
}
[XmlRoot(ElementName = "Demographics")]
public class Demographics
{
[XmlElement(ElementName = "IndigenousStatus")]
public int IndigenousStatus { get; set; }
[XmlElement(ElementName = "Sex")]
public int Sex { get; set; }
[XmlElement(ElementName = "BirthDate")]
public DateTime BirthDate { get; set; }
[XmlElement(ElementName = "BirthDateVerification")]
public int BirthDateVerification { get; set; }
[XmlElement(ElementName = "CountryArrivalDate")]
public DateTime CountryArrivalDate { get; set; }
[XmlElement(ElementName = "EnglishProficiency")]
public EnglishProficiency EnglishProficiency { get; set; }
[XmlElement(ElementName = "LanguageList")]
public LanguageList LanguageList { get; set; }
[XmlElement(ElementName = "DwellingArrangement")]
public DwellingArrangement DwellingArrangement { get; set; }
[XmlElement(ElementName = "Religion")]
public Religion Religion { get; set; }
[XmlElement(ElementName = "ReligiousEventList")]
public ReligiousEventList ReligiousEventList { get; set; }
[XmlElement(ElementName = "PermanentResident")]
public string PermanentResident { get; set; }
[XmlElement(ElementName = "VisaSubClass")]
public int VisaSubClass { get; set; }
[XmlElement(ElementName = "VisaStatisticalCode")]
public string VisaStatisticalCode { get; set; }
[XmlElement(ElementName = "VisaExpiryDate")]
public DateTime VisaExpiryDate { get; set; }
[XmlElement(ElementName = "LBOTE")]
public string LBOTE { get; set; }
[XmlElement(ElementName = "ImmunisationCertificateStatus")]
public string ImmunisationCertificateStatus { get; set; }
[XmlElement(ElementName = "CulturalBackground")]
public int CulturalBackground { get; set; }
[XmlElement(ElementName = "MaritalStatus")]
public int MaritalStatus { get; set; }
[XmlElement(ElementName = "MedicareNumber")]
public int MedicareNumber { get; set; }
}
[XmlRoot(ElementName = "PhoneNumber")]
public class PhoneNumber
{
[XmlElement(ElementName = "Number")]
public int Number { get; set; }
[XmlElement(ElementName = "ListedStatus")]
public string ListedStatus { get; set; }
[XmlAttribute(AttributeName = "Type")]
public int Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "PhoneNumberList")]
public class PhoneNumberList
{
[XmlElement(ElementName = "PhoneNumber")]
public PhoneNumber PhoneNumber { get; set; }
}
[XmlRoot(ElementName = "Email")]
public class Email
{
[XmlAttribute(AttributeName = "Type")]
public int Type { get; set; }
[XmlText]
public string Text { get; set; }
}
[XmlRoot(ElementName = "EmailList")]
public class EmailList
{
[XmlElement(ElementName = "Email")]
public List<Email> Email { get; set; }
}
[XmlRoot(ElementName = "PersonInfo")]
public class PersonInfo
{
[XmlElement(ElementName = "Name")]
public Name Name { get; set; }
[XmlElement(ElementName = "Demographics")]
public Demographics Demographics { get; set; }
[XmlElement(ElementName = "AddressList")]
public object AddressList { get; set; }
[XmlElement(ElementName = "PhoneNumberList")]
public PhoneNumberList PhoneNumberList { get; set; }
[XmlElement(ElementName = "EmailList")]
public EmailList EmailList { get; set; }
}
Below is the response XML details from Result.content:
<StudentPersonal xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" RefId=\"9ec476fd-09b5-4b2b-967a-fb80b207526e\" xmlns=\"http://www.sifassociation.org/datamodel/au/3.4\">
<LocalId>2432717276450097998</LocalId>
<StateProvinceId>674361</StateProvinceId>
<ElectronicIdList />
<OtherIdList>
<OtherId Type=\"MazeKey\">674363</OtherId>
<OtherId Type=\"VETUSI\">674362</OtherId>
<OtherId Type=\"UPN\">674363</OtherId>
</OtherIdList>
<PersonInfo>
<Name Type=\"LGL\">
<Title>Ms</Title>
<FamilyName>Gutteridge2</FamilyName>
<GivenName>Ferdinanda2</GivenName>
<MiddleName>Creamer1</MiddleName>
<FamilyNameFirst>N</FamilyNameFirst>
<PreferredFamilyName>Gutteridge</PreferredFamilyName>
<PreferredFamilyNameFirst>N</PreferredFamilyNameFirst>
<PreferredGivenName>Ferdinanda</PreferredGivenName>
<FullName> Ferdinanda Gutteridge</FullName>
</Name>
<Demographics>
<IndigenousStatus>1</IndigenousStatus>
<Sex>2</Sex>
<BirthDate>2006-03-21</BirthDate>
<BirthDateVerification>1004</BirthDateVerification>
<CountryArrivalDate>2008-09-17</CountryArrivalDate>
<EnglishProficiency>
<Code>1</Code>
</EnglishProficiency>
<LanguageList>
<Language>
<Code>1201</Code>
<LanguageType>3</LanguageType>
</Language>
<Language>
<Code>4303</Code>
<LanguageType>1</LanguageType>
</Language>
<Language>
<Code>4303</Code>
<LanguageType>1</LanguageType>
</Language>
</LanguageList>
<DwellingArrangement>
<Code>1671</Code>
</DwellingArrangement>
<Religion>
<Code>0002</Code>
</Religion>
<ReligiousEventList>
<ReligiousEvent>
<Type>Confirmation</Type>
<Date>2018-07-17</Date>
</ReligiousEvent>
</ReligiousEventList>
<PermanentResident>P</PermanentResident>
<VisaSubClass>124</VisaSubClass>
<VisaStatisticalCode>VisaStatisticalCode</VisaStatisticalCode>
<VisaExpiryDate>2019-01-17</VisaExpiryDate>
<LBOTE>N</LBOTE>
<ImmunisationCertificateStatus>C</ImmunisationCertificateStatus>
<CulturalBackground>0901</CulturalBackground>
<MaritalStatus>3</MaritalStatus>
<MedicareNumber>67436</MedicareNumber>
</Demographics>
<AddressList />
<PhoneNumberList>
<PhoneNumber Type=\"0350\">
<Number>12367436</Number>
<ListedStatus>U</ListedStatus>
</PhoneNumber>
</PhoneNumberList>
<EmailList>
<Email Type=\"01\">person67436#email1.com.au</Email>
<Email Type=\"02\">person67436#email2.com.au</Email>
<Email Type=\"03\">person67436#email3.com.au</Email>
</EmailList>
</PersonInfo>
<GiftedTalented>N</GiftedTalented>
<EconomicDisadvantage>N</EconomicDisadvantage>
<ESL>N</ESL>
<YoungCarersRole>N</YoungCarersRole>
<Disability>N</Disability>
<IntegrationAide>N</IntegrationAide>
<EducationSupport>U</EducationSupport>
<Sensitive>N</Sensitive>
</StudentPersonal>
Error is from the above XML response. Getting Error
There is an error in XML document (1, 2). InvalidOperationException: <StudentPersonal xmlns='http://www.sifassociation.org/datamodel/au/3.4'> was not expected.
There seems to be two issues.
Add the namespace to your class:
[XmlRoot(ElementName = "StudentPersonal", Namespace = "http://www.sifassociation.org/datamodel/au/3.4")]
public class StudentPersonal
...
In the XML, all " are escaped with \.
<StudentPersonal xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" RefId=\"9ec476fd-09b5-4b2b-967a-fb80b207526e\" xmlns=\"http://www.sifassociation.org/datamodel/au/3.4\">
...
</StudentPersonal>
This also occurs in other places throughout the XML. Such as:
<PhoneNumber Type=\"0350\">
To resolve the issue, replace all occurrences of \" with ".
<StudentPersonal xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" RefId="9ec476fd-09b5-4b2b-967a-fb80b207526e" xmlns="http://www.sifassociation.org/datamodel/au/3.4">
...
</StudentPersonal>
and
<PhoneNumber Type="0350">
Here's a method that can be used to deserialize the XML:
DeserializeXMLStringToObject:
public static T DeserializeXMLStringToObject<T>(string xmlData)
{
T rObject = default(T);
try
{
if (string.IsNullOrEmpty(xmlData))
{
return default(T);
}
//replace \" with "
string xmlDataSanitized = xmlData.Replace("\\\"", "\"");
using (System.IO.StringReader reader = new System.IO.StringReader(xmlDataSanitized))
{
//add the namespace to the class instead of adding it here
//System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T), "http://www.sifassociation.org/datamodel/au/3.4");
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
rObject = (T)serializer.Deserialize(reader);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
throw ex;
}
return rObject;
}
Usage:
private StudentPersonal _studentPersonal = null;
...
_studentPersonal = DeserializeXMLStringToObject<StudentPersonal>(result.Content);
//for testing, display some of the data
//System.Diagnostics.Debug.WriteLine("LocalId: " + _studentPersonal.LocalId);
//System.Diagnostics.Debug.WriteLine("GivenName: " + _studentPersonal.PersonInfo.Name.GivenName);

Reading Xml File using C# and returnig as list of object

This is my xml File i want to read the all the inner test of all the child element of the exp
<TreasuryExp xmlns="http://page.com/Exp" MsgDtTm="2020-06-08T12:30:35.283"
MessageId="11EXP080620201" Source="11" Destination="CPSMS" Name="user1" RecordsCount="1861"
NetAmountSum="4440306976.00">
<Exp>
<Year1>2018</Year1>
<Year2>2019</Year2>
<Code>11</Code>
<Budget>205500101638352</Budget>
<ObjectHeadCode>52_400</ObjectHeadCode>
<DDOCode>300008</DDOCode>
<VoucherNumber>5994</VoucherNumber>
<Date>31/03/2019</Date>
<GrossAmount>51200.00</GrossAmount>
<DeductionAmount>0.00</DeductionAmount>
<NetAmount>51200.00</NetAmount>
<UniqueID>3392430</UniqueID>
<PayeeCount>0</PayeeCount>
<PayDetail>N</PayDetail>
<PayeeDetails />
</Exp>
<Exp>
<Year1>2018</Year1>
<Year2>2019</Year2>
<Code>11</Code>
<Budget>205500101638352</Budget>
<ObjectHeadCode>52_400</ObjectHeadCode>
<DDOCode>300008</DDOCode>
<VoucherNumber>5995</VoucherNumber>
<Date>31/03/2019</Date>
<GrossAmount>4400.00</GrossAmount>
<DeductionAmount>0.00</DeductionAmount>
<NetAmount>4400.00</NetAmount>
<UniqueID>3303530</UniqueID>
<PayeeCount>0</PayeeCount>
<PayDetail>N</PayDetail>
<PayeeDetails />
</Exp>
</TreasuryExp>
i and reading it and running it on loop but m getting returned same items every time
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("E:/trial/Expenditure.xml");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
XmlAttributeCollection attrColl = xmldoc.DocumentElement.Attributes;
var xmlnsattribute = attrColl.GetNamedItem("xmlns").Value;
XmlNodeList xmlnodli = xmldoc.SelectNodes("//ab:Expenditure", nsmgr);
List<Expenditure> strli = new List<Expenditure>();
here i am running a loop to load in list to get the value of the xml nodes or u can say values of each nodes
foreach (XmlNode xm in xmlnodli)
{
fin_year1 = Convert.ToInt32(xm.SelectSingleNode("//ab:Year1", nsmgr).InnerText),
fin_year2 = Convert.ToInt32(xm.SelectSingleNode("//ab:Year2", nsmgr).InnerText),
statecode = Convert.ToInt32(xm.SelectSingleNode("//ab:Code", nsmgr).InnerText),
budgetcode = Convert.ToInt64(xm.SelectSingleNode("//ab:Budget", nsmgr).InnerText),
objectheadcode = (xm.SelectSingleNode("//ab:ObjectHeadCode", nsmgr).InnerText).ToString(),
ddocode = xm.SelectSingleNode("//ab:DDOCode", nsmgr).InnerText.ToString(),
vouchernumber = Convert.ToInt32(xm.SelectSingleNode("//ab:VoucherNumber", nsmgr).InnerText),
date = Convert.ToDateTime(xm.SelectSingleNode("//ab:Date", nsmgr).InnerText),
grossamount = Convert.ToDecimal(xm.SelectSingleNode("//ab:GrossAmount", nsmgr).InnerText),
deductionamount = Convert.ToDecimal(xm.SelectSingleNode("//ab:DeductionAmount", nsmgr).InnerText),
netamount = Convert.ToDecimal(xmlnodli[i].SelectSingleNode("NetAmount", nsmgr).InnerText.ToString()),
objectheadcode = Convert.ToString(xm.SelectNodes("//ab:NetAmount", nsmgr)),
stateexpuniqueid = Convert.ToInt32(xm.SelectSingleNode("//ab:StateExpUniqueID", nsmgr).InnerText),
payeecount = Convert.ToInt32(xm.SelectSingleNode("//ab:PayeeCount", nsmgr).InnerText),
paydetail = Convert.ToString(xm.SelectSingleNode("//ab:PayDetail", nsmgr).InnerText),
}
return strli;
here is my DTO
public class Expenditure
{
public int year1 { get; set; }
public int year2 { get; set; }
public int code { get; set; }
public long budget { get; set; }
public string objectheadcode { get; set; }
public string ddocode { get; set; }
public int vouchernumber { get; set; }
public DateTime date { get; set; }
public decimal grossamount { get; set; }
public decimal deductionamount { get; set; }
public decimal netamount { get; set; }
public int stateexpuniqueid { get; set; }
public int payeecount { get; set; }
public string paydetail { get; set; }
public string payeedetails { get; set; }
public string xmlns { get; set; }
public string MsgDtTm { get; set; }
public string MessageId { get; set; }
public string Source { get; set; }
public string Destination { get; set; }
public string StateName { get; set; }
public string RecordsCount { get; set; }
public string NetAmountSum { get; set; }
}
Thank You
Use following :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializier = new XmlSerializer(typeof(TreasuryExp));
TreasuryExp treasuryExp = (TreasuryExp)serializier.Deserialize(reader);
}
}
[XmlRoot(ElementName = "TreasuryExp", Namespace = "http://page.com/Exp")]
public class TreasuryExp
{
[XmlElement("Exp")]
public List<Expenditure> Expenditure { get; set; }
}
public class Expenditure
{
[XmlElement("Year1")]
public int year1 { get; set; }
[XmlElement("Year2")]
public int year2 { get; set; }
[XmlElement("Code")]
public int code { get; set; }
[XmlElement("Budget")]
public long budget { get; set; }
[XmlElement("ObjectHeadCode")]
public string objectheadcode { get; set; }
[XmlElement("DDOCode")]
public string ddocode { get; set; }
[XmlElement("VpucherNumber")]
public int vouchernumber { get; set; }
private DateTime _date { get; set; }
[XmlElement("Date")]
public string date
{
get{ return _date.ToString("dd/MM/yyyy");}
set{ _date = DateTime.ParseExact(value, "dd/MM/yyyy", CultureInfo.InvariantCulture);}
}
[XmlElement("GrossAmount")]
public decimal grossamount { get; set; }
[XmlElement("DectionAmount")]
public decimal deductionamount { get; set; }
[XmlElement("NetAmount")]
public decimal netamount { get; set; }
[XmlElement("UniqueID")]
public int stateexpuniqueid { get; set; }
[XmlElement("PayeeCount")]
public int payeecount { get; set; }
[XmlElement("PayeeDetail")]
public string paydetail { get; set; }
[XmlElement("PayeeDetails")]
public string payeedetails { get; set; }
[XmlElement("")]
public string xmlns { get; set; }
[XmlElement("")]
public string MsgDtTm { get; set; }
[XmlElement("")]
public string MessageId { get; set; }
[XmlElement("")]
public string Source { get; set; }
[XmlElement("")]
public string Destination { get; set; }
[XmlElement("")]
public string StateName { get; set; }
[XmlElement("")]
public string RecordsCount { get; set; }
[XmlElement("")]
public string NetAmountSum { get; set; }
}
}

Can't deserialize xml properly

I have the following string to deserialize:
<result>
<error>
<errorcode>0</errorcode>
<errorge>წარმატებით</errorge>
<errorru>Удачно</errorru>
<erroren>successfully</erroren>
<line>89</line>
</error>
<amount>
<gel>1</gel>
</amount>
<user>01001</user>
<service>MyService</service> // here
<data>
<nickname>popcorn2</nickname>
<identification_name>identified</identification_name>
<wallet_code>5554654</wallet_code>
<NationalRate>1</NationalRate>
<RATE>1</RATE>
<GENERATED_AMOUNT>1</GENERATED_AMOUNT>
<CURRENCY>GEL</CURRENCY>
</data>
<accoutant>
<agentBenefit>0</agentBenefit>
<agentCommission>0.44</agentCommission>
<clientCommission>0</clientCommission>
</accoutant>
<service>
<min_amount>0.49</min_amount>
<max_amount>1500.00</max_amount>
<currency>GEL</currency>
</service>
<avance>-134206.1500</avance>
<operation_status>0</operation_status>
</result>
as you noticed it has two tags named "service" and they have different contents
here is the class that I've been using
[XmlRoot(ElementName = "error")]
public class ErrorInfo
{
[XmlElement(ElementName = "errorcode")]
public string Errorcode { get; set; }
[XmlElement(ElementName = "errorge")]
public string Errorge { get; set; }
[XmlElement(ElementName = "errorru")]
public string Errorru { get; set; }
[XmlElement(ElementName = "erroren")]
public string Erroren { get; set; }
[XmlElement(ElementName = "line")]
public string Line { get; set; }
}
[XmlRoot(ElementName = "amount")]
public class Amount
{
[XmlElement(ElementName = "gel")]
public string Gel { get; set; }
}
[XmlRoot(ElementName = "data")]
public class Data
{
[XmlElement(ElementName = "nickname")]
public string Nickname { get; set; }
[XmlElement(ElementName = "identification_name")]
public string Identification_name { get; set; }
[XmlElement(ElementName = "wallet_code")]
public string Wallet_code { get; set; }
[XmlElement(ElementName = "NationalRate")]
public string NationalRate { get; set; }
[XmlElement(ElementName = "RATE")]
public string RATE { get; set; }
[XmlElement(ElementName = "GENERATED_AMOUNT")]
public string GENERATED_AMOUNT { get; set; }
[XmlElement(ElementName = "CURRENCY")]
public string CURRENCY { get; set; }
}
[XmlRoot(ElementName = "accoutant")]
public class Accoutant
{
[XmlElement(ElementName = "agentBenefit")]
public string AgentBenefit { get; set; }
[XmlElement(ElementName = "agentCommission")]
public string AgentCommission { get; set; }
[XmlElement(ElementName = "clientCommission")]
public string ClientCommission { get; set; }
}
[XmlRoot(ElementName = "service")]
public class Service
{
[XmlElement(ElementName = "min_amount")]
public string Min_amount { get; set; }
[XmlElement(ElementName = "max_amount")]
public string Max_amount { get; set; }
[XmlElement(ElementName = "currency")]
public string Currency { get; set; }
}
[XmlRoot(ElementName = "result")]
public class Result
{
[XmlElement(ElementName = "error")]
public ErrorInfo Error { get; set; }
[XmlElement(ElementName = "amount")]
public Amount Amount { get; set; }
[XmlElement(ElementName = "user")]
public string User { get; set; }
[XmlElement(ElementName = "data")]
public Data Data { get; set; }
[XmlElement(ElementName = "accoutant")]
public Accoutant Accoutant { get; set; }
[XmlElement(ElementName = "service")]
public Service[] Service { get; set; }
[XmlElement(ElementName = "avance")]
public string Avance { get; set; }
[XmlElement(ElementName = "operation_status")]
public string Operation_status { get; set; }
}
I tried to handle service tags as array but it could not work. I was able to get values from the last tag , but not from the first one. both tags are neccessary so i am wondering if there is a way to get values from both tags ?
Visual Studio has this really great feature where you can put your XML in the clipboard and then do Edit -> Paste special -> Paste XML as classes.
If you do that with your XML, it generates the following class for the service tag:
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class resultService
{
private decimal min_amountField;
private decimal max_amountField;
private string currencyField;
private string[] textField;
/// <remarks/>
public decimal min_amount
{
get
{
return this.min_amountField;
}
set
{
this.min_amountField = value;
}
}
/// <remarks/>
public decimal max_amount
{
get
{
return this.max_amountField;
}
set
{
this.max_amountField = value;
}
}
/// <remarks/>
public string currency
{
get
{
return this.currencyField;
}
set
{
this.currencyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string[] Text
{
get
{
return this.textField;
}
set
{
this.textField = value;
}
}
}
If you want to do it manually, the secret seems to be to add a field with the XmlTextAttribute attribute.
You can not have two different elements with the same name. It won't know which tag belongs where, you have to alter the name to "MyServiceName" or something similar.
Change your classes as follows:
[XmlRoot(ElementName = "result")]
public class Result
{
// no need XmlElement attribute
public ServiceComplex ServiceComplex { get; set; }
// no need XmlElement attribute
public string ServiceSimple { get; set; }
// other properties
}
// no need XmlRoot attribute
public class ServiceComplex
{
[XmlElement(ElementName = "min_amount")]
public string Min_amount { get; set; }
[XmlElement(ElementName = "max_amount")]
public string Max_amount { get; set; }
[XmlElement(ElementName = "currency")]
public string Currency { get; set; }
}
Instead of ServiceComplex and ServiceSimple, choose the names that suit you.
Subscribe XmlSerializer to UnknownElement event:
var xs = new XmlSerializer(typeof(Result));
xs.UnknownElement += Xs_UnknownElement;
private static void Xs_UnknownElement(object sender, XmlElementEventArgs e)
{
if (e.Element.Name == "service")
{
var result = (Result)e.ObjectBeingDeserialized;
if (e.Element.ChildNodes.Count == 1)
{
result.ServiceSimple = e.Element.InnerText;
}
else
{
result.ServiceComplex = new ServiceComplex
{
Min_amount = e.Element.SelectSingleNode("min_amount").InnerText,
Max_amount = e.Element.SelectSingleNode("max_amount").InnerText,
Currency = e.Element.SelectSingleNode("currency").InnerText
};
}
}
}
In the event handler, we manually populate the properties of our class from xml.

InvalidOperationException Error Reflecting Class

After read multiples posts related on this error and not found an solution to my problem, I explain it here.
I use XmlSerializer to serialize simple classes.
Here's my code:
private void btnGenerateXml_Click(object sender, RoutedEventArgs e)
{
Orchard orchard = new Orchard
{
Recipe = new Recipe
{
Name = "Generated by JooWeb.Tools",
Author = "admin",
ExportUtc = DateTime.UtcNow
},
MyDatas = new MyDatas
{
//Test = "test"
TrendDatas = new TrendDatas
{
Id = null,
Status = "Published",
TrendDatasPart = new TrendDatasPart
{
IdSource = 0,
PostalCode = "1000",
Locality = "Test5",
Surface = (decimal)0.00,
Price = (decimal)0.00,
Type = "",
InsertDateIndicator = "",
UpdateDateIndicator = "",
GetFromDate = DateTime.Now,
UpdatedDate = new DateTime(1900, 1, 1)
},
CommonPart = new CommonPart
{
Owner = "/User.UserName=admin",
CreatedUtc = DateTime.UtcNow,
PublishedUtc = DateTime.UtcNow,
ModifiedUtc = DateTime.UtcNow
}
}
}
};
XmlSerializer orchardXmlSerializer = new XmlSerializer(typeof(Orchard));
var path = #"C:\Temp\orchardFileImport_" + string.Format("{0:yyyyMMdd}", DateTime.Today) + ".xml";
if (File.Exists(path))
File.Delete(path);
orchardXmlSerializer.Serialize(File.OpenWrite(path), orchard);
MessageBox.Show("Finished");
}
}
[XmlRoot]
public class Orchard
{
[XmlElement]
public Recipe Recipe { get; set; }
[XmlElement(ElementName = "Data")]
public MyDatas MyDatas { get; set; }
}
public class Recipe
{
[XmlElement]
public string Name { get; set; }
[XmlElement]
public string Author { get; set; }
[XmlElement]
public DateTime ExportUtc { get; set; }
}
public class MyDatas
{
public MyDatas()
{
}
//[XmlElement]
//public string Test { get; set; }
[XmlElement]
public TrendDatas TrendDatas { get; set; }
}
public class TrendDatas
{
[XmlAttribute]
public string Status { get; set; }
[XmlAttribute]
public int? Id { get; set; }
//[XmlIgnore]
[XmlElement]
public TrendDatasPart TrendDatasPart { get; set; }
//[XmlIgnore]
[XmlElement]
public CommonPart CommonPart { get; set; }
}
public class TrendDatasPart
{
[XmlAttribute]
public int IdSource { get; set; }
[XmlAttribute]
public string PostalCode { get; set; }
[XmlAttribute]
public string Locality { get; set; }
[XmlAttribute]
public decimal Surface { get; set; }
[XmlAttribute]
public decimal Price { get; set; }
[XmlAttribute]
public string Type { get; set; }
[XmlAttribute]
public string InsertDateIndicator { get; set; }
[XmlAttribute]
public string UpdateDateIndicator { get; set; }
[XmlAttribute]
public DateTime GetFromDate { get; set; }
[XmlAttribute]
public DateTime UpdatedDate { get; set; }
}
public class CommonPart
{
[XmlAttribute]
public string Owner { get; set; }
[XmlAttribute]
public DateTime CreatedUtc { get; set; }
[XmlAttribute]
public DateTime PublishedUtc { get; set; }
[XmlAttribute]
public DateTime ModifiedUtc { get; set; }
}
With this code when I click on Generate xml file, I got the error InvalidOperationException
There was an error reflecting type 'MergeExcelFiles.Orchard'.
{"There was an error reflecting property 'MyDatas'."}
Like you see in my comments, I try to just add a string xmlElement to node MyDatas, with this change I got no error but in the xml file I don't have any node with name Data.
I don't understand why with class Recipe all look right but with node MyDatas nothing showed in xml file or got this error "InvalidOperationException".
You need to dig into your error message more because the reason is in the innermost exception:
System.InvalidOperationException: Cannot serialize member 'Id' of type System.Nullable`1[System.Int32]. XmlAttribute/XmlText cannot be used to encode complex types.
The issue is that you have a nullable value type as a property (TrendDatas.Id) to be serialized as an attribute and XmlSerializer does not handle these well. There are a number of workarounds listed here and here. None of them is particularly elegant. The best option might be changing the definition of Id to an element:
public class TrendDatas
{
// ... snip ...
[XmlElement(IsNullable = true)]
public int? Id { get; set; }
public bool ShouldSerializeId() { return Id.HasValue; }
// ... snip ...
}
The ShouldSerializeId is a method that, by convention, the serializer uses to decide if the property should be serialized in the output. In the case of a null value, no element will be defined in the serialized output.

Categories