Appending new Elements with respect to XSI:TYPE

Joined
Jul 20, 2012
Messages
1
Reaction score
0
Per this XML example:
<tns:grunnlagsdataverdi>
<gass:fraDato>2012-01-03</gass:fraDato>
<gass:tilDato>2012-01-04</gass:tilDato>
<gass:verdi>789</gass:verdi>
<gass:kommentar>Round 3</gass:kommentar>
</tns:grunnlagsdataverdi>

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())
break;
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();

///tns:gassomsetningOppgave/tns:grunnlagsdata/tns:grunnlagsdataverdi
XmlNode grunnlagsdataverdi = doc.CreateElement("tns", "grunnlagsdataverdi", tns);

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

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

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

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

doc.AppendChild(grunnlagsdataverdi);

var space = string.Empty;

space = "/tns:gassomsetningOppgave/tns:grunnlagsdata";
formControl1.XmlForm.MainDataSource.CreateNavigator().SelectSingleNode(space,
nav).AppendChild(doc.DocumentElement.CreateNavigator());
}
}
However, because of the XSI:TYPE reference, this XML example throws me for a loop:
<tns:salg xsi:type="tns:SalgGassGevinstdelingType">
<gass:levertMengde>
<type:levertMengdePerDag>
<type:leveringsDato>2012-07-04</type:leveringsDato>
<type:levertMengdePaaDag>1234</type:levertMengdePaaDag>
</type:levertMengdePerDag>
</gass:levertMengde>
</tns:salg>
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!
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Top