locked
Populate Repeating Table in C# with Subgroups by appending XmlDocument RRS feed

  • Question

  • I know this question has been asked and answered before but here is a unique situation where I can not get it to work.  I am getting the dreaded "Schema validation found non-data type errors." error message.  I have deleted all of the "nil" attributes and built the XmlDocument correctly I believe.

    Here is my xpath structure

    /my:myFields/my:TEPC
    /my:myFields/my:TEPC/my:TEPCStartDate
    /my:myFields/my:TEPC/my:TEPCStopDate
    /my:myFields/my:TEPC/my:TEPCByPosition
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TEPCPosition
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TEPCPositionStartDate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TEPCPositionStopDate
    
    //GCR Subgroup
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRDose
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRDoseEquivalent
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRQF
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRDoseRate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRDoseEquivalentRate
    
    //Trapped Particle Subgroup
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleDose
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleDoseEquivalent
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleQF
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleDoseRate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleDoseEquivalentRate
    
    //Solar Particle Subgroup
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleDose
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleDoseEquivalent
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleQF
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleDoseRate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleDoseEquivalentRate
    
    //Total Particle Subgroup
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleDose
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleDoseEquivalent
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleQF
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleDoseRate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleDoseEquivalentRate
    

    Here is the code

        private void PopulateTEPCData()
        {
          //Set overall TEPC start and stop dates based upon dock and undock times
          XPathNavigator TEPCStartDate = root.SelectSingleNode("/my:myFields/my:TEPC/my:TEPCStartDate", NamespaceManager);
          XPathNavigator TEPCStopDate = root.SelectSingleNode("/my:myFields/my:TEPC/my:TEPCStopDate", NamespaceManager);
          DeleteNil(TEPCStartDate);
          DeleteNil(TEPCStopDate);
          TEPCStartDate.SetValue(DockDateTime.ToString());
          TEPCStopDate.SetValue(UnDockDateTime.ToString());
    
          //Set TEPC start and stop dates based upon dock and undock times for each TEPC position during the period
          //Delete all nils
          XPathNavigator TEPCPositionStartDate = root.SelectSingleNode("/my:myFields/my:TEPC/my:TEPCByPosition/my:TEPCPositionStartDate", NamespaceManager);
          XPathNavigator TEPCPositionStopDate = root.SelectSingleNode("/my:myFields/my:TEPC/my:TEPCByPosition/my:TEPCPositionStopDate", NamespaceManager);
          DeleteNil(TEPCPositionStartDate);
          DeleteNil(TEPCPositionStopDate);
          XPathNavigator GCRtop = root.SelectSingleNode("/my:myFields/my:TEPC/my:TEPCByPosition/my:GCR", NamespaceManager);
          XPathNodeIterator GCRnodes = GCRtop.SelectChildren(XPathNodeType.Element);
          foreach (XPathNavigator nodes in GCRnodes)
            DeleteNil(nodes);
          XPathNavigator TrappedParticletop = root.SelectSingleNode("/my:myFields/my:TEPC/my:TEPCByPosition/my:GCR", NamespaceManager);
          XPathNodeIterator TrappedParticlenodes = TrappedParticletop.SelectChildren(XPathNodeType.Element);
          foreach (XPathNavigator nodes in TrappedParticlenodes)
            DeleteNil(nodes);
          XPathNavigator SolarParticletop = root.SelectSingleNode("/my:myFields/my:TEPC/my:TEPCByPosition/my:GCR", NamespaceManager);
          XPathNodeIterator SolarParticlenodes = SolarParticletop.SelectChildren(XPathNodeType.Element);
          foreach (XPathNavigator nodes in SolarParticlenodes)
            DeleteNil(nodes);
          XPathNavigator Totaltop = root.SelectSingleNode("/my:myFields/my:TEPC/my:TEPCByPosition/my:GCR", NamespaceManager);
          XPathNodeIterator Totalnodes = Totaltop.SelectChildren(XPathNodeType.Element);
          foreach (XPathNavigator nodes in Totalnodes)
            DeleteNil(nodes);
          string TEPCactlogNewSQLcmd = TEPCactlogOrigSQLcmd + " AND \"LogDate\" >= '" + DateTime.Parse(DockDateTime.ToString()) + "' AND \"LogDate\" <= '" + DateTime.Parse(UnDockDateTime.ToString()) + "'";
          TEPCactlog.Command = TEPCactlogNewSQLcmd;
          TEPCactlog.Execute();
          XPathNodeIterator TEPCactnav = this.DataSources["ISSTEPCActivityLog"].CreateNavigator().Select("/dfs:myFields/dfs:dataFields/d:ActivityLog", NamespaceManager);
          int count = 1;
          int last = TEPCactnav.Count;
          foreach (XPathNavigator TEPC in TEPCactnav)
          {
            
            XmlDocument doc = new XmlDocument();
            XmlNode TEPCtop = doc.CreateElement("TEPCByPosition", NamespaceManager.LookupNamespace("my"));
            //Set Positions
            XmlNode field = doc.CreateElement("TEPCPosition", NamespaceManager.LookupNamespace("my"));
            XmlNode node = TEPCtop.AppendChild(field);
            node.InnerText = TEPC.SelectSingleNode("@Location", NamespaceManager).Value.ToString();
            //Set start date (=Dock Date for first date)
            field = doc.CreateElement("TEPCPositionStartDate", NamespaceManager.LookupNamespace("my"));
            node = TEPCtop.AppendChild(field);
            if (count == 1)
            {
              node.InnerText = DockDateTime.ToString();
            }
            else
            {
              node.InnerText = TEPC.SelectSingleNode("@LogDate", NamespaceManager).ToString();
            }
            field = doc.CreateElement("TEPCPositionStopDate", NamespaceManager.LookupNamespace("my"));
            node = TEPCtop.AppendChild(field);
            if (count == last)
            {
              node.InnerText = UnDockDateTime.ToString();
            }
            else
            {
              node.InnerText = TEPC.SelectSingleNode("@LogDate", NamespaceManager).ToString();
            }
    
            field = doc.CreateElement("GCR", NamespaceManager.LookupNamespace("my"));
            XmlNode GCR = doc.CreateElement("GCRDose", NamespaceManager.LookupNamespace("my"));
            XmlNode GCRnode = field.AppendChild(GCR);
            GCRnode.InnerText = "0.1";
            GCR = doc.CreateElement("GCRDoseEquivalent", NamespaceManager.LookupNamespace("my"));
            GCRnode = field.AppendChild(GCR);
            GCRnode.InnerText = "0.1";
            GCR = doc.CreateElement("GCRDoseQF", NamespaceManager.LookupNamespace("my"));
            GCRnode = field.AppendChild(GCR);
            GCRnode.InnerText = "0.1";
            GCR = doc.CreateElement("GCRDoseRate", NamespaceManager.LookupNamespace("my"));
            GCRnode = field.AppendChild(GCR);
            GCRnode.InnerText = "0.1";
            GCR = doc.CreateElement("GCRDoseEquivalentRate", NamespaceManager.LookupNamespace("my"));
            GCRnode = field.AppendChild(GCR);
            GCRnode.InnerText = "0.1";
            node = TEPCtop.AppendChild(field);
    
            field = doc.CreateElement("TrappedParticle", NamespaceManager.LookupNamespace("my"));
            XmlNode TrappedParticle = doc.CreateElement("TrappedParticleDose", NamespaceManager.LookupNamespace("my"));
            XmlNode TrappedParticlenode = field.AppendChild(TrappedParticle);
            TrappedParticlenode.InnerText = "0.1";
            TrappedParticle = doc.CreateElement("TrappedParticleDoseEquivalent", NamespaceManager.LookupNamespace("my"));
            TrappedParticlenode = field.AppendChild(TrappedParticle);
            TrappedParticlenode.InnerText = "0.1";
            TrappedParticle = doc.CreateElement("TrappedParticleDoseQF", NamespaceManager.LookupNamespace("my"));
            TrappedParticlenode = field.AppendChild(TrappedParticle);
            TrappedParticlenode.InnerText = "0.1";
            TrappedParticle = doc.CreateElement("TrappedParticleDoseRate", NamespaceManager.LookupNamespace("my"));
            TrappedParticlenode = field.AppendChild(TrappedParticle);
            TrappedParticlenode.InnerText = "0.1";
            TrappedParticle = doc.CreateElement("TrappedParticleDoseEquivalentRate", NamespaceManager.LookupNamespace("my"));
            TrappedParticlenode = field.AppendChild(TrappedParticle);
            TrappedParticlenode.InnerText = "0.1";
            node = TEPCtop.AppendChild(field);
    
            field = doc.CreateElement("SolarParticle", NamespaceManager.LookupNamespace("my"));
            XmlNode SolarParticle = doc.CreateElement("SolarParticleDose", NamespaceManager.LookupNamespace("my"));
            XmlNode SolarParticlenode = field.AppendChild(SolarParticle);
            SolarParticlenode.InnerText = "0.1";
            SolarParticle = doc.CreateElement("SolarParticleDoseEquivalent", NamespaceManager.LookupNamespace("my"));
            SolarParticlenode = field.AppendChild(SolarParticle);
            SolarParticlenode.InnerText = "0.1";
            SolarParticle = doc.CreateElement("SolarParticleDoseQF", NamespaceManager.LookupNamespace("my"));
            SolarParticlenode = field.AppendChild(SolarParticle);
            SolarParticlenode.InnerText = "0.1";
            SolarParticle = doc.CreateElement("SolarParticleDoseRate", NamespaceManager.LookupNamespace("my"));
            SolarParticlenode = field.AppendChild(SolarParticle);
            SolarParticlenode.InnerText = "0.1";
            SolarParticle = doc.CreateElement("SolarParticleDoseEquivalentRate", NamespaceManager.LookupNamespace("my"));
            SolarParticlenode = field.AppendChild(SolarParticle);
            SolarParticlenode.InnerText = "0.1";
            node = TEPCtop.AppendChild(field);
    
            field = doc.CreateElement("Total", NamespaceManager.LookupNamespace("my"));
            XmlNode TotalParticle = doc.CreateElement("TotalParticleDose", NamespaceManager.LookupNamespace("my"));
            XmlNode TotalParticlenode = field.AppendChild(TotalParticle);
            TotalParticlenode.InnerText = "0.1";
            TotalParticle = doc.CreateElement("TotalParticleDoseEquivalent", NamespaceManager.LookupNamespace("my"));
            TotalParticlenode = field.AppendChild(TotalParticle);
            TotalParticlenode.InnerText = "0.1";
            TotalParticle = doc.CreateElement("TotalParticleDoseQF", NamespaceManager.LookupNamespace("my"));
            TotalParticlenode = field.AppendChild(TotalParticle);
            TotalParticlenode.InnerText = "0.1";
            TotalParticle = doc.CreateElement("TotalParticleDoseRate", NamespaceManager.LookupNamespace("my"));
            TotalParticlenode = field.AppendChild(TotalParticle);
            TotalParticlenode.InnerText = "0.1";
            TotalParticle = doc.CreateElement("TotalParticleDoseEquivalentRate", NamespaceManager.LookupNamespace("my"));
            TotalParticlenode = field.AppendChild(TotalParticle);
            TotalParticlenode.InnerText = "0.1";
            node = TEPCtop.AppendChild(field);
            
    
            
            doc.AppendChild(TEPCtop);
            root.CreateNavigator().SelectSingleNode("/my:myFields/my:TEPC", NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());
            count = count + 1;
            
          }
    Here is the xml for the xmlDocument after the first loop
    - <TEPCByPosition xmlns="http://schemas.microsoft.com/office/infopath/2003/myXSD/2011-02-22T15:33:23">
     <TEPCPosition>COL1 A3</TEPCPosition> 
     <TEPCPositionStartDate>2/9/2008 5:17:00 PM</TEPCPositionStartDate> 
     <TEPCPositionStopDate>3/3/2008 11:42:00 AM</TEPCPositionStopDate> 
    - <GCR>
     <GCRDose>0.1</GCRDose> 
     <GCRDoseEquivalent>0.1</GCRDoseEquivalent> 
     <GCRDoseQF>0.1</GCRDoseQF> 
     <GCRDoseRate>0.1</GCRDoseRate> 
     <GCRDoseEquivalentRate>0.1</GCRDoseEquivalentRate> 
     </GCR>
    - <TrappedParticle>
     <TrappedParticleDose>0.1</TrappedParticleDose> 
     <TrappedParticleDoseEquivalent>0.1</TrappedParticleDoseEquivalent> 
     <TrappedParticleDoseQF>0.1</TrappedParticleDoseQF> 
     <TrappedParticleDoseRate>0.1</TrappedParticleDoseRate> 
     <TrappedParticleDoseEquivalentRate>0.1</TrappedParticleDoseEquivalentRate> 
     </TrappedParticle>
    - <SolarParticle>
     <SolarParticleDose>0.1</SolarParticleDose> 
     <SolarParticleDoseEquivalent>0.1</SolarParticleDoseEquivalent> 
     <SolarParticleDoseQF>0.1</SolarParticleDoseQF> 
     <SolarParticleDoseRate>0.1</SolarParticleDoseRate> 
     <SolarParticleDoseEquivalentRate>0.1</SolarParticleDoseEquivalentRate> 
     </SolarParticle>
    - <Total>
     <TotalParticleDose>0.1</TotalParticleDose> 
     <TotalParticleDoseEquivalent>0.1</TotalParticleDoseEquivalent> 
     <TotalParticleDoseQF>0.1</TotalParticleDoseQF> 
     <TotalParticleDoseRate>0.1</TotalParticleDoseRate> 
     <TotalParticleDoseEquivalentRate>0.1</TotalParticleDoseEquivalentRate> 
     </Total>
     </TEPCByPosition>
    This should work?  The only thing I can think of is that is choking on the subgroups.  Please look at the code and see if there is something I am missing.

    Charles Picco

     


    Tuesday, May 24, 2011 8:25 PM

Answers

  • After some consternation, I have found the problem.  You cannot populate a repeating node programatically if it has siblings.  The repeating node must be by itself. For example my original structure looked like this:

    /my:myFields/my:TEPC
    /my:myFields/my:TEPC/my:TEPCStartDate
    /my:myFields/my:TEPC/my:TEPCStopDate
    /my:myFields/my:TEPC/my:TEPCByPosition
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TEPCPosition
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TEPCPositionStartDate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TEPCPositionStopDate

    //GCR Subgroup
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRDose
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRDoseEquivalent
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRQF
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRDoseRate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:GCR/my:GCRDoseEquivalentRate

    //Trapped Particle Subgroup
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleDose
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleDoseEquivalent
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleQF
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleDoseRate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:TrappedParticle/my:TrappedParticleDoseEquivalentRate

    //Solar Particle Subgroup
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleDose
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleDoseEquivalent
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleQF
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleDoseRate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:SolarParticle/my:SolarParticleDoseEquivalentRate

    //Total Particle Subgroup
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleDose
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleDoseEquivalent
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleQF
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleDoseRate
    /my:myFields/my:TEPC/my:TEPCByPosition/my:Total/my:TotalParticleDoseEquivalentRate

    However, since these elements

    /my:myFields/my:TEPC/my:TEPCStartDate
    /my:myFields/my:TEPC/my:TEPCStopDate


    were siblings to my repeating node:

    /my:myFields/my:TEPC/my:TEPCByPosition

    the AppendChild method would not work.

    Hope this is useful to other people.


    Charles Picco
    • Marked as answer by Charles Picco Wednesday, May 25, 2011 6:04 PM
    Wednesday, May 25, 2011 6:04 PM