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 " " 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 " " are implicitly changed with "& ;#10;" by XmlDocument.
What should I do to save xml with " " in it?
Use CDATA
http://www.w3schools.com/xml/xml_cdata.asp
Related
I want to make use of Regex.Replace function to replace data in the format
05-11
to
"05-11"
so that excel can read it as a string.
Excel is converting the data to 05-Nov even though that particular column is defined as char.
In my application code, I have the below piece of code to replace any data that starts with a dash (-) with double quotes, "data"
var newString = Regex.Replace(data, #"^(-.*)$", "=\"$0\"");
How can I make use of this function to replace any data which are like
'05-11', '15-2019'
with
"05-11", "15-2019"
for the excel to read them as a string not as date format.
Unfortunately Excel does not accept " as an indicator of a text column, the only way to be sure is to proceed the value with a single quote.
var newstring = Regex.Replace(data, #"\b""?(\d\d-(?:\d\d)?\d\d)""?\b", "\"=\"\"$0\"\"\"");
This finds possibly double-quoted date strings that constitute the whole column value, and outputs it quoted with an equals sign and double quotes.
Unfortunately this special formatting is lost if you save as CSV from inside Excel and try to reload.
See this question for details.
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");
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);
}
I have been trying to write html content into Excel spreadsheet's cells using ExcelPackage OpenOfficeXML and c#.
I am getting errors stating that input string has an invalid token. Has anyone came across anything similar?
Saving html directly in Excel works OK.
I do not want to use html encoding as the content has to be in readable form.
Without knowing more of the specifics:
If you're using XML to create your spreadsheet, you should use CDATA tags for HTML content
<someEntry> <![CDATA[ YOUR HTML HERE ]]> </someEntry>
In c# you can add CDATA. It is a type of element.
XmlCDataSection CData;
CData = doc.CreateCDataSection("<someNodeName><h1>blah</h1></someNodeName>");
XmlElement root = doc.DocumentElement;
root.AppendChild(CData);
Otherwise, you probably need to escape quotes and double quotes \" \'
Or you can use System.Security.SecurityElement.Escape() to encode both single and double quotes.
Escaping single quotes solved the problem - Replace("'", #"""");
Thank you all.
I am trying to generate a xml document using LinqXml, which has the "\n" to be "& #10;" in the XElement value, no matter whatever settings I try with the XmlWriter, it still emits a "\n" in the final xml document.
I did try the following, Extended the XmlWriter.
Overrided the XmlWriterSettings changed the NewLine Handling.
Both of the options didnt work out for me.
Any help/pointers will be appriciated.
Regards
Stephen
LINQ to XML works on top of XmlReader/XmlWriter. The XmlReader is an implementation of the XML processor/parser as described in the XML spec. That spec basically says that the parser needs to hide the actual representation in the text from the application above. Meaning that both \n and
should be reported as the same thing. That's what it does.
XmlWriter is the same thing backwards. It's purpose is to save the input in such a way, that when parsed you will get exactly the same thing back.
So writing a text value "\n" will write it such that the parser will report back "\n" (in this case the output text is \n for text node, but
for attribute due to normalization which occurs in attribute values).
Following that idea trying to write a text value "
" will actually write out "
" because when the reader parses that it will get back the original "
".
LINQ to XML uses XmlWriter to save the tree to an XML file. So you will get the above behavior.
You could write the tree into the XmlWriter yourself (or part of it) in which case you get more control. In particular it will allow you to use the XmlWriter.WriteCharEntity method which forces the writer to output the specified character as a character entity, that is in the $#xXX; format. (Note that it will use the hex format, not the decimal).
What is the reason for having the escaped value for '\n' in the XML element? The newline character is valid inside an XML element and when you parse the XML again, it will be parsed as you expect.
What you're looking for would happen if the newline character is placed within the value of an XML attribute:
XElement xEl = new XElement("Root",
new XAttribute("Value",
"Hello," + Environment.NewLine + "World!"));
Console.WriteLine(xEl);
Output:
<Root Value="Hello,
World!" />