0

i generate xml by taking data from richtextbox in c# and generates the following xml file

xml file

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="task.xsl"?>
<Nodes><sNode><Word>this is a sample text this is a sample text this is a sample        text</Word></sNode></Nodes>

"the xslt that i have made for xml is as under" xslt example

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
<html>
  <body>
    <h2>Rich Text Box</h2>
    <table border="1">
      <tr bgcolor="#9acd32">
        <th>Data</th>
      </tr>
      <xsl:for-each select="Nodes/sNode">
        <tr>
          <td><xsl:value-of select="word"/></td>              
          </tr>
        </xsl:for-each>
       </table>
     </body>
   </html>
  </xsl:template>
</xsl:stylesheet>

Sample input data (I enter the following data in the same format in richtextbox)

this is a sample text
this is a sample text
this is a sample text

I want to show this data in explorer in the same format (in different lines). My code is as under

        using (XmlWriter writer = XmlWriter.Create("c:\\Task\\task.xml",settings))
        {

            //writer.WriteRaw("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
            writer.WriteRaw("<?xml-stylesheet type=\"text/xsl\" href=\"task.xsl\"?>");
            writer.WriteStartElement("Nodes");
            writer.WriteStartElement("sNode");
            writer.WriteElementString("Word", words);
            writer.WriteEndElement();
            writer.WriteEndDocument();
        }

where should i use <br/> tags. or what should i do to show the data in the same format. Thank you for any help

Tim C
  • 70,053
  • 14
  • 74
  • 93
Zeebok
  • 388
  • 1
  • 4
  • 15
  • 3
    you are aware of the fact, that XML is a plain-text format, and by that is usally displayed reader-friendly by a parser/displaying program. Programmaticly telling it to "new line" ( aka \n aka
    ) is ... strange
    – Najzero Sep 19 '13 at 12:30
  • no programmaticly.. .when i take the input as string STR = richTextBox1.Text.ToString(); it gaves me the input with \n if lines are in new lines, i mean in seperate lines... i store the different lines in a single node of xml. should i replace \n with
    if so then i already tried it. it didnt work. any ideas please.
    – Zeebok Sep 19 '13 at 12:45
  • on the site http://xslttest.appspot.com/ i have tried as (xml code)

    this is data provided as input for
    testing

    Hello, World!
    – Zeebok Sep 19 '13 at 12:48
  • xslt provided as ---------------------

    from
    – Zeebok Sep 19 '13 at 12:48
  • and found the html as-------------------------

    Hello, World!

    from this is data provided as input for testing
    – Zeebok Sep 19 '13 at 12:49
  • then you need to split your string depending on your line returns (http://stackoverflow.com/questions/1547476/easiest-way-to-split-a-string-on-newlines-in-net ) given from the richTextBox. Then your XML should have a structure like this `Text of Line 1Text of...` – Najzero Sep 19 '13 at 12:50
  • you see the br tag is automatically ommitted. . . what should i do to do the task. . .hope the above senerio will be much helpful to you – Zeebok Sep 19 '13 at 12:51

2 Answers2

0

I prefer use se XmlTextWriter instead of using XmlWriter, cause I don't have to worry about escaping anything since XmlTextWriter escapes the characters where needed, but those class are deprecated, Another way is to explicit use CDATA

Example with CDATA

using (XmlWriter writer = XmlWriter.Create("c:\\Task\\task.xml",settings))
{
    //writer.WriteRaw("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
    writer.WriteRaw("<?xml-stylesheet type=\"text/xsl\" href=\"task.xsl\"?>");
    writer.WriteStartElement("Nodes");
    writer.WriteStartElement("sNode");
    writer.WriteStartElement("Work");
    writer.WriteCData("words");
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndDocument();
}

Example with XmlTextWriter

string words = "<node>it's my \"node\" & i like it<node><br/>test";
using (XmlTextWriter xtw = new XmlTextWriter(@"c:\xmlTest.xml", Encoding.Unicode))
{
    writer.WriteStartElement("Nodes");
    writer.WriteStartElement("sNode");
    writer.WriteElementString("Word", words);
    writer.WriteEndElement();
    writer.WriteEndElement();
    writer.WriteEndDocument();
}

If you don't want to user XmlTextWriter you can use

Emanuele
  • 469
  • 2
  • 10
  • -1: you should not use `new XmlTextReader()` or `new XmlTextWriter()`. They have been deprecated since .NET 2.0. Use `XmlReader.Create()` or `XmlWriter.Create()` instead. – John Saunders Sep 19 '13 at 12:43
  • I see no change in your answer. – John Saunders Sep 19 '13 at 12:48
  • Only your code doesn't compile. Also, the OP had no problem with escaping. Please read the question carefully (I know it's not very clear). – John Saunders Sep 19 '13 at 12:53
  • sorry about the confusion, i'm a newbie @stackoverflow, I've readed again, and changed the code again, maybe the code won't compile, i've provided only for hint – Emanuele Sep 19 '13 at 13:01
  • i am sorry i am also newbie. . . thanks to all of you for your consideration . . . i am taking input in richtextbox control as ----string STR = richTextBox1.Text.ToString(); string words = STR.Replace("\n", "
    "); the input provided will be in multiple lines as by pressing enter. i have used explorer control in my application to show the user inputed data in the same formatting(only new line not other formattings like bold or color etc)
    – Zeebok Sep 19 '13 at 14:44
  • the above given ans produce the xml as ----<![CDATA[11
    22
    22]]>
    ---------------while result shown on explorer control is as 11
    22
    33 instead of 11 in seperate line 22 in another and 33 is also in another new line. . . @saunders hope now it may clear
    – Zeebok Sep 19 '13 at 14:44
  • the output produced is correct,
    is correctly escaped, if you want produce newline instead of usign
    it's another problem. you should replace "
    " with "\r\n" or delegate the representation of the new line to a html formatter....
    – Emanuele Sep 19 '13 at 15:03
  • try to writer.WriteCData(words.replace("
    ","\r\n"); works but it's not what is called "clean code", the problem is that you're showing html inside the xml directly through IE, btw you have to manipulate your xml manually
    – Emanuele Sep 19 '13 at 15:06
  • by using string STR = richTextBox1.Text.ToString(); i get a string 11\n22\n33\n so i think there is no need of writer.WriteCData(words.replace("
    ","\r\n");-------------in most clear words i have string or a single node of xml having data in a single line having \r\n or
    . . . . the problem is how to format the output using this information either using in xml or something else. . . xml node may be as this is line1
    this is line2
    so how can i format my output as both lines in seperate lines.If there sould be a change in xsl file thn wht should be it.please any idea.
    – Zeebok Sep 19 '13 at 15:22
  • i found something on web like this for xsl file i dont know it will be helpful for me. . .and how can i use it. . .seniors please help – Zeebok Sep 19 '13 at 15:42
0

If you want a very crude way of doing this, just wrap the text you are outputing in a pre element

<td>
    <pre><xsl:value-of select="Word"/></pre>
</td>

(As an aside, this is probably just a typo in your question, but in your XML you have a word element, but in the XSLT you are looking for a Word element. As XML is case-sensitive, these are not the same).

EDIT: If you really want replace line-feeds with br elements, then to do this in XSLT you will need a recursive template. There are probably many examples here on StackOverflow, but I plumped for this one

xslt 1.0 string replace function

However, this will need a tweak because it replaces text with text, when you want to replace text with a newly created element.

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
      <html>
         <body>
            <h2>Rich Text Box</h2>
            <table border="1">
               <tr bgcolor="#9acd32">
                  <th>Data</th>
               </tr>
               <xsl:for-each select="Nodes/sNode">
                  <tr>
                     <td>
                        <xsl:call-template name="replace-string-with-element">
                           <xsl:with-param name="text" select="Word"/>
                           <xsl:with-param name="replace" select="'&#10;'"/>
                           <xsl:with-param name="with" select="'br'"/>
                        </xsl:call-template>
                     </td>
                  </tr>
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>

   <xsl:template name="replace-string-with-element">
      <xsl:param name="text"/>
      <xsl:param name="replace"/>
      <xsl:param name="with"/>
      <xsl:choose>
         <xsl:when test="contains($text,$replace)">
            <xsl:value-of select="substring-before($text,$replace)"/>
            <xsl:element name="{$with}"/>
            <xsl:call-template name="replace-string-with-element">
               <xsl:with-param name="text" select="substring-after($text,$replace)"/>
               <xsl:with-param name="replace" select="$replace"/>
               <xsl:with-param name="with" select="$with"/>
            </xsl:call-template>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$text"/>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>

When applied to the following XML

<Nodes><sNode>
<Word>this is a sample text 
this is a sample text
this is a sample text
</Word>
</sNode>
</Nodes>

The following is output

<html>
<body>
<h2>Rich Text Box</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Data</th>
</tr>
<tr>
<td>this is a sample text <br>    this is a sample text<br>    this is a sample text<br>    </td>
</tr>
</table>
</body>
</html>
Community
  • 1
  • 1
Tim C
  • 70,053
  • 14
  • 74
  • 93
  • thankyou dear tim your comment (As an aside, this is probably just a typo in your question, but in your XML you have a word element, but in the XSLT you are looking for a Word element. As XML is case-sensitive, these are not the same) solved also a big problem that was also undetected bu my seniors. . . – Zeebok Sep 19 '13 at 15:29
  • @user2787182 I've amended my question to show how you can actually replace the line-feeds with `
    ` if required
    – Tim C Sep 19 '13 at 19:12
  • thankyou dear tim. . you are so kind and awsome and this answer is tremendiously wonderful. . . please accept many thanks form my side with the deep of my heart. :) – Zeebok Sep 20 '13 at 11:09
  • i need little bit more help.the above xsl is replacing with br tag. if i want multiple replacements of characters then is it possible. for example i want to preserve spaces too. so will be replaced by &nbsp. How can i do that. for example--- "this is sample line". when the output is show all blank spaces are omitted. while i want to preserve them to show again back the entered data in its original format. Thank you. – Zeebok Sep 20 '13 at 11:33
  • It's probably possible. If you just want to replace one character with another then you could use the `translate` function, for example. It would be best to ask a whole new question though, as this particular question was just about the `
    ` elements. Thanks!
    – Tim C Sep 20 '13 at 12:41