Generated Decode Methods when Using the Default Parser (XmlReader)

Generated classes will have two methods for decoding:

public void DecodeDocument(System.Xml.XmlReader reader)

public void Decode(System.Xml.XmlReader reader, bool asGroup)
            

The DecodeDocument method should be used when the parser is positioned at the start of an XML document, making it necessary to move to the root element before reaching the data to be decoded. This is the method you will most likely use.

The Decode method should be used when the parser is already positioned on the element to be decoded. If the current element contains the content to be decoded, invoke Decode with asGroup = false. If, however, the current element is itself part of the content to be decoded (and, in some cases, its siblings also), then invoke Decode with asGroup = true. Most likely, you will pass false.

To illustrate, suppose that we have a SEQUENCE type that defines elem1 and elem2. If we have an XML snippet to decode, <root><elem1/><elem2/></root>, and we are positioned on the root element, we would use asGroup = false, because root contains the content to be decoded and is not a part of it. If, however, we were positioned on the elem1 element, we would use group = true, because elem1 is a part of the content to be decoded; it is the beginning of the content itself.

The general procedure for decoding is as follows:

  1. Create an XMLReader on the input stream to be decoded.

  2. Instantiate an instance of the generated class you want to decode.

  3. Invoke the DecodeDocument method on the generated class.

  4. Access the decoded data through the public members of the generated class.

A program fragment that could be used to decode an employee record is as follows:

   public class Reader
   {
      public static void  Main(System.String[] args)
      {
         System.String filename = "message.xml";
         bool trace = true;
                  
         try
         {
            // Create an XML reader object
            
            FileStream fs = new FileStream(filename, FileMode.Open, 
                                             FileAccess.Read, FileShare.Read);
            XmlReader reader = XmlReader.Create(fs);
            
            // Read and decode the message
            
            PersonnelRecord personnelRecord = new PersonnelRecord();
            personnelRecord.DecodeDocument(reader);
            if (trace)
            {
               System.Console.Out.WriteLine("Decode was successful");
               personnelRecord.Print("personnelRecord");
            }
         }
         catch (System.Exception e)
         {
            System.Console.Out.WriteLine(e.Message);
            Asn1Util.WriteStackTrace(e, Console.Error);
            return ;
         }
      }
   }