web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

XML levels - Serialization

(0) ShareShare
ReportReport
Posted on by 172

I'm looking for some advice. Trying to do a serialization by using a c# linq library. Below some sample code, Sorry for the language in it.

I do want to know how to refeer to deeper elements. I can access to lower levels of elements like this but how do I assign values there? Do I need to create a method for every layer?

     [Serializable]
    [XmlRoot(ElementName = "Faktura")]
    public class KSeF_Faktura : KSeF_SerializeInvoice
    {
        [XmlElement(ElementName = "Naglowek")]
        public KSeF_Naglowek Naglowek { get; set; }

        [XmlElement(ElementName = "Podmiot1")]
        public KSeF_Podmiot1 Podmiot1 { get; set; }

        [XmlElement(ElementName = "Podmiot2")]
        public KSeF_Podmiot2 Podmiot2 { get; set; }

        [XmlElement(ElementName = "Fa")]
        public KSeF_Fa Fa { get; set; }

        [XmlElement(ElementName = "Stopka")]
        public KSeF_Stopka Stopka { get; set; }
    }
 [XmlType(TypeName = "Podmiot1")]
    public class KSeF_Podmiot1
    {
        public String PrefiksPodatnika { get; set; }
        public String NrEORI { get; set; }
        [XmlElement(ElementName = "DaneIdentyfikacyjne")]
        public DaneIdentyfikacyjne DaneIdentyfikacyjne { get; set; }
        [XmlElement(ElementName = "Adres")]
        public Adres1 Adres { get; set; }
        [XmlElement(ElementName = "AdresKoresp")]
        public AdresKoresp1 AdresKoresp { get; set; }
        public String Email { get; set; }
        public String Telefon { get; set; }
        public String StatusInfoPodatnika { get; set; }
    }

    [XmlType(TypeName = "DaneIdentyfikacyjne")]
    public class DaneIdentyfikacyjne
    {
        public String NIP { get; set; }
        public String NazwaHandlowa { get; set; }
        public String PelnaNazwa { get; set; }
        public String ImiePierwsze { get; set; }
        public String Nazwisko { get; set; }

    }

    [XmlType(TypeName = "Adres")]
    public class Adres1
    {
        [XmlElement(ElementName = "AdresPol")]
        public AdresPol1 AdresPol { get; set; }
        [XmlElement(ElementName = "AdresZagr")]
        public AdresZagr1 AdresZagr { get; set; }
    }

    SRM_EDI_KSeF.KSeF_Faktura KSeF_Faktura;
    CustInvoiceJour CustInvoiceJour;
    RecordSortedList           list;
    str xml;

    public void createXML()
    {
        if (list.len() > 0)
        {
            list.first(custInvoiceJour);
            do
            {
                KSeF_Faktura = new SRM_EDI_KSeF.KSeF_Faktura();

                KSeF_Faktura.Naglowek = this.createHeader();
                KSeF_Faktura.Podmiot1 = this.createCustomer();

                xml = KSeF_Faktura.Serialize();
                info(xml);
            }
            while (list.next(custInvoiceJour));
        }
    }

protected SRM_EDI_KSeF.KSeF_Podmiot1 createCustomer()
    {

        SRM_EDI_KSeF.KSeF_Podmiot1 customerinfo = new SRM_EDI_KSeF.KSeF_Podmiot1();
        SRM_EDI_KSeF.Adres1 adres = new SRM_EDI_KSeF.Adres1();
        boolean	anyLines = false;

        customerinfo.Telefon = '123123123';
        customerinfo.adres = "DE";
        customerinfo.adres.AdresZagr = "Bern";
        customerinfo.adres.AdresZagr.NrDomu = "22";
        customerinfo.adres.AdresPol.Miejscowosc. = 'as';

        return customerinfo;
    }

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,959 Most Valuable Professional on at

    You already seem to know how to do it, you just need to fix all the bugs.

    For example, adres.AdresZagr = "Bern" is the right approach, but the property is called Adres, not adres (maybe your original intention was using adres variable instead of customerinfo.Adres). You have another bug just line above (line 33), where you're trying to assign "DE" to the Adres property. You forgot that its type is SRM_EDI_KSeF.Adres1, not string. You should have used your adres variable instead, which you created but never used.

  • RadekM Profile Picture
    172 on at

    Actually Adres should never have any value itself it leads to lower level elements. There's just something I tried out... What I wanted to is to get from this method deep enough to assisng values to children of Adres. Adres is the parent it has 2 children that don't get any value but theyhave more children where the values are finally assigned.

    Is it able to do it like Adres.AdresPol.GLN = '5555' or do I need to go like tihs... Adres.AdresPol = this.GLN and so on.

  • Martin Dráb Profile Picture
    237,959 Most Valuable Professional on at

    Adres should have a value - an instance of SRM_EDI_KSeF.Adres1 class. Using any other type (such as a string) is a bug.

    Regarding your last sentence, both snippets may make sense in some context. It depends on what you have in those variables.

  • RadekM Profile Picture
    172 on at

    So should I go with this..? 

    protected SRM_EDI_KSeF.KSeF_Podmiot1 createPodmiot()
        {
    
            SRM_EDI_KSeF.KSeF_Podmiot1 podmiot = new SRM_EDI_KSeF.KSeF_Podmiot1();
    
            podmiot.Telefon = '123123123';
    
            podmiot.Adres = this.createAdres();
    
            return podmiot;
        }
    
        protected SRM_EDI_KSeF.Adres1 createAdres()
        {
    
            SRM_EDI_KSeF.Adres1 adres = new SRM_EDI_KSeF.Adres1();
    
            adres.AdresPol = this.createAdresPol();
    
            return adres;
        }
    
        protected SRM_EDI_KSeF.AdresPol1 createAdresPol()
        {
    
            SRM_EDI_KSeF.AdresPol1 AdresPol = new SRM_EDI_KSeF.AdresPol1();
    
            AdresPol.GLN = '55';
    
            return AdresPol;
        }

  • Martin Dráb Profile Picture
    237,959 Most Valuable Professional on at

    You can do that.

    Doing each piece in a separate method may be a good idea, but it's not necessary. You can also do like this, if the logic is simple enough:\

    using SRM_EDI_KSeF;
    
    ...
    
    AdresPol1 addressPol = new AdresPol1();
    addressPol.GLN = '55';
    
    Adres1 address = new Adres1();
    adres.AdresPol = addressPol;
    
    KSeF_Podmiot1 customer = new KSeF_Podmiot1();
    customer.Telefon = '123123123';
    customer.Adres = adres;

    It's almost trivial - just creating a few objects and doing a few assignments. The only unusual thing is that X doesn't have object properties, only methods. We normally use syntax a.b = c only for table fields.

  • RadekM Profile Picture
    172 on at

    Thank you, last thing I need to ask is how to create attributes like this:

    
    -
    
    
    -
    
    FA
    
    1
    
    2022-02-15T09:30:47Z
    
    Samplofaktur
    
    

    Right now I'm generating something like this after overriding the Stringwriter for UTF8 encoding..

    
    -
    
    
    -
    
    2a
    
    xyz11
    
    2021-12-10T00:00:00
    
    

  • Martin Dráb Profile Picture
    237,959 Most Valuable Professional on at

    I'm not sure if you mean attributes, namespaces or both.

    Regarding attributes, decorate the property (in C#) with XmlAttribute. For example:

    [XmlAttribute("wersjaSchemy")]
    public string SchemaVersion { get; set; )

  • RadekM Profile Picture
    172 on at

    Adding a attribute ended with:

    - xml attribute showed on parent not child node

    - adding 2 attributes ended with 'There was an error reflecting type '

    - no idea how to add attributes to root element

  • Martin Dráb Profile Picture
    237,959 Most Valuable Professional on at

    Re: xml attribute showed on parent not child node

    If your code doesn't do what you intended, fix it. It seems that the structure of your classes isn't what you actually want. If you need more help, give us a simple example of your code and explain the problem.

    Re: adding 2 attributes ended with 'There was an error reflecting type '

    Doing that it's indeed wrong. If you have a single property for schema version, for example, why would you want to create two attributes for the same value? There is no need for that in the XML you showed to us.

    Re: no idea how to add attributes to root element

    Please provide more information about your requirement. I suspect that you actual mean namespaces.

  • RadekM Profile Picture
    172 on at

    I might be wrong but I see here 2 attributes wersjaSchemy and KodSystemowy. Again root got xmlns, xmlns:xsi and xmlns:etd.

    I need to make it look like this:

    
    -
    
    
    -
    
    FA

    As below it produces one attribute at Naglowek element not in KodFormularza. Addid a second one gave me the reflection error.

    namespace SRM_EDI_KSeF
    {
        [Serializable]
        [XmlRoot(ElementName = "Faktura")]
        public class KSeF_Faktura : KSeF_SerializeInvoice
        {
            [XmlElement(ElementName = "Naglowek")]
            public KSeF_Naglowek Naglowek { get; set; }
    
            [XmlElement(ElementName = "Podmiot1")]
            public KSeF_Podmiot1 Podmiot1 { get; set; }
    
            [XmlElement(ElementName = "Podmiot2")]
            public KSeF_Podmiot2 Podmiot2 { get; set; }
    
            [XmlElement(ElementName = "Fa")]
            public KSeF_Fa Fa { get; set; }
    
            [XmlElement(ElementName = "Stopka")]
            public KSeF_Stopka Stopka { get; set; }
    
            [XmlAttribute("xmlns")]
            public string xmlns { get; set; }
    
            [XmlAttribute("xmlns:xsi")]
            public string xmlnsxsi { get; set; }
    
            [XmlAttribute("xmlns:etd")]
            public string xmlndetd { get; set; }
        }
         
        /// 
        /// /////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// 
    
        [XmlType(TypeName = "Naglowek")]
        public class KSeF_Naglowek
        {
            public String KodFormularza { get; set; }
            [XmlAttribute("wersjaSchemy")]
            public string SchemaVersion { get; set; }
            [XmlAttribute("kodSystemowy")]
            public string SystemCode { get; set; }
            public String WariantFormularza { get; set; }
            public DateTime DataWytworzeniaFa { get; set; }
            public String SystemInfo { get; set; }
        }
    

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 592 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 478 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans