开发者

Newline char in exchange appointments

I need to put an adress into a appointment. The address is constructed out of several variables. Of course I also need some newlines. But "\n" doesnt result in an new line when i open the appointment in outlook.

Ok here is code snippet:

    string address = name + "\n" + strasse + "\n" + plz.ToString() + " " + ort;
        if ( telefon != "") {
            address = address + "\nTelefon:: " + telefon;
        }
        if ( natel != "") {
            address = address + "\nNatel: " + natel;
        }
        if ( mail != "") {
            address = address + "\nE-Mail: " +mail;
        }

Nothing special. The Pro开发者_如何学编程blem is when i write this to the body of an appointment, then there aren't any actual newlines.


Its pretty hard to diagnose this without seeing at least an example of the string you are passing, but one thing that I tend to do in my C# code is to use the constant:

Environment.NewLine

Or I use the StringBuilder class with the AppendLine() call to add a newline.

Edit: Based on your code snippet, I would write it this way (it will be more performant as well). With your snippet, lots of strings are being allocated (because strings are immutable). The recommended approach in this case is to use StringBuilder.

StringBuilder address = new StringBuilder();
address.AppendLine(name);
address.AppendLine(strasse);
address.Append(plz.ToString()); // This may not be neccessary depending on the type of plz, StringBuilder has overloads that will convert base types to string for you
address.Append(" ");
address.Append(ort);
if (!string.IsNullOrEmpty(telefon))
{
     address.AppendLine();
     address.Append("Telefon:: ");
     address.Append(telefon);
}
if (!string.IsNullOfEmpty(natel))
{
     address.AppendLine();
     address.Append("Natel: ");
     address.Append(natel);
}
if (!string.IsNullOrEmpty(mail))
{
     address.AppendLine();
     address.Append("E-Mail: ");
     address.Append(mail);
}

return address.ToString();

Note: If you are using .Net 4.0 you can use string.IsNullOrWhitespace instead of IsNullOrEmpty to check for not just an empty string, but one that contains only whitespace.

Edit 2 - Based on your answer of needing <br /> tags instead of newlines.

const string newLine = " <br /> ";
StringBuilder address = new StringBuilder();
address.Append(name);
address.Append(newLine);
address.Append(strasse);
address.Append(newLine);
address.Append(plz.ToString()); // This may not be neccessary depending on the type of plz, StringBuilder has overloads that will convert base types to string for you
address.Append(" ");
address.Append(ort);
if (!string.IsNullOrEmpty(telefon))
{
     address.Append(newLine);
     address.Append("Telefon:: ");
     address.Append(telefon);
}
if (!string.IsNullOfEmpty(natel))
{
     address.Append(newLine);
     address.Append("Natel: ");
     address.Append(natel);
}
if (!string.IsNullOrEmpty(mail))
{
     address.Append(newLine);
     address.Append("E-Mail: ");
     address.Append(mail);
}

return address.ToString();


Ok i got it now. I found out that appointments are stored in html format. So i tried to use the html entity for \r\n, .That didn't work. I finally solved the problem by using the br tag


While you're absolutely correct about using <br/>, newline is not the only thing Exchange eats in notes/appointment body.

I ended up with the following code:

Regex NewlineRegex = new Regex("(\r\n)|(\r)|(\n)");

string valueToWrite = NewlineRegex.Replace(
  SecurityElement.Escape(fieldValue), "<br/>")
.Replace(" ", "&nbsp;")
.Replace("&apos;", "'"); // &apos; is not in HTML.

And even after that you will read back an extra "\r\n" in the end of the body/notes, so I have to .TrimEnd() them after reading.


you should try "\r\n"


See http://www.infinitec.de/post/2009/08/25/Exchange-WebServices-Bug-with-Lineendings.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜