XElement xml = new XElement("user",
new XElement("Authenticated","Yes"))
);
xml.Save(savePath);
Il fonctionne pour .net 3 et au-dessus, mais Vous pouvez utiliser XmlDocument pour versions ultérieures
XmlDocument xmlDoc = new XmlDocument();
// Write down the XML declaration
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
// Create the root element
XmlElement rootNode = xmlDoc.CreateElement("user");
xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
xmlDoc.AppendChild(rootNode);
// Create the required nodes
XmlElement mainNode = xmlDoc.CreateElement("Authenticated");
XmlText yesText= xmlDoc.CreateTextNode("Yes");
mainNode.AppendChild(yesText);
rootNode.AppendChild(mainNode);
xmlDoc.Save(savePath);
Vous pouvez également utiliser XmlWriter comme le suggère @marc_s ou au moins vous pouvez stocker XML dans le fichier comme piqûre
using(StreamWriter sw = new StreamWriter(savePath))
{
sw.Write(string.Format("<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<user><Authenticated>{0}</Authenticated></user>","Yes"));
}
merci marc_s. cela a fonctionné très bien – happysmile