Appending elements using XPathNavigator LookupNamespace and the dreaded and embeded reference to [xsi:type=""] - InfoPath Dev
in

InfoPath Dev

Appending elements using XPathNavigator LookupNamespace and the dreaded and embeded reference to [xsi:type=""]

Last post 07-20-2012 09:34 AM by flAzer. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 07-19-2012 07:44 PM

    • flAzer
    • Not Ranked
    • Joined on 07-19-2012
    • Posts 3

    Appending elements using XPathNavigator LookupNamespace and the dreaded and embeded reference to [xsi:type=""]

    Hello,

    While hosting an INFOPATH file in a c# WINFORM application, I'm having a problem referencing the XML namespaces below (the last XML example).
    Well, better stated, I'm having an issue trying to INSERT the snippet below.

    The [xsi:type=""] reference is I think is giving me an issue

    But before I deal refer to my diffucult snippet, here's one that is relatively easy to insert (well, easy now !)

    <tns:grunnlagsdata>
      <tns:grunnlagsdataverdi>
        <gass:fraDato>2012-07-05</gass:fraDato>
        <gass:tilDato>2012-07-05</gass:tilDato>
        <gass:verdi>123</gass:verdi>
        <gass:kommentar>abc</gass:kommentar>
      </tns:grunnlagsdataverdi>
      <tns:grunnlagsdataverdi>
        <gass:fraDato>2012-07-19</gass:fraDato>
        <gass:tilDato>2012-07-21</gass:tilDato>
        <gass:verdi>456</gass:verdi>
        <gass:kommentar>xyz</gass:kommentar>
      </tns:grunnlagsdataverdi>
    </tns:grunnlagsdata>

    It's easy because the code to look up the namespaces, similar to this, does the job nicely.

    private bool FindInfoPathNamespace(string ns)
    {
      var navigator2 = formControl1.XmlForm.MainDataSource.CreateNavigator();
      var namespaceName2 = string.Empty;
      var foundNamespace2 = false;
      if (navigator2.MoveToFirstChild())
      {
        namespaceName2 = navigator2.LookupNamespace(@"xmlns:" + ns);
        while (namespaceName2 == null || namespaceName2.Length <= 0)
        {
          if (!navigator2.MoveToNext())
            break;
          namespaceName2 = navigator2.LookupNamespace(ns);
        };
      }
      if (namespaceName2 != null && namespaceName2.Length > 0)
        foundNamespace2 = true;
      else
        foundNamespace2 = false;
    }

    (code above is hacked a little just for clarity)

    In each case, I can pass the Namespace Prefixs to the function and get the full path  - in the case of either of these (tns / gass).
    And the code to create and insert the element might go like this, after I've populated (tns / gass):

          XmlNode grunnlagsdataverdi = doc.CreateElement("tns", "grunnlagsdataverdi", tns);

          XmlNode field = doc.CreateElement("gass", "fraDato", gass);

          XmlNode node = grunnlagsdataverdi.AppendChild(field);

          node.InnerText = "2012-01-01";

     

          field = doc.CreateElement("gass", "tilDato", gass);

          node = grunnlagsdataverdi.AppendChild(field);

          node.InnerText = "2012-01-02";

     

          field = doc.CreateElement("gass", "verdi", gass);

          node = grunnlagsdataverdi.AppendChild(field);

          node.InnerText = "996";

     

          field = doc.CreateElement("gass", "kommentar", gass);

          node = grunnlagsdataverdi.AppendChild(field);

          node.InnerText = "I did it!";

     

          doc.AppendChild(grunnlagsdataverdi);

     

          var space = "xyz";// abbreviated for clarity.

     

          formControl1.XmlForm.MainDataSource.CreateNavigator().SelectSingleNode(space,

            NamespaceManager).AppendChild(doc.DocumentElement.CreateNavigator());

    But the example below really sends me for a whirl - not really sure how to pull the namespaces , I suspect, because of xsi:type="" references.

    <tns:salg xsi:type="tns:SalgGassGevinstdelingType">
      <gass:levertMengde>
        <type:levertMengdePerDag>
          <type:leveringsDato>2012-07-12</type:leveringsDato>
          <type:levertMengdePaaDag>123</type:levertMengdePaaDag>
        </type:levertMengdePerDag>
        <type:levertMengdePerDag>
          <type:leveringsDato>2012-07-19</type:leveringsDato>
          <type:levertMengdePaaDag>545</type:levertMengdePaaDag>
        </type:levertMengdePerDag>
      </gass:levertMengde>
    </tns:salg>

     Appreciate your assistance, thanks!

  • 07-20-2012 09:34 AM In reply to

    • flAzer
    • Not Ranked
    • Joined on 07-19-2012
    • Posts 3

    Re: Appending elements using XPathNavigator LookupNamespace and the dreaded and embeded reference to [xsi:type=""]

    Hmm I think the answer is related to something like:
     
    XmlAttribute nsAttribute = doc.CreateAttribute("type", "xx", "tns:SalgGassGevinstdelingType");newElement.Attributes.Append(nsAttribute);
Page 1 of 1 (2 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.