Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In XML, what are the nodes with question marks called, and how do I add them in C#?

Tags:

c#

xml

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

like image 801
Stephen Oberauer Avatar asked Jul 26 '10 13:07

Stephen Oberauer


People also ask

How do I find nodes in XML?

To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string. Method XmlNode.

What is XML declaration tag?

XML declaration contains details that prepare an XML processor to parse the XML document. It is optional, but when used, it must appear in the first line of the XML document.

What is attribute and element in XML?

Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements. An XML attribute is always a name-value pair.

Which XML DOM object represents a node in the node tree?

Within the XML document structure, each circle in this illustration represents a node, which is called an XmlNode object. The XmlNode object is the basic object in the DOM tree.


Video Answer


2 Answers

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

like image 88
Peter Lillevold Avatar answered Sep 16 '22 19:09

Peter Lillevold


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

like image 29
Bryan Menard Avatar answered Sep 18 '22 19:09

Bryan Menard