2009-10-09 10 views
2

Je dois créer un fichier xml iciComment générer un fichier XML

bool result= false; 

Comment y parvenir dans ASP.NET avec la syntaxe C#. result est la valeur que j'ai besoin d'ajouter dans le fichier XML.

Je dois créer un fichier XML dans un dossier avec un contenu comme celui-ci

<?xml version="1.0" encoding="utf-8" ?> 
<user> 
    <Authenticated>yes</Authenticated> 
</user> 

merci

Répondre

2

Que diriez-vous ceci:

XmlTextWriter xtw = new XmlTextWriter(@"yourfilename.xml", Encoding.UTF8); 

xtw.WriteProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""); 
xtw.WriteStartElement("user"); 
xtw.WriteStartElement("Authenticated"); 
xtw.WriteValue(result); 
xtw.WriteEndElement(); // Authenticated 
xtw.WriteEndElement(); // user 

xtw.Flush(); 
xtw.Close(); 

Ou si vous préférez construire votre fichier XML en mémoire, vous pouvez également utiliser la classe XmlDocument et ses méthodes :

// Create XmlDocument and add processing instruction 
XmlDocument xdoc = new XmlDocument(); 
xdoc.AppendChild(xdoc.CreateProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\"")); 

// generate <user> element 
XmlElement userElement = xdoc.CreateElement("user"); 

// create <Authenticated> subelement and set it's InnerText to the result value   
XmlElement authElement = xdoc.CreateElement("Authenticated"); 
authElement.InnerText = result.ToString(); 

// add the <Authenticated> node as a child to the <user> node 
userElement.AppendChild(authElement); 

// add the <user> node to the XmlDocument 
xdoc.AppendChild(userElement); 

// save to file 
xdoc.Save(@"C:\yourtargetfile.xml"); 

devrait fonctionner sur toute version du framework .NET, si vous avez une clause using System.Xml; en haut de votre fichier.

Marc

+0

merci marc_s. cela a fonctionné très bien – happysmile

3
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")); 
} 
+0

je ne trouve pas la propriété de nom comme il est XElement – happysmile

+0

fonctionne pour .NET 3.5 et au-dessus –

1

Si vous souhaitez générer le XML et donner à l'utilisateur un choix pour enregistrer le XML dans leur poste de travail, consultez le poste ci-dessous. Il explique ce processus en détail.

Generating XML in Memory Stream and download

+0

Fournir un exemple de code garantirait que votre message sera utile aux futurs utilisateurs. ;) – vdbuilder

Questions connexes