How to Make Xmlserializer Ignore the Namespace on Deserialization

Can I make XmlSerializer ignore the namespace on deserialization?

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.

Define an XmlTextReader that ignores namespaces. Like so:

// helper class to ignore namespaces when de-serializing
public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader): base(reader) { }

public override string NamespaceURI
{
get { return ""; }
}
}

// helper class to omit XML decl at start of document when serializing
public class XTWFND : XmlTextWriter {
public XTWFND (System.IO.TextWriter w) : base(w) { Formatting= System.Xml.Formatting.Indented;}
public override void WriteStartDocument () { }
}

Here's an example of how you would de-serialize using that TextReader:

public class MyType1 
{
public string Label
{
set { _Label= value; }
get { return _Label; }
}

private int _Epoch;
public int Epoch
{
set { _Epoch= value; }
get { return _Epoch; }
}
}



String RawXml_WithNamespaces = @"
<MyType1 xmlns='urn:booboo-dee-doo'>
<Label>This document has namespaces on its elements</Label>
<Epoch xmlns='urn:How to Make Xmlserializer Ignore the Namespace on DeserializationHow to Make Xmlserializer Ignore the Namespace on DeserializationHow to Make Xmlserializer Ignore the Namespace on Deserializationaaaaaa'>0</Epoch>
</MyType1>";


System.IO.StringReader sr;
sr= new System.IO.StringReader(RawXml_WithNamespaces);
var s1 = new XmlSerializer(typeof(MyType1));
var o1= (MyType1) s1.Deserialize(new NamespaceIgnorantXmlTextReader(sr));
System.Console.WriteLine("\n\nDe-serialized, then serialized again:\n");
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("urn", "booboo-dee-doo");
s1.Serialize(new XTWFND(System.Console.Out), o1, ns);
Console.WriteLine("\n\n");

The result is like so:

    <MyType1>
<Label>This document has namespaces on its elements</Label>
<Epoch>0</Epoch>
</MyType1>

C# deserialize xml while ignoring namespace

You have a few issues here:

  1. The default namespace attribute is added to the string returned by ReadOuterXml() because ReadOuterXml() is designed not to change the semantics of the returned XML. Apparently in your XML there is a default namespace applied to some parent node of <IWantThis> -- which, being a default namespace, recursively applies to <IWantThis> itself. To retain this namespace membership, ReadOuterXml() must emit a default namespace as it writes out the nested XML.

    If you really want to completely ignore namespaces on XML, you need to create a custom XmlReader, e.g. as shown in

    • this answer to Can I make XmlSerializer ignore the namespace on deserialization? by Cheeso.
    • this answer to How do I create a XmlTextReader that ignores Namespaces and does not check characters by Alterant.
  2. You need to construct an XmlSerializer for SomeClass whose expected root node is <IWantThis>. You can do this using the XmlSerializer(Type, XmlRootAttribute) constructor, however, if you do, you must statically cache and reuse the serializer to avoid a severe memory leak, as explained in Memory Leak using StreamReader and XmlSerializer.

  3. You are creating a local copy wantedNodeContents of the element you want to deserialize, then re-parsing that local copy. There is no need to do this, you can use XmlReader.ReadSubtree() to deserialize just a portion of the XML.

Putting all these issues together, your GetObjectFromXml() could look like:

public static partial class XmlExtensions
{
public static T GetObjectFromXml<T>(string path, string localName, string namespaceURI, bool ignoreNamespaces = false)
{
using (var textReader = new StreamReader(path))
return GetObjectFromXml<T>(textReader, localName, namespaceURI);
}

public static T GetObjectFromXml<T>(TextReader textReader, string localName, string namespaceURI, bool ignoreNamespaces = false)
{
using (var xmlReader = ignoreNamespaces ? new NamespaceIgnorantXmlTextReader(textReader) : XmlReader.Create(textReader))
return GetObjectFromXml<T>(xmlReader, localName, namespaceURI);
}

public static T GetObjectFromXml<T>(XmlReader reader, string localName, string namespaceURI)
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "IWantThis" && reader.NamespaceURI == namespaceURI)
{
var serializer = XmlSerializerFactory.Create(typeof(T), localName, namespaceURI);
using (var subReader = reader.ReadSubtree())
return (T)serializer.Deserialize(subReader);
}
}
// Or throw an exception?
return default(T);
}
}

// This class copied from this answer https://stackoverflow.com/a/873281/3744182
// To https://stackoverflow.com/questions/870293/can-i-make-xmlserializer-ignore-the-namespace-on-deserialization
// By https://stackoverflow.com/users/48082/cheeso
// helper class to ignore namespaces when de-serializing
public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader): base(reader) { }

public override string NamespaceURI { get { return ""; } }
}

public static class XmlSerializerFactory
{
// To avoid a memory leak the serializer must be cached.
// https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer
// This factory taken from
// https://stackoverflow.com/questions/34128757/wrap-properties-with-cdata-section-xml-serialization-c-sharp/34138648#34138648

readonly static Dictionary<Tuple<Type, string, string>, XmlSerializer> cache;
readonly static object padlock;

static XmlSerializerFactory()
{
padlock = new object();
cache = new Dictionary<Tuple<Type, string, string>, XmlSerializer>();
}

public static XmlSerializer Create(Type serializedType, string rootName, string rootNamespace)
{
if (serializedType == null)
throw new ArgumentNullException();
if (rootName == null && rootNamespace == null)
return new XmlSerializer(serializedType);
lock (padlock)
{
XmlSerializer serializer;
var key = Tuple.Create(serializedType, rootName, rootNamespace);
if (!cache.TryGetValue(key, out serializer))
{
cache[key] = serializer = new XmlSerializer(serializedType, new XmlRootAttribute { ElementName = rootName, Namespace = rootNamespace });
}
return serializer;
}
}
}

Demo fiddle here.

Deserialize XML to object with xmlns namespace problem

You can use an XmlTextReader to ignore the namespaces before you deserialize your result.
Also, your ArrayOfThemes class should probably have an array of themes unless you only ever expect one. Below example works for deserializing that xml.

class Program
{
static void Main(string[] args)
{
var xml = @"<ArrayOfThemes xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns=""https://brickset.com/api/"">
<themes>
<theme>4 Juniors</theme>
<setCount>24</setCount>
<subthemeCount>5</subthemeCount>
<yearFrom>2003</yearFrom>
<yearTo>2004</yearTo>
</themes>
</ArrayOfThemes>";
var ms = new MemoryStream(Encoding.UTF8.GetBytes(xml));
var reader = new XmlTextReader(ms) {Namespaces = false};
var serializer = new XmlSerializer(typeof(ArrayOfThemes));

var result = (ArrayOfThemes) serializer.Deserialize(reader);
}
}

public class Themes
{
[XmlElement("theme")]
public string Theme { get; set; }
[XmlElement("setCount")]
public string SetCount { get; set; }
[XmlElement("subthemeCount")]

public string SubthemeCount { get; set; }
[XmlElement("yearFrom")]

public string YearFrom { get; set; }
[XmlElement("yearTo")]

public string YearTo { get; set; }
}

[Serializable, XmlRoot("ArrayOfThemes")]
public class ArrayOfThemes
{
[XmlElement("themes")]
public Themes[] Themes { get; set; }
}

how to ignore xml namespaces?

A namespace is fundamental in XML (unlike namespaces which are interchangeable). If Person is in that namespace, you must tell it:

[XmlRoot(Namespace="http://tempuri.org/PaymentInformationXml.xsd")]
public class Person {...}

C# XML Deserialization where root node sometimes has namespace attribute

You can use a NamespaceIgnorantXmlTextReader

public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader) : base(reader) { }

public override string NamespaceURI
{
get { return ""; }
}
}

var ser = new XmlSerializer(typeof(Project));

using var sr = new StringReader(xml);
using var textReader = new NamespaceIgnorantXmlTextReader(sr);

var proj = ser.Deserialize(textReader);

You can also set textReader.Namespaces = false; before calling Deserialize

Deserialize XML without namespaces but in a class expecting namespaces

I had run into a similar challenge with a proxy class. For reasons that I won't go into, I needed to serialize the class manually using the XmlSerializer on web server and deserialize on client. I was not able to find an elegant solution online, so I just avoided the issue by removing the XmlTypeAttribute from the proxy class manually after I auto-generated it in Visual Studio.

I kept coming back to see if there was a way to get the namespace to workout. Here is how I got it working without the need to modify the auto-generated classes. I ended up using an XmlTextReader to return the desired namespace on nodes matching a property name. There is room for improvement, but i hope it helps someone.

class Program
{
static void Main(string[] args)
{
//create list to serialize
Person personA = new Person() { Name = "Bob", Age = 10, StartDate = DateTime.Parse("1/1/1960"), Money = 123456m };
List<Person> listA = new List<Person>();
for (int i = 0; i < 10; i++)
{
listA.Add(personA);
}

//serialize list to file
XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
XmlTextWriter writer = new XmlTextWriter("Test.xml", Encoding.UTF8);
serializer.Serialize(writer, listA);
writer.Close();

//deserialize list from file
serializer = new XmlSerializer(typeof(List<ProxysNamespace.Person>));
List<ProxysNamespace.Person> listB;
using (FileStream file = new FileStream("Test.xml", FileMode.Open))
{
//configure proxy reader
XmlSoapProxyReader reader = new XmlSoapProxyReader(file);
reader.ProxyNamespace = "http://myappns.com/"; //the namespace of the XmlTypeAttribute
reader.ProxyType = typeof(ProxysNamespace.Person); //the type with the XmlTypeAttribute

//deserialize
listB = (List<ProxysNamespace.Person>)serializer.Deserialize(reader);
}

//display list
foreach (ProxysNamespace.Person p in listB)
{
Console.WriteLine(p.ToString());
}

Console.ReadLine();
}
}

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime StartDate { get; set; }
public decimal Money { get; set; }
}

namespace ProxysNamespace
{
[XmlTypeAttribute(Namespace = "http://myappns.com/")]
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public DateTime Birthday { get; set; }
public decimal Money { get; set; }

public override string ToString()
{
return string.Format("{0}:{1},{2:d}:{3:c2}", Name, Age, Birthday, Money);
}
}
}

public class XmlSoapProxyReader : XmlTextReader
{
List<object> propNames;
public XmlSoapProxyReader(Stream input)
: base(input)
{
propNames = new List<object>();
}

public string ProxyNamespace { get; set; }

private Type proxyType;
public Type ProxyType
{
get { return proxyType; }
set
{
proxyType = value;
PropertyInfo[] properties = proxyType.GetProperties();
foreach (PropertyInfo p in properties)
{
propNames.Add(p.Name);
}
}
}

public override string NamespaceURI
{
get
{
object localname = LocalName;
if (propNames.Contains(localname))
return ProxyNamespace;
else
return string.Empty;
}
}
}

Remove namespace xml of a property during XML serialization

If you don't own the type Name, you can still control its serialisation by overriding the XML attributes using XmlAttributeOverrides when creating the serialiser.

For example:

var overrides = new XmlAttributeOverrides();

overrides.Add(typeof(Name), new XmlAttributes());

var serializer = new XmlSerializer(
typeof(A), overrides, null, null, string.Empty);

See this fiddle for a working demo.

InvalidOperationException when deserializing custom XML (lack of namespace)

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))


Related Topics



Leave a reply



Submit