1

I have a RichTextBox where the user can format the text and save as an xml document. I save the RichtextBox.Rtf content in my xml document. When i try to load the saved data from xml. The RichTextBox doesn't format the text based on the Rtf content from XML.

Sample Code used for creating XML

XmlTextWriter objXmlWriter = new XmlTextWriter(path, null);
objXmlTextWriter.Formatting = Formatting.Indented;
objXmlTextWriter.WriteStartDocument();    
objXmlTextWriter.WriteStartElement("MyData");

RichTextBox tBox = (RichTextBox)sender;
objXmlWriter.WriteStartElement("RcText");
objXmlWriter.WriteString(tBox.Rtf);
objXmlWriter.WriteEndElement();

objXmlWriter.WriteEndDocument();
objXmlWriter.Flush();
objXmlWriter.Close();    

Sample Code used for reloading the RichTextBox from XML

XmlDocument xDoc = new XmlDocument();
xDoc.Load(selPath);
XmlNodeList oDescendant = doc.SelectNodes("/MyData");
foreach (XmlNode xn in oDescendant)
{
    ID++;

    RichTextBox rcTxt = new RichTextBox();
    rcTxt.Name = "rtd" + ID;

    rcTxt.Rtf = xn["RcText"].InnerText;
}

Sample content as seen after loading the content to richtext from XML.

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\f0\fs17 Sample \b Text\b0\par
}

I am not sure where I am wrong. Please give your suggestions.

I tried searching for some similar cases but was not helpful. Some of the links which i referred : Code Project Link

4

0 回答 0