Get Formatted Text of Linked Document - c#

In order to get text of linked document and insert it to the documentation I used the element.GetLinkedDocument() method.
However, I got RTF characters within the text.
Therefore, I used the Repository.GetFormatFromField() method to return it to simple text.
What I would like now is to get the linked document text formatted just as it looks like from within EA- without RTF characters but formatted.
Any ideas?
string linkedDocumentText = Repository.GetFormatFromField("TXT", Repository.GetFieldFromFormat("RTF", element.GetLinkedDocument()));
Document.Instance().InsertText("\n" + linkedDocumentText + "\n", "Normal");

Related

How to replace a string within a larger string but exclude anything within img tags

I have created a system where i load out content from a Database field into a literal as content for an article. I have added the ability to pass a search text string via the URL to be highlighted on the page. So this is being done via doing a replace like so below...
articleTitle = articleTitle.Replace(searchString, "<span title=\"Searched Term Match\" class=\"SearchedTextTitle\">" + searchString + "</span>");
The issue i have encountered is my content is all HTML so it includes the html for images and so on and if the alt tags or image url's contain the search text term it is also being replaced by the replace method above. How can i exclude any of the content that is within HTML tags etc?
Thanks in advance for you help
You can either use IndexOf("") method to build substrings and perform the replaces on only the parts of the HTML you want to affect, or you can use regex replace, which will allow you to build more logic into your search.

how to used less than sign in xml document?

I am using C#.net where I required to used xml string,which needs to populate into xmldocument. It is loading fine,but when that string has special following values in one of the node then it is not working
sometime I have html tags with style and class. so how to load that string in xml document. so How to deal with in such cases?
here my string which produces an error
<restdata>
<listingAddress>
fsdfsdf dfdf <Not Specified=""> Argentina dsfsf</listingAddress>
<listingAddress>
xxk dfsdf 899993
</listingAddress>
</restdata>
in my case error may be because of <not Specified="".
also sometime there may be html tags.
so how this would be used generalized way so any data my it should work fine?
Generally if you need to use characters that are commonly reserved in XML, you can use their encoded HTML entities if you need to enter HTML data :
Use < for <
Use > for >
Use & for &
Use " for "
You can find a complete list of them here. If you need to programatically encode HTML cotent in C#, you can use the HttpUtility.HtmlEncode() method :
// Your original text
var input = "<a href='http://example-site.com'>This is a link</a>";
// This yields <a href='http://example-site.com'>This is a link</a>
var encoded = HttpUtility.HtmlEncode(input);

WPF New line not working when saving text in RTF from a TextBlock

I use a TextBlock in WPF to display formated text. The display works fine but when I try to save the content to a file in RTF format the new line characters are not used. I get a "one line text".
I use lines of code like this:
displayBuffer.Inlines.Add(new Run("Blabla \n")
{ TextDecorations = TextDecorations.Underline, Foreground = Brushes.Blue });
and save with:
FileStream fileStream = new FileStream(dlg.FileName, FileMode.Create);
TextRange range = new TextRange(displayBuffer.ContentStart, displayBuffer.ContentEnd);
range.Save(fileStream, DataFormats.Rtf);
I have tried \n \r\n and Environment.NewLine for the end of line without success.
Any idea what I am doing wrong?
EDIT:
Quite interesting idea to post process string to get right result (Stefano reference). I have looked at RTF format here http://latex2rtf.sourceforge.net/rtfspec_7.html#rtfspec_18 and \par is probably what I want but \line would be ok.
What is strange is that the RTF file generated by the Range.Save does not contain \line or \par at all ?
So may be I can replace \n by special char sequence as described in your reference and replace this character in the RTF file by \par.
However I would prefer to do this before saving the file. Is there a way to do an equivalent of range.Save(fileStream, DataFormats.Rtf) to save string in memory? Otherwise I will need to write a temp file process it and rewrite final one but all this looks ugly for something supposed to be simple?
The following code implements a method that takes a RichTextBox as an argument, and returns a string representing the plain text contents of the RichTextBox.
The method creates a new TextRange from the contents of the RichTextBox, using the ContentStart and ContentEnd to indicate the range of the contents to extract. ContentStart and ContentEnd properties each return a TextPointer, and are accessible on the underlying FlowDocument that represents the contents of the RichTextBox. TextRange provides a Text property, which returns the plain text portions of the TextRange as a string.
Try this:
string StringFromRichTextBox(RichTextBox rtb)
{
TextRange textRange = new TextRange(
// TextPointer to the start of content in the RichTextBox.
rtb.Document.ContentStart,
// TextPointer to the end of content in the RichTextBox.
rtb.Document.ContentEnd
);
// The Text property on a TextRange object returns a string
// representing the plain text content of the TextRange.
return textRange.Text;
}
To save in rtf format correctly try this:
using (FileStream file = new FileStream(fileLocation, FileMode.Create))
{
textrange.Save(file, System.Windows.DataFormats.Rtf);
}
Maybe the Unicode Line-Separator (U+2028) does work:
\u2028
In my opinion you should use a RichTextBox instead of a TextBlock (if you want to save the text in a rft file). Moreover insert a new LineBreak instead of a simple "\n".

Adding a carriage return after XML ending closing tag?

I am working with some XML-like text that comes from a vendor. The text looks likes this:
Notice that there is a carriage return/line feed after each element's closing tag.
We have a program that creates a new XML-like document, to send back to the vendor. It's basically just a text file. The problem is, though, our resulting document does not contain a carriage-return/line-feed after each closing tag.
We can't modify the program, but I figured we could write a small program that would read the text document in, add a CR/LF to the end of each closing tag, then write it back out, basically just modifying the text to look like the vendor's document.
My first attempt at doing this didn't work well. Here's the code I used:
// Add CR/LF to file.
var myFile = File.ReadAllText(_filePath);
myFile = myFile.Replace(">", ">" + Environment.NewLine);
File.WriteAllText(_filePath, myFile);
However, I forgot that doing the replace on the > character, will also do it for the starting element tag, too. So, I now have a CR/LF after the start and end tags:
So, basically, I'm wondering how I can just add the CR/LF after the ending closing tag?
I should also mention that the file that I'm trying to do this to is one long string of xml-like text. So, it looks like this:
<name>nextOver.gif</name><relativelink>images/nextOver.gif</relativelink><resourceflags>0</resourceflags>...
I just want to read the text file in, add a CR/LF after each closking tag, then write out the modified file.
EDIT: I just had a thought. Perhaps I can use RegEx to pick out each closing tag, based on whether the tag contains a / character then, somehow, add the CR/LF after...
You can use Regex :
string result = Regex.Replace(str, "</([^>]*)>", "</$1>" + Environment.NewLine);
If the text is a valid XML, try to read it to the XmlDocument and then write it back with the following XmlWriterSettings:
using (XmlWriter writer = XmlWriter.Create(filename, new XmlWriterSettings
{ Indent = true, IndentChars = String.Empty }))
{
xmlDocument.Save(writer);
}

How make xml to not auto replace ampersand characters in ASP.NET

There is a method wich takes the *.xml template made with Excel formatting and insert some text into it.
When I'm inserting text with \r\n symbols, Excel ignores the brakes and write all in one line. It turns out, that Excel needs the "&#10;" in xml instead of "\r\n". So i'm trying to replace
NewText = NewText.Replace("\r\n", "
").Replace("\n", "
");
node["Data"].InnerText = NewText;
But then I see, that all the "&#10;" are implicitly changed with "&amp ;#10;" by XmlDocument.
What should I do to save xml with "&#10;" in it?
Use CDATA
http://www.w3schools.com/xml/xml_cdata.asp

Categories

Resources