Xml to Text Convert
15,745
Solution 1
Something like this:
XmlDocument doc = new XmlDocument();
doc.LoadXml(your text string);
StringBuilder sb = new StringBuilder();
foreach (XmlNode node in doc.DocumentElement.ChildNodes)
{
sb.Append(char.ToUpper(node.Name[0]));
sb.Append(node.Name.Substring(1));
sb.Append(' ');
sb.AppendLine(node.InnerText);
}
Console.WriteLine(sb);
Solution 2
The following XSLT script will do what you want:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="*">
<xsl:for-each select="*">
<xsl:value-of select="local-name()"/>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Then you can apply the script using the XslCompiledTransform
class as such:
private string transformXml(string sourceXmlText, string xsltText)
{
XmlDocument sourceXmlDocument = new XmlDocument();
sourceXmlDocument.LoadXml(sourceXmlText);
XslCompiledTransform transformer = new XslCompiledTransform();
XmlTextReader xsltReader = new XmlTextReader(new StringReader(xsltText));
transformer.Load(xsltReader);
MemoryStream outputStream = new MemoryStream();
XmlWriter xmlWriter = XmlWriter.Create(outputStream, transformer.OutputSettings);
transformer.Transform(sourceXmlDocument, null, xmlWriter);
outputStream.Position = 0;
StreamReader streamReader = new StreamReader(outputStream);
return streamReader.ReadToEnd();
}
It's obviously more complex than the other solutions people have posted, but it has the major advantage of being able to easily change the script if you need to change the formatting.
Solution 3
You can do something like
string xml = @"<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>";
StringBuilder sb = new StringBuilder();
foreach (XElement element in XDocument.Parse(XML-STRING).Descendants("note"))
{
sb.AppendLine(string.Format("{0} {1}", element.Name, element.Value));
}

Author by
Pomster
Updated on June 10, 2022Comments
-
Pomster almost 2 years
I would like to write something in C# that takes Xml and converts it to plain text.
<note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
Would become:
To Tove From Jani Heading Reminder Body don't forget me this weekend!
Is there any thing already like this? and how would i go about doing this?
This is just ruffly the idea im going for still needs lots of work:
private void dataGridViewResult_SelectionChanged(object sender, EventArgs e) { if (this.dataGridViewResult.SelectedRows.Count > 0) { XslCompiledTransform xslt = new XslCompiledTransform(); xslt.Load("SQL"); //.xml xslt.Transform("SQL","SQL"); //.xml, .html this.richTextBoxSQL.Text = this.dataGridViewResult.SelectedRows[0].Cells["SQL"].Value.ToString(); } }
-
Pomster almost 12 years+1 Yes this does look Great, just looking at this part: "transformer.Load(xsltFilePath);" , Calling Stored procedures out the database and then want to convert them.
-
Steven Doggart almost 12 years@Pomster That would be the path to the XSLT script file. If you don't want to go to a file to get the script, you could give it an XmlReader that reads the script from memory, instead.
-
Pomster almost 12 yearsYes that sounds better, so XmlReader i can pass Xml to, then convert it?
-
Steven Doggart almost 12 years@Pomster I updated my example to show how it can all be done in memory using strings.
-
Joe Healy almost 5 yearsnice simple solution. created a full console app around it at github.com/jhealy/XmlToText if anyone is looking for the 'easy' button.