title: unit testing xsd
body: <p>I have been tasked with finding out why a program passes an XML unit test when the attributes in the xsd portion do not match the values in the unit test.  The problem is I know next to nothing about XML or xsd.  I have read up on it in various places but nohing has helped me understand why the test is failing.</p>
  
  <p>It seems that before I can figure out why it's not working, I need to understand what is happening in the method doing the unit testing.  I think I grasp most of it, but there is a portion that I'm not sure I understand.  This portion also deals with the xsd.</p>
  
  <p>Here is my xsd:</p>
  
  <pre><code>public static class Constants
  {
      public const string Xsd = @"
  &lt;xs:schema elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema""&gt;
     &lt;xs:element name=""Datas""&gt;
        &lt;xs:complexType&gt;
           &lt;xs:sequence&gt;
             &lt;xs:element name=""Data""&gt;
               &lt;xs:attributeGroup ref=""allocData""/&gt;
             &lt;/xs:element&gt;
           &lt;/xs:sequence&gt;
        &lt;/xs:complexType&gt;
     &lt;/xs:element&gt;
  
     &lt;xs:attributeGroup id=""allocData""&gt;
       &lt;xs:attribute name=""Site"" type=""string""/&gt;    
       &lt;xs:attribute name=""MonthNum"" type=""string""/&gt;
       &lt;xs:attribute name=""Department"" type=""string""/&gt;
       &lt;xs:attribute name=""Numerator"" type=""float""/&gt;    
       &lt;xs:attribute name=""Data_Indicator"" type=""string""/&gt;
       &lt;xs:attribute name=""Industry_Benchmark"" type=""int""/&gt;
       &lt;xs:attribute name=""OSHA_Benchmark"" type=""int""/&gt;    
       &lt;!--xs:attribute name=""Comments"" type=""string""/--&gt;
       &lt;xs:attribute name=""Executive_Comments"" type=""string""/&gt;
       &lt;xs:attribute name=""Fleet_Executive_Comments"" type=""string""/&gt;
     &lt;/xs:attributeGroup&gt;   
  &lt;/xs:schema&gt;";
  </code></pre>
  
  <p>and here is the method for my unit test:</p>
  
  <pre><code>public void XMLTest()
  {
      List&lt;DataResults&gt; ListOfResults = new List&lt;DataResults&gt;();
  
          DataResults r1 = new DataResults();
          DataResults r2 = new DataResults();
          HarvesterHandle testData = new HarvesterHandle();
  
          r1.Site = "Springfield Site";
          r1.Department = "Freak";
          r1.MonthNum = "8";
          r1.Numerator = 65807;
          r1.Data_Indicator = "A12";
          // r1.Comments = "Hello, World";
          r1.Industry_Benchmark = 23;
          r1.OSHA_Benchmark = 2;
          r1.ExecutiveComments = "It costs HOW MUCH?";
          r1.FleetExecComments = "No you can't have a new car";
  
          r2.Site = "Joplin Site";
          r2.Department = "Rock";
          r2.MonthNum = "12";
          r2.Numerator = 65625;
          r2.Data_Indicator = "Ou812";
          r2.Industry_Benchmark = 523;
          r2.OSHA_Benchmark = 2456;
          // r2.Comments = "GoodBye, World!";
          r2.ExecutiveComments = "Cut it!";
          r2.FleetExecComments = "It only has 250,000 miles";
  
          ListOfResults.Add(r1);
          ListOfResults.Add(r2);
  
          var test = testData.XMLTest(ListOfResults);
  
  
          var xmlDoc = XDocument.Parse(test.ToString());
  
          XmlSchemaSet set = new XmlSchemaSet();
          set.Add(XmlSchema.Read(XElement.Parse(Constants.Xsd).CreateReader(), (o, e) =&gt; { })); bool valid = true;
          xmlDoc.Validate(set, (o, e) =&gt;
          {
              valid = false;
          });
          Assert.IsTrue(valid);
  
          var element = test.Descendants("Data").FirstOrDefault(x =&gt; x.Attribute("Site").Value.Equals(r1.Site));
  
          Assert.IsNotNull(element);
  
          Assert.AreEqual(r1.Site, element.Attribute("Site").Value);
          Assert.AreEqual(r1.Department, element.Attribute("Department").Value);
          Assert.AreEqual(r1.MonthNum.ToString(), element.Attribute("MonthNum").Value);
          Assert.AreEqual(r1.Numerator.ToString(), element.Attribute("Numerator").Value);
          Assert.AreEqual(r1.Data_Indicator, element.Attribute("Data_Indicator").Value);
          Assert.AreEqual(r1.Industry_Benchmark.ToString(), element.Attribute("Industry_Benchmark").Value);
          Assert.AreEqual(r1.OSHA_Benchmark.ToString(), element.Attribute("OSHA_Benchmark").Value);
          // Assert.AreEqual(r1.Comments.ToString(), element.Attribute(
          //     "Comments").Value);
          Assert.AreEqual(r1.ExecutiveComments.ToString(), element.Attribute("Executive_Comments").Value);
          Assert.AreEqual(r1.FleetExecComments.ToString(), element.Attribute("Fleet_Executive_Comments").Value);
  
          element = test.Descendants("Data").FirstOrDefault(x =&gt; x.Attribute("Site").Value.Equals(r2.Site));
  
          Assert.IsNotNull(element);
  
          Assert.AreEqual(r2.Site, element.Attribute("Site").Value);
          Assert.AreEqual(r2.Department, element.Attribute("Department").Value);
          Assert.AreEqual(r2.MonthNum.ToString(), element.Attribute("MonthNum").Value);
          Assert.AreEqual(r2.Numerator.ToString(), element.Attribute("Numerator").Value);
          Assert.AreEqual(r2.Data_Indicator, element.Attribute("Data_Indicator").Value);
          Assert.AreEqual(r2.Industry_Benchmark.ToString(), element.Attribute("Industry_Benchmark").Value);
          Assert.AreEqual(r2.OSHA_Benchmark.ToString(), element.Attribute("OSHA_Benchmark").Value);
          // Assert.AreEqual(r2.Comments.ToString(), element.Attribute(
          //    "Comments").Value);
          Assert.AreEqual(r2.ExecutiveComments.ToString(), element.Attribute(Executive_Comments").Value);
          Assert.AreEqual(r2.FleetExecComments.ToString(), element.Attribute("Fleet_Executive_Comments").Value);
      }
  </code></pre>
  
  <p>the part that appear to be dealing with the xsd is this:</p>
  
  <pre><code>XmlSchemaSet set = new XmlSchemaSet();
  set.Add(XmlSchema.Read(XElement.Parse(Constants.Xsd).CreateReader(), (o, e) =&gt; { })); bool valid = true;
  xmlDoc.Validate(set, (o, e) =&gt;
  {
      valid = false;
  });
      Assert.IsTrue(valid);
  </code></pre>
  
  <p>The problem is if the attribute data types in the xsd do not match the actual data, the test still passes.  For instance, if any of the string attributes (MonthNum, Site, etc) are changed to float, the test still passes.  Also, if the .xsd element names Datas and Data are changed to something like Fred and Ethyl, the test still passes.</p>
  
  <p>I am assuming that whatever the xsd portion of the code in the xml unit test method is doing, it is not checking for correct datatypes or element names</p>
  
  <p>per request, here is my xml:</p>
  
  <pre><code>    public const string XSLT = @"
  &lt;xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""&gt;
    &lt;xsl:output method=""text"" encoding=""us-ascii"" /&gt;
    &lt;xsl:template match=""/""&gt;Site, MonthNum, Department, Numerator, Denominator, Data_Indicator, Industry_Benchmark, OSHA_Benchmark, &lt;!--Comments--&gt; Executive_Comments, Fleet_Executive_Comments,
    &lt;xsl:for-each select=""Datas/Data""&gt;
    &lt;xsl:text&gt;'&lt;/xsl:text&gt;
    &lt;xsl:value-of select=""@site""/&gt;
    &lt;xsl:text&gt;','&lt;/xsl:text&gt;      
    &lt;xsl:value-of select=""@monthNum""/&gt;
    &lt;xsl:text&gt;','&lt;/xsl:text&gt;
    &lt;xsl:value-of select=""@Department""/&gt;
    &lt;xsl:text&gt;','&lt;/xsl:text&gt;
    &lt;xsl:value-of select=""@numerator""/&gt;
    &lt;xsl:text&gt;','&lt;/xsl:text&gt;,       
    &lt;xsl:value-of select=""Industry_Benchmark""/&gt;
    &lt;xsl:text&gt;'&lt;/xsl:text&gt;,
    &lt;xsl:value-of select=""OSHA_Benchmark""/&gt;
    &lt;xsl:text&gt;'&lt;/xsl:text&gt;,
    &lt;!--xsl:value-of select=""Comments""/--&gt;
    &lt;!--xsl:text&gt;'&lt;/xsl:text--&gt;,
    &lt;xsl:value-of select=""Executive_Comments""/&gt;
    &lt;xsl:text&gt;'&lt;/xsl:text&gt;,
    &lt;xsl:value-of select=""Fleet_Executive_Comments""/&gt;
    &lt;xsl:text&gt;'&lt;/xsl:text&gt;,
  
    &lt;/xsl:for-each&gt;
    &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;";
  }
  </code></pre>
  
  <p>}</p>
  
  <p>edit:  This is the XML generated when running the test:</p>
  
  <pre><code>&lt;Data&gt;
    &lt;Data Site="Springfield Site" MonthNum="8" Department="Freak" Numerator="65807" Data_Indicator="A12" Industry_Benchmark="23" OSHA_Benchmark="2" Executive_Comments="It costs HOW MUCH?" Fleet_Executive_Comments="No you can't have a new car" /&gt;
    &lt;Data Site="Joplin Site" MonthNum="12" Department="Rock" Numerator="65625" Data_Indicator="Ou812" Industry_Benchmark="523" OSHA_Benchmark="2456" Executive_Comments="Cut it!" Fleet_Executive_Comments="It only has 250,000 miles" /&gt;
  &lt;/Data&gt;
  </code></pre>