C# with XML file data connections

G

G. Kumar

I have an XML file that I'm trying to read and write to in an infopath form. The data connection that I use says "Recieve Data", however with JScript, using the "save" method of the IXMLDOMDocument object, you can write to the url of the XML file data connection. I am trying to switch my code to C#, however I can't get it to work. In JScript, I did:

var dbxml = XDocument.DataObjects("Database").DOM.xml;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.loadXML(dbxml);
.... modify xmlDoc ...
xmlDoc.save(XDocument.DataObjects("Database").QueryAdapter.FileURL);

Now, my idea for C# was this:

string dbxml = eventObj.XDocument.DataObjects["Database"].DOM.xml;
IXMLDOMDocument xmlDoc = new IXMLDOMDocument();
xmlDoc.loadXML(dbxml);
.... modify xmlDoc ...
object dbxmlfile = eventObj.XDocument.DataObjects["Database"].QueryAdapter;
xmlDoc.save(((XMLFileAdapter) dbxmlfile).FileURL);

I am not able to do the command "new IXMLDOMDocument()" which is not surprising because I totally guessed at how to create a new IXMLDOMDocument. Does anyone else know this or know how to write to XML files using C#? Any help or sample code would be very appreciated. Thanks!
 
G

G. Kumar

So, I think I figured out how you make a new IXMLDOMDocument. It seems to work like this:
IXMLDOMDocument dbDOM = eventObj.XDocument.DataObjects["Database"].DOM;
string dbxml = dbDOM.xml;
IXMLDOMDocument xmlDoc = eventObj.XDocument.CreateDOM();
xmlDoc.loadXML(dbxml);
.... modify xmlDoc ...
object dbxmlfile = eventObj.XDocument.DataObject["Database"].QueryAdapter;
xmlDoc.save(((XMLFileAdapter) dbxmlfile).FileURL);

However, I'm still getting an Access Violation at the save command. Is that because my XML file data connection is "Retrieve Data". Does anyone know how to submit data to an XML file? Or how to fix it so I don't get an Access Violation? Thanks!
 
A

Andrew Ma [MSFT]

What is the FileUrl? If it's not local, then it's not going to save.
Also, if you're not in full trust mode, it won't let you write to disk.

--
Andrew J. Ma
Software Test Engineer
Microsoft Office InfoPath
http://blogs.msdn.com/ajma
---------------------------------------
This posting is provided "As Is" with no warranties, and confers no rights.
Use of any included script sample are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
Please do not send email directly to this alias. This alias is for
newsgroup purposes only.


G. Kumar said:
So, I think I figured out how you make a new IXMLDOMDocument. It seems to
work like this:
IXMLDOMDocument dbDOM = eventObj.XDocument.DataObjects["Database"].DOM;
string dbxml = dbDOM.xml;
IXMLDOMDocument xmlDoc = eventObj.XDocument.CreateDOM();
xmlDoc.loadXML(dbxml);
... modify xmlDoc ...
object dbxmlfile = eventObj.XDocument.DataObject["Database"].QueryAdapter;
xmlDoc.save(((XMLFileAdapter) dbxmlfile).FileURL);

However, I'm still getting an Access Violation at the save command. Is
that because my XML file data connection is "Retrieve Data". Does anyone
know how to submit data to an XML file? Or how to fix it so I don't get an
Access Violation? Thanks!


G. Kumar said:
I have an XML file that I'm trying to read and write to in an infopath
form. The data connection that I use says "Recieve Data", however with
JScript, using the "save" method of the IXMLDOMDocument object, you can
write to the url of the XML file data connection. I am trying to switch
my code to C#, however I can't get it to work. In JScript, I did:

var dbxml = XDocument.DataObjects("Database").DOM.xml;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.loadXML(dbxml);
... modify xmlDoc ...
xmlDoc.save(XDocument.DataObjects("Database").QueryAdapter.FileURL);

Now, my idea for C# was this:

string dbxml = eventObj.XDocument.DataObjects["Database"].DOM.xml;
IXMLDOMDocument xmlDoc = new IXMLDOMDocument();
xmlDoc.loadXML(dbxml);
... modify xmlDoc ...
object dbxmlfile =
eventObj.XDocument.DataObjects["Database"].QueryAdapter;
xmlDoc.save(((XMLFileAdapter) dbxmlfile).FileURL);

I am not able to do the command "new IXMLDOMDocument()" which is not
surprising because I totally guessed at how to create a new
IXMLDOMDocument. Does anyone else know this or know how to write to XML
files using C#? Any help or sample code would be very appreciated.
Thanks!
 
A

Andrew Ma [MSFT]

You can make as individual infopath form full trust. That way, users don' t
have to change global IE security settings.
You can make an Infopath for full trust but that will require users to
isntall the solution on to their computers.

--
Andrew J. Ma
Software Test Engineer
Microsoft Office InfoPath
http://blogs.msdn.com/ajma
---------------------------------------
This posting is provided "As Is" with no warranties, and confers no rights.
Use of any included script sample are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
Please do not send email directly to this alias. This alias is for
newsgroup purposes only.


G. Kumar said:
Yea, it was a trust problem... I'm looking into using a web service
instead. If I put the xml file on a public website url, would each
computer need to change their security settings or just the server that
the file is on? In any case, I have a feeling it would be easier to use a
web service. Thanks for replying.
-Gautam

Andrew Ma said:
What is the FileUrl? If it's not local, then it's not going to save.
Also, if you're not in full trust mode, it won't let you write to disk.

--
Andrew J. Ma
Software Test Engineer
Microsoft Office InfoPath
http://blogs.msdn.com/ajma
---------------------------------------
This posting is provided "As Is" with no warranties, and confers no
rights.
Use of any included script sample are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.
Please do not send email directly to this alias. This alias is for
newsgroup purposes only.


G. Kumar said:
So, I think I figured out how you make a new IXMLDOMDocument. It seems
to
work like this:
IXMLDOMDocument dbDOM = eventObj.XDocument.DataObjects["Database"].DOM;
string dbxml = dbDOM.xml;
IXMLDOMDocument xmlDoc = eventObj.XDocument.CreateDOM();
xmlDoc.loadXML(dbxml);
... modify xmlDoc ...
object dbxmlfile =
eventObj.XDocument.DataObject["Database"].QueryAdapter;
xmlDoc.save(((XMLFileAdapter) dbxmlfile).FileURL);

However, I'm still getting an Access Violation at the save command. Is
that because my XML file data connection is "Retrieve Data". Does
anyone
know how to submit data to an XML file? Or how to fix it so I don't get
an
Access Violation? Thanks!


:

I have an XML file that I'm trying to read and write to in an infopath
form. The data connection that I use says "Recieve Data", however with
JScript, using the "save" method of the IXMLDOMDocument object, you
can
write to the url of the XML file data connection. I am trying to
switch
my code to C#, however I can't get it to work. In JScript, I did:

var dbxml = XDocument.DataObjects("Database").DOM.xml;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
xmlDoc.loadXML(dbxml);
... modify xmlDoc ...
xmlDoc.save(XDocument.DataObjects("Database").QueryAdapter.FileURL);

Now, my idea for C# was this:

string dbxml = eventObj.XDocument.DataObjects["Database"].DOM.xml;
IXMLDOMDocument xmlDoc = new IXMLDOMDocument();
xmlDoc.loadXML(dbxml);
... modify xmlDoc ...
object dbxmlfile =
eventObj.XDocument.DataObjects["Database"].QueryAdapter;
xmlDoc.save(((XMLFileAdapter) dbxmlfile).FileURL);

I am not able to do the command "new IXMLDOMDocument()" which is not
surprising because I totally guessed at how to create a new
IXMLDOMDocument. Does anyone else know this or know how to write to
XML
files using C#? Any help or sample code would be very appreciated.
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