tags:

views:

74

answers:

3

Here's an example of an XML file created in InfoPath:

  <?xml version="1.0" encoding="UTF-8"?>
  <?mso-infoPathSolution solutionVersion="1.0.0.1" productVersion="12.0.0" PIVersion="1.0.0.0" href="file:///C:\Metastorm\Sample%20Procedures\InfoPath%20samples\Template1.xsn" name="urn:schemas-microsoft-com:office:infopath:Template1:-myXSD-2010-07-21T14-21-13" ?>
  <?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>
  <my:myFields xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-07-21T14:21:13" xml:lang="en-us">
    <my:field1>hello</my:field1>
    <my:field2>world</my:field2>
  </my:myFields>

What are those top 3 nodes with the question mark called... and how do I create them in C#?

So far I have this:

  XmlDocument xmldoc;
  XmlDeclaration xmlDeclaration;

  xmldoc=new XmlDocument();
  xmlDeclaration = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "") as XmlDeclaration;
  xmlDeclaration.Encoding = "UTF-8";
  xmldoc.AppendChild(xmlDeclaration);

This works fine for the top XML declaration node , but how do I create the next two?

Thanks in advance :)

+3  A: 

These are called processing instructions. Add 'em using XmlDocument.CreateProcessingInstruction.

Peter Lillevold
Note that the first one isn't a processing instruction. It's the XML document declaration. Use [CreateXmlDeclaration](http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createxmldeclaration.aspx) for this.
Porges
+3  A: 

Those are called processing instructions. You can use the XmlProcessingInstruction class to interact with them in an XmlDocument.

As with most elements defined within an XmlDocument, you cannot instantiate it directly; you must use the appropriate factory method on XmlDocument (CreateProcessingInstruction in that particular case.)

Bryan Menard
A: 

Thanks for explaining that these are processing instructions. Using CreateProcessingInstruction as suggested, here is the solution:

  xmlPi = xmldoc.CreateProcessingInstruction("mso-infoPathSolution", "solutionVersion=\"1.0.0.1\" productVersion=\"12.0.0\" PIVersion=\"1.0.0.0\" href=\"file:///C:\\Metastorm\\Sample%20Procedures\\InfoPath%20samples\\Template1.xsn\" name=\"urn:schemas-microsoft-com:office:infopath:Template1:-myXSD-2010-07-21T14-21-13\"");
  xmldoc.AppendChild(xmlPi);
Lost Hobbit
@Lost Hobbit - great, but you should update your question instead of answering your own question.
Peter Lillevold