2015-11-15 7 views
0

Je suis en train d'écrire un XML en utilisant XmlDocument, j'ai besoin d'un élément ou un attribut comme illustré ci-dessouscomment ajouter instruction de traitement en XML en utilisant XmlDocument

L'élément ou attribut requis est <?Validversion="1" ?>

comment créer en utilisant XmlDocument ou xmlwriter.

 // to create <?Validversion="1" ?> 
     XmlDocument aDoc = new XmlDocument(); 
     aDoc.CreateXmlDeclaration("1.0", "utf-16", null); 
     XmlCDataSection aDataSec =aDoc.CreateCDataSection("?Version = 2"); 
     aDoc.AppendChild(aDataSec); 
     aDoc.Save("c:\\vector.xml"); 
+0

À quoi ressemble le fichier XML que vous voulez générer? –

Répondre

2

Vous recherchez XmlDocument.CreateProcessingInstruction et non section CDATA:

var document = new XmlDocument(); 
document.AppendChild(document.CreateXmlDeclaration("1.0", "utf-16", null)); 
var piNode = document.CreateProcessingInstruction("Version", "=\"2\""); 
document.AppendChild(pi); 

Side note: ne pas oublier de AppendChild noeud nouvellement créé.

+0

merci beaucoup – Racs