Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have EPCIS Class from this i am looking to generate the XML file

C#
EPCISDocumentType EPCISDOC = new EPCISDocumentType();


          EPCISDOC.schemaVersion = Convert.ToDecimal(1.2);
          EPCISDOC.creationDate = DateTime.Now;

          ManifestItem[] Main = new ManifestItem[1];
          Main[0].Description = "DEscription";
          Main[0].LanguageCode = "Code";
          Main[0].MimeTypeQualifierCode = "1";
          Main[0].UniformResourceIdentifier = "1";

          Scope[] scopearray = new Scope[1];

          scopearray[0].Identifier = "Iden";
          scopearray[0].InstanceIdentifier = "In";
          scopearray[0].ScopeInformation[0] = "23";
          scopearray[0].Type = "type";

          Partner[] Sender = new Partner[1];

          Sender[0].Identifier.Authority = "Authority";
          Sender[0].Identifier.Value = "Value";
          Sender[0].ContactInformation[0].Contact = "Contact";
          Sender[0].ContactInformation[0].ContactTypeIdentifier = "Contact";
          Sender[0].ContactInformation[0].EmailAddress = "Contact";
          Sender[0].ContactInformation[0].FaxNumber = "Contact";
          Sender[0].ContactInformation[0].TelephoneNumber = "Contact";

          Partner[] Receiver = new Partner[1];

          Receiver[0].Identifier.Authority = "Authority";
          Receiver[0].Identifier.Value = "Value";
          Receiver[0].ContactInformation[0].Contact = "Contact";
          Receiver[0].ContactInformation[0].ContactTypeIdentifier = "Contact";
          Receiver[0].ContactInformation[0].EmailAddress = "Contact";
          Receiver[0].ContactInformation[0].FaxNumber = "Contact";
          Receiver[0].ContactInformation[0].TelephoneNumber = "Contact";



          EPCISHeaderType EpcisHeader = new EPCISHeaderType();

          EpcisHeader.StandardBusinessDocumentHeader = new StandardBusinessDocumentHeader
          {
              HeaderVersion = "1",
              DocumentIdentification = new DocumentIdentification
              {
                  CreationDateAndTime = DateTime.Now,
                  InstanceIdentifier = "1",
                  MultipleType = true,
                  MultipleTypeSpecified = true,
                  Standard = "Epcis",
                  Type = "1",
                  TypeVersion = "1.2"
              },
              Manifest = new Manifest
              {
                  NumberOfItems = "1",
                  ManifestItem = Main,

              },
              BusinessScope = scopearray,
              Receiver =Receiver,
              Sender = Sender
          };

          EPCISDOC.EPCISHeader.Equals(EpcisHeader);

          //EPCISDOC.EPCISHeader.StandardBusinessDocumentHeader.HeaderVersion = "1.0";
          //EPCISDOC.EPCISHeader.StandardBusinessDocumentHeader.Sender.Equals(Sender) ;
          //EPCISDOC.EPCISHeader.StandardBusinessDocumentHeader.Receiver.Equals(Receiver);

          XmlSerializer serializer = new XmlSerializer(typeof(EPCISDocumentType));
          serializer.Serialize(File.Create("file.xml"), EPCISDOC);


When I assign any value to the object it gives a null Exception

What I have tried:

Can anyone help to identify the error
Posted
Updated 15-Dec-21 0:24am

1 solution

You haven't really provided enough context on what lines are erroring but from the looking at the code I can see you're not using arrays correctly. When you declare an array, all elements of that array will be uninitialized, so you need to initialize each element before using them.
C#
ManifestItem[] Main = new ManifestItem[1];
Main[0].Description = "DEscription";

The issue is here, you're trying to change the content of the element at Main[0] but this is going to be null. You need to initialize it like so:
C#
ManifestItem[] Main = new ManifestItem[1];
Main[0] = new ManifestItem();                     <---
Main[0].Description = "DEscription";

And you need to apply the same logic for all of the arrays in the code. Just declaring an array doesn't create each of the elements in the array, each element needs it's own new call.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900