<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://www.infopathdev.com:443/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Code</title><link>https://www.infopathdev.com:443/forums/45.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007 SP3 (Build: 31118.962)</generator><item><title>Connect to XML via Managed Code (C#)</title><link>https://www.infopathdev.com:443/forums/thread/43569.aspx</link><pubDate>Mon, 29 Jun 2009 18:23:00 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:43569</guid><dc:creator>ccarns</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/43569.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=43569</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Could someone point me to an example of creating a data connection to an XML file for a secondary data source? The data in question&amp;nbsp;is being created by a different InfoPath form on the same server and in the same folder as this one, not that it should make much difference.&lt;/p&gt;
&lt;p&gt;The example I find are all creating it via the GUI and I need to do in the code behind because my connection is dynamic (path parameter coming into the form via the URL -which I have already worked out).&lt;/p&gt;
&lt;p&gt;Environment:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;div&gt;MOSS&amp;nbsp;2007&amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;
&lt;div&gt;Using form services and web forms (2007)&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;p&gt;All I am trying to accomplish is to display a few fields from the other XML file in this form as read only information.&lt;/p&gt;
&lt;p&gt;Thanks ahead of time for your time and effort!&lt;/p&gt;
&lt;p&gt;==========UPDATE========================================&lt;/p&gt;This code almost works but I run into authentication issues - and I set the form to FULL TRUST under FORM OPTIONS:&lt;font size="2"&gt;&lt;/font&gt;&lt;font size="2"&gt; 
&lt;p&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt;///////////////////////////////////////////////////////////////////////////&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt;&lt;font color="#000000" size="2"&gt; DocTemplateURI = &amp;quot;&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;&lt;a href="https://%7bdomain/folderpaths%7D/sampledata.xml?NoRedirect=true"&gt;https://{domain/folderpaths}/sampledata.xml?NoRedirect=true&lt;/a&gt;&amp;quot;&lt;/font&gt;&lt;/font&gt;&lt;font color="#000000" size="2"&gt;;&lt;/font&gt;&lt;/p&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;
&lt;p&gt;System.Net.&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;WebRequest&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; webreq = System.Net.&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;WebRequest&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;.Create(DocTemplateURI);&lt;br /&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt;//webreq.Credentials = new System.Net.NetworkCredential(&amp;quot;{username}&amp;quot;, &amp;quot;{pass&amp;quot;);&amp;nbsp;&amp;nbsp; //this didnt seem to help&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;System.Net.&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;WebResponse&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; webres = webreq.GetResponse();&lt;br /&gt;System.IO.&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;Stream&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; xmlStream = webres.GetResponseStream();&lt;br /&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;XmlDocument&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; doc = &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;new&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;XmlDocument&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;();&lt;br /&gt;&lt;/font&gt;&lt;font size="2"&gt;doc.Load(xmlStream);&lt;br /&gt;&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#008000"&gt;// Grab some data from new datasource&lt;/font&gt;&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&lt;font color="#0000ff" size="2"&gt;string&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; title = doc.SelectSingleNode(&amp;quot;&lt;font color="#a31515"&gt;sample/@attirbute&amp;quot;&lt;/font&gt;&lt;/font&gt;&lt;a href="mailto:sample/@attribute).InnerText"&gt;&lt;font size="2"&gt;).InnerText&lt;/a&gt;;&lt;br /&gt;&lt;font color="#008000"&gt;// Get reference to main datasource field I want to populate with secondary data field&lt;br /&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#2b91af" size="2"&gt;&lt;font color="#2b91af" size="2"&gt;XPathNavigator&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt; fieldContent = MainDataSource.CreateNavigator().SelectSingleNode(&lt;/font&gt;&lt;font color="#a31515" size="2"&gt;&lt;font color="#a31515" size="2"&gt;&amp;quot;sections/section/content&amp;quot;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;, NamespaceManager);&lt;br /&gt;fieldContent.SetValue(title);&lt;/p&gt;&lt;/font&gt;</description></item></channel></rss>