在XML中,带有问号的节点是什么,以及如何在C#中添加它们?

时间:2021-11-24 20:03:36

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

以下是在InfoPath中创建的XML文件的示例:

  <?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#?

那些带有问号的前三个节点是什么叫......以及如何在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?

这适用于*XML声明节点,但如何创建接下来的两个?

Thanks in advance :)

提前致谢 :)

3 个解决方案

#1


8  

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

这些被称为处理指令。使用XmlDocument.CreateProcessingInstruction添加它们。

#2


7  

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

这些被称为处理指令。您可以使用XmlProcessingInstruction类在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.)

与XmlDocument中定义的大多数元素一样,您无法直接实例化它;您必须在XmlDocument上使用适当的工厂方法(在该特定情况下为CreateProcessingInstruction。)

#3


2  

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

感谢您解释这些是处理说明。使用CreateProcessingInstruction建议,这是解决方案:

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

#1


8  

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

这些被称为处理指令。使用XmlDocument.CreateProcessingInstruction添加它们。

#2


7  

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

这些被称为处理指令。您可以使用XmlProcessingInstruction类在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.)

与XmlDocument中定义的大多数元素一样,您无法直接实例化它;您必须在XmlDocument上使用适当的工厂方法(在该特定情况下为CreateProcessingInstruction。)

#3


2  

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

感谢您解释这些是处理说明。使用CreateProcessingInstruction建议,这是解决方案:

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