Appending new Elements with respect to XSI:TYPE

Discussion in 'InfoPath' started by flAzer, Jul 20, 2012.

  1. flAzer


    Jul 20, 2012
    Likes Received:
    Per this XML example:
    <gass:kommentar>Round 3</gass:kommentar>

    I can easily use this code:
    private static string FindInfoPathNamespace(string nsSought, XPathNavigator nav)
    var nsFound = string.Empty;

    if (nav.MoveToFirstChild())
    nsFound = nav.LookupNamespace(@"xmlns:" + nsSought);
    while (nsFound == null || nsFound.Length <= 0)
    if (!nav.MoveToNext())
    nsFound = nav.LookupNamespace(nsSought);
    return nsFound;

    to find the Namespaces so that, per the following, I can create a new element, and inject it within the XML of my InfoPath document:
    private void InsertElement()
    var nav = formControl1.XmlForm.MainDataSource.CreateNavigator();

    string tns = FindInfoPathNamespace("tns", nav);
    string gass = FindInfoPathNamespace("gass", nav);

    foreach (var rec in priceTable.AsEnumerable())
    XmlDocument doc = new XmlDocument();

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

    XmlNode field = doc.CreateElement("gass", "fraDato", gass);
    XmlNode node = grunnlagsdataverdi.AppendChild(field);
    node.InnerText = DateTime.Parse(rec["fraDato"].ToString()).ToString("yyyy-MM-dd");

    field = doc.CreateElement("gass", "tilDato", gass);
    node = grunnlagsdataverdi.AppendChild(field);
    node.InnerText = DateTime.Parse(rec["tilDato"].ToString()).ToString("yyyy-MM-dd");

    field = doc.CreateElement("gass", "verdi", gass);
    node = grunnlagsdataverdi.AppendChild(field);
    node.InnerText = rec["verdi"].ToString();

    field = doc.CreateElement("gass", "kommentar", gass);
    node = grunnlagsdataverdi.AppendChild(field);
    node.InnerText = rec["kommentar"].ToString();


    var space = string.Empty;

    space = "/tns:gassomsetningOppgave/tns:grunnlagsdata";
    However, because of the XSI:TYPE reference, this XML example throws me for a loop:
    <tns:salg xsi:type="tns:SalgGassGevinstdelingType">
    Apprecaite your reading this post and of course any help you can provide helping me to do the Namespace Lookups and of course, to properly build the Element so that it will play nice with my InfoPath XML stream - thanks!
    flAzer, Jul 20, 2012
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.