How to add mixed Text and XElements on XElement value - c#

I am trying to add as value to an XElement mixed text and inline elements.
For example when setting the string "this is a mixed text <foo>and</foo> inline element." the XElement.Nodes to be able to return the text node as XmlNodeType.Text & the element as XmlNodeType.Element.
Thanks in advance.

Use e.g. new XElement("parent", "this is a mixed text ", new XElement("foo", "and"), " inline element.") respectively element.Add("this is a mixed text ", new XElement("foo", "and"), " inline element.").
If you have a plain string then use e.g.
element.Add(XElement.Parse("<root>" + "this is a mixed text <foo>and</foo> inline element." + "</root>").Nodes());

Related

String formatting for rich text box with constant and dynamic text

Currently I am attempting to create a dictionary which maps a selected item form a list view with a corresponding string output in a rich text box. I would like to bold specific text in the string, which will always be the same (constant) and also adding dynamic text to the string that would change.
Something like this:
ID: 8494903282
Where ID is the constant text I need bolded and the numbers would be a dynamic ID that changes. I will need to have multiple lines with different data in this format which will be changing:
ID: 8494903282
Name: Some Name
Date: 3/15/2018
Currently I have a rich text box to output to and I am trying to use some string formatting to do what I want but this is not working correctly. Essentially I need a string value I can store in a dictionary so when an item gets selected I can just set the rtf property of the text box to the value of that dictionary item.
Below I have my current format string I am attempting to set the rtf property to:
string s1 = string.Format(#"{{\rtf1\ansi \b Commit ID: \b0 {0}\line}}", entry.ID);
string s2 = string.Format(#"{{\b Author: \b0 {0}\line}}", entry.Author);
string s3 = string.Format(#"{{\b Date: \b0 {0}\line}}", entry.Date.ToString("d"));
string s4 = Environment.NewLine + Environment.NewLine + entry.Message;
contents = (s1 + s2 + s3 + s4);
Then setting the rtf property of my rich text box:
LogContentsTB.Rtf = Logs[LogNamesLV.SelectedItems[0].Name];
Where logs is a dictionary of the form < string, string > that holds the format string for the specific item.
However, I get the following output rather than my expected output:
This is the correct form of output for the first item but nothing else appears. If there are any other ways to do this I am open to suggestion.
After doing some light reading on the rtf syntax I noticed that I was trying to close off each string with curly braces. Curly braces are used for RTF groups. For some reason the rich text box in windows forms did not play well with that.
Another thing to notice is that the string.format method was probably the main culprit for cause of issues with this type of formatting. In my answer I do not use it but rather just add the string directly into the rtf formatted string i.e. < format >< variable string >< ending format >
If you look at NetMage's response, you will notice he only puts an opening brace on the very first string, s1. This is to group the whole string. But we need to add a closing brace on the final string, s4, to finish the grouping. Below is the final code and screenshot that worked for my application.
string s1 = #"{\rtf1\ansi\b ID: \b0 " + entry.ID + #" \line\line";
string s2 = #"\b Author: \b0 " + entry.Author + #" \line\line";
string s3 = #"\b Date: \b0 " + entry.Date.ToString("d") + #" \line\line ";
string s4 = entry.Message + #"}";
contents = s1 + s2 + s3 + s4;
Thanks for pointing me in the right direction!
I think your RTF formatting is wrong. You could try:
string s1 = string.Format(#"{{\rtf1\ansi\r\b Commit ID:\b0 {0}\line\r", entry.ID);
string s2 = string.Format(#"\b Author: \b0 {0}\line\r", entry.Author);
string s3 = string.Format(#"\b Date: \b0 {0}\line\r", entry.Date.ToString("d"));
string s4 = Environment.NewLine + Environment.NewLine + entry.Message + "}}";
contents = (s1 + s2 + s3 + s4);

Regex to replace " with " only if it's not in another "

I'm converting an encoded XML document to its original format
string myXml = oldXml.Replace("<", "<").Replace("&", "&")
.Replace(">", ">")
.Replace(""", "\"")
.Replace("&apos;", "'");
It works fine. However I want to exclude " if its in another ".
Example:
Original XML
//Note the title value
<v:shape id="_x0000_i1025" title="a" title "b"> </v:shape>
Encoded XML
<v:shape id="_x0000_i1025" title="a" title "b"> </v:shape>
Recovered XML after replace
//Note the title value
<v:shape id="_x0000_i1025" title="a" title "b"> </v:shape>
As you can see the inside " shouldn't be convert to ". So how can I do the replace with Regex so that it doesn't replace the inside "
Thank you
Turned out I don't need to recover the encoded XML.
Since nodElement.SetAttribute("myXmlAttribute", myXml); encodes the original Xml at the first place, when I read the attribute value and assign it to a string, C# will recover the original Xml automatically.
string content = theNode.Attributes["myXmlAttribute"].Value;
I don't need to do any replacing.

How to keep xml from converting /r/n into

I have here a small code:
string attributeValue = "Hello" + Environment.NewLine + " Hello 2";
XElement element = new XElement("test");
XElement subElement = new XElement("subTest");
XAttribute attribute = new XAttribute("key", "Hello");
XAttribute attribute2 = new XAttribute("key2", attributeValue);
subElement.Add(attribute);
subElement.Add(attribute2);
element.Add(subElement);
Console.Write(element.ToString());
Console.ReadLine();
I have an issue, basically the /r/n or the new line is converted in
in attribute, but I dont want to have it, I want to keep it /r/n as when I use this XML with the Microsoft Word documents template, the new lines are not implemented, although it is multilined text, in word document I only get the spaces. But no new lines :/
Anyone has any idea?
Although i've set the allow multi line int he property of the field in the template.
Actually the behaviour you get with
is the same than the one of Environment.NewLine. You can do a simple test to confirm this (add two TextBoxes to your Form with the Multiline property set to True: textBox1 and textBox2):
textBox1.Text = element.ToString(); //
string text = element.ToString().Replace("
", Environment.NewLine);
textBox2.Text = text; ///r/n
On the other hand, if you want to avoid the
part anyway (for example: because of wanting to output the given string to an external program not working on .NET), you can just rely on the aforementioned Replace after dealing with XElement and new lines.

Replacing newline in multiline textbox with <br/>

I am trying to use a multiline textbox to insert text into a table which is displayed as html. I want to with javascript take the text inside of the textbox and find where the user pressed enter and place "<br/>" in that position so when the text is displayed it will show line break. Any ideas on how I could do this?
I tried something like this but it did not work.
var text = document.getElementById("announcementid").value;
var newtext = text.replace("\n", "<br/>");
text = newtext;
The newtext variable ends up being a copy of the original string from your announcementid element. Thus, you'll need to re-set the value property on the original document element:
var text = document.getElementById("announcementid").value;
var newtext = text.replace(/\n/g, "<br />");
document.getElementById("announcementid").value = newtext;
Also, as Konstantin pointed out, the replace() function in Javascript will just replace the first instance unless you pass in a global regular expression.
Fiddle example
Text is a primitive so it would not replace the value. Use a regular expression to globally replace instead of replacing just the first instance:
var text = document.getElementById("announcementid").value;
var newtext = text.replace(/\n/g, "<br />");
document.getElementById("announcementid").value = newtext;

c# Rich text Format error in code

hoping you can help
I have the following code
List<string> comconfig = populate.resolveconfig(_varibledic, populate.GetVaribles[0].Substring(populate.GetVaribles[0].IndexOf("=") + 1)); //get the aray of strings
string config = ""; //create a empty otput string
config = #"\rtf1\ansi\deff0\deftab240 {\fonttbl {\f000 Monaco;} {\f001 Monaco;} } {\colortbl \red255\green255\blue255; \red000\green000\blue000; \red255\green255\blue255; \red000\green000\blue000; }";
config = config + #"\f96\fs20\cb3\cf2 \highlight1\cf0 "; // assigned rtf header to output string
foreach (var strings in comconfig) //loop though array adding to output string
{
config = config + strings + #"\par ";
}
config = config + "}"; //close of RTF code
So trying to create a RTF string that I can later display. comconfig is an array of strings with some RTF mark up for highlighting and stuff.
trouble is that if I use # then I get double \ which mess up the RTF, and if i dont use them, then the escape charatures mess up the code??
what is the best way to build up this string by adding a preformated RTF header and the aray of strings in the middle. it is displayed finaly in a RTF.textbox. or converted to a plain text string at the users request. I need to ignore the escape charatures with out messing up the RTF?
Cheers
Aaron
No, you don't get a double \. You're getting confuzzled by the debugger display of the string. It shows you what the string looks like if you had written it in C# without the #. Click the spy glass icon at the far right and select the Text Visualizer.

Categories