Implementing serialization and input/output functionality in a .NET Framework application

These questions are based on 70-622CSHP – TS: Microsoft .NET Framework 2.0 – Application Development Foundation (C#.NET) Microsoft Self-Test Software Practice Test.


Objective: Implementing serialization and input/output functionality in a .NET Framework application
SubObjective: Control the serialization of an object into XML format by using the System.Xml.Serialization namespace


Item No. 70-536CSHP.4.2.1
Single Answer, Multiple Choice


You are an application developer for a company. You have created a point of sale application that serializes sales products into XML. This XML will be consumed by a company partner. You have created the appropriate serializable classes as shown in the exhibit. (Click the Exhibit(s) button.)


You decide to create the following code to test the serialization process:


public void Serialize ( string filename ) {
     XmlSerializer serializer = new XmlSerializer( typeof( Region ) );
     StreamWriter writer = new StreamWriter( filename );
     SalesProduct product1 = new SalesProduct();
     product1.Name = “Sofa”;
     product1.IsTaxable = true;
     Product productDisplay = new Product();
     productDisplay.Name = “Television”;
     Region region = new Region();
     region.Name = “East”;
     region.Products = new Product[] { product1, productDisplay };
     serializer.Serialize( writer, region );
     writer.Close();
}


If you were to invoke the Serialize method, what would be the contents of the file?



  1. <?xml version=”1.0″ encoding=”utf-8″?>
    <Region xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
      xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
         <Products>
              <Product> IsTaxable=”true”>Sofa</Product>
              <Product>Television</Product>
         </Products>
    </Region>


  2. <?xml version=”1.0″ encoding=”utf-8″?>
    <Region xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
      xmlns:xsd=”http://www.w3.org/2001/XMLSchema”>
         <Products>
              <SalesProduct>
                   <IsTaxable>true</IsTaxable>
                   <Name>Sofa</Name>
              </SalesProduct>
              <Product>
                   <Name>Television</Name>
              </Product>
         </Products>
    </Region>


  3. <?xml version=”1.0″ encoding=”utf-8″?>
    <Region xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
      xmlns:xsd=http://www.w3.org/2001/XMLSchema>
         <Name>East</Name>
         <Products>
              <SalesProduct Taxable=”true”>
                   <Name>Sofa</Name>
              </SalesProduct>
              <Product>
                   <Name>Television</Name>
              </Product>
         </Products>
    </Region>


  4. <?xml version=”1.0″ encoding=”utf-8″?>
    <Region xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
      xmlns:xsd=”http://www.w3.org/2001/XMLSchema” Area=”East”>
         <Products>
              <SalesProduct Taxable=”true”>
                   <Name>Sofa</Name>
              </SalesProduct>
              <Product>
                   <Name>Television</Name>
              </Product>
         </Products>
    </Region>

Answer:



  1. <?xml version=”1.0″ encoding=”utf-8″?>
    <Region xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
      xmlns:xsd=”http://www.w3.org/2001/XMLSchema” Area=”East”>
         <Products>
              <SalesProduct Taxable=”true”>
                   <Name>Sofa</Name>
              </SalesProduct>
              <Product>
                   <Name>Television</Name>
              </Product>
         </Products>
    </Region>

Tutorial:
If invoked, the Serialize method would generate the following file contents:


<?xml version=”1.0″ encoding=”utf-8″?>
<Region xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
  xmlns:xsd=”http://www.w3.org/2001/XMLSchema” Area=”East”>
     <Products>
          <SalesProduct Taxable=”true”>
               <Name>Sofa</Name>
          </SalesProduct>
          <Product>
               <Name>Television</Name>
          </Product>
     </Products>
</Region>


This code is generated because of the inclusion and exclusion of XML serialization attributes. The Product class contains no XML-specific attributes, so the Name field will translate into the Name element. The ID field will not serialize because it is a private member. The XmlElement attribute can specify the element name if it is different from the field name. The SalesProduct class inherits the Product class and adds the IsTaxable

Like what you see? Share it.Google+LinkedInFacebookRedditTwitterEmail
cmadmin

ABOUT THE AUTHOR

Posted in Archive|

Comment: