Tutoriaux
Certe, l'action d'indenter du XML dans un TextBox n'est pas chose courante! Par contre, il m'est arrivé d'avoir besoin de le faire. Si on ne l'indente pas, il s'avère assez difficile de s'y retrouver dans cet amoncellement de texte. Donc, si vous aussi par hazard vous auriez besoin d'effectuer un tel traitement, voici comment faire facilement sans le faire manuellement. Donc voici comment faire grâce à C#.
C#
private static string IndentXMLString(string xml)
{
string outXml = string.Empty;
MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.Unicode);
XmlDocument doc = new XmlDocument();
try
{
// Chargement de XML non formatté dans un XML Document.
doc.LoadXml(xml);
// Modifier la valeur de la propriété du formatage à ; Formatting.Indented
xtw.Formatting = Formatting.Indented;
// Écriture du XML dans le XmlTextWriter
doc.WriteContentTo(xtw);
// Vider le contenu du XmlTextWriter
xtw.Flush();
// Positioner la lecture sur la première ligne
ms.Seek(0, SeekOrigin.Begin);
// Transférer le contenur du fichier mémoire dans un StramReader
StreamReader sr = new StreamReader(ms);
// Retourner le fichier XML Formaté
return sr.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return string.Empty;
}
}
{
string outXml = string.Empty;
MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.Unicode);
XmlDocument doc = new XmlDocument();
try
{
// Chargement de XML non formatté dans un XML Document.
doc.LoadXml(xml);
// Modifier la valeur de la propriété du formatage à ; Formatting.Indented
xtw.Formatting = Formatting.Indented;
// Écriture du XML dans le XmlTextWriter
doc.WriteContentTo(xtw);
// Vider le contenu du XmlTextWriter
xtw.Flush();
// Positioner la lecture sur la première ligne
ms.Seek(0, SeekOrigin.Begin);
// Transférer le contenur du fichier mémoire dans un StramReader
StreamReader sr = new StreamReader(ms);
// Retourner le fichier XML Formaté
return sr.ReadToEnd();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return string.Empty;
}
}
Envoyer par mail
Vu: 722
Commentaires (0)

Ecrivez un commentaire
Navigation
Connexion
Publicités
MeilleursPrix.ca
|
|















