Search code examples
xmlnewlinexelement

How do I include line breaks when using an XElement?


I have a web service that accepts XML data. Included in the data is an <address> tag whose data could contain a new line.

How do I include the new line? I tried using the HTML line break tag <br />, but this was removed when accessing the XElement's Value property, leaving me with the address on a single line. I also tried using real lines breaks, but that caused problems parsing the XML.

Anyone any suggestions?


Solution

  • Simply use \n while adding content

    XElement XEleEmp = new XElement("Employee");
    XElement XEleAdd = new XElement("Address", "Plot No. 123 \nXYZ Villa \nRoad No. 321\n");
    XEleEmp.Add(XEleAdd);
    

    XML:

    <Employee>
      <Address>Plot No. 123
    XYZ Villa
    Road No. 321
    </Address>
    </Employee>