web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Line break in XML file created from X++ code

(0) ShareShare
ReportReport
Posted on by 11,633

Hi,

I am creating an XML log file from X++ code as a result of processing another XML file. I am using '\n' to insert line breaks for the text I have under <Description></Description> tag. The file is showing line breaks when I open it in Notepad, but when open in the browser, it is not showing line breaks and all text showing on a single line. 

I tried to use CData with <br/> tags, but no success. I am using the same text to fill one of the placeholder in email template and sending email using that template. The text in the email also requires to show the line breaks.

Does anyone have any idea how to achieve the above requirements?

Thanks,

Baber.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    236,513 Most Valuable Professional on at
    RE: Line break in XML file created from X++ code

    If you see it in notepad, you have a proof that your file is all right - nothing to fix there.

    That HTML suppress white-spaces as line breaks is a different topic.

    If you require line break in HTML, you need a different way for converting XML to HTML (to convert line break to <br /> tags in HTML).

  • syed baber Profile Picture
    11,633 on at
    RE: Line break in XML file created from X++ code

    Thanks Martin for your quick response. I did try to show line breaks in HTML when open in browser, but didn't get the desired results. Like the following HTML not showing line breaks when open in IE and Chrome:

    <Status><Source>ErrorUsage.xml</Source><DateTime>2016-10-07T14:06:03</DateTime><StatusCode>1</StatusCode><Description>

    <![CDATA[first line<br/>second line<br/>]]>

    </Description></Status>

    Any idea why it is not working as expected ?

  • Martin Dráb Profile Picture
    236,513 Most Valuable Professional on at
    RE: Line break in XML file created from X++ code

    You're trying to mix XML and HTML, which doesn't make good sense.

    This works fine for me in Edge and IE:

    <?xml version="1.0" encoding="UTF-8"?>
    <Status>
       <Source>ErrorUsage.xml</Source>
       <DateTime>2016-10-07T14:06:03</DateTime>
       <StatusCode>1</StatusCode>
       <Description><![CDATA[first line
    second line]]></Description>
    </Status>
    

    But that wasn't my point. I meant that if you don't like the way how the browser converts XML to HTML, you should convert it by yourself (in anyway to you like) and let the browser simply show the HTML code you've built.

  • syed baber Profile Picture
    11,633 on at
    RE: Line break in XML file created from X++ code

    Hi Martin,

    Actually there is an existing code which creates an XML file by specifying these tags, Status, Source, DateTime, StatusCode and Description. The description contains some details as a result of processing another XML file. There can be multiple errors need to be shown under Description tag, and all errors requires to be shown on separate line as shown below:

    <Status><Source>MultiErrorUsage.xml</Source><DateTime>2016-10-07T15:17:37</DateTime><StatusCode>1</StatusCode><Description>

    ERROR:  1st error message.

    ERROR:  2nd error message.

    ERROR:  3rd error message.

    </Description></Status>

    I tried to use CData around these error text with <br/> tags, but the errors are not showing on separate line when open in browser. I want these to be shown on separate line in browser, as the users will most probably open these files in browser and not notepad. Hope this makes sense.

    Please note that I am creating this XML file from X++ code by concatenating error messages and showing them under Description tag.

    Thanks,

    Baber.

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: Line break in XML file created from X++ code

    You can use XmlTextWriter class to specify some standard XML formatting settings, including how to treat the line feed characters. In your case, the trick is to use the custom writer to enable XML indentation and use XmlWriterSettings.newLineHandling method to keep the line breaks.

    Please see the sample code below. It generates a sample XML document (I used the same values as in your example) and then configure XmlTextWriter, so XmlDocument will save the file with proper indentation and preserved line breaks:

    // Created on 10 Oct 2016 by Jovan Bulajic
    static void Test_XML(Args _args)
    {
        XmlDocument         doc;
        XmlNode             node;
        XmlTextWriter       writer;
        XmlWriterSettings   writerSettings;
    
        // Helper method to quickly add some XML
        void addElement(str _name, str _value)
        {
            XmlNode element;
            ;
            element = doc.createElement(_name);
            element.text(_value);
            node.appendChild(element);
        }
        ;
        // Initialize XML
        doc = XmlDocument::newBlank();
        node = doc.createElement("Status");
        doc.appendChild(node);
    
        // Some sample data ...
        addElement("Source", "MultiErrorUsage.xml");
        addElement("DateTime", "2016-10-07T15:17:37");
        addElement("Description", "\nERROR:  1st error message.\nERROR:  2nd error message.\nERROR:  3rd error message.\n");
    
        // Configure the XML writer
        writerSettings = new XmlWriterSettings();
        writerSettings.newLineHandling(XmlNewLineHandling::Replace);
        writerSettings.indent(true);
    
        // Save the XML to your file - change the actual file name
        writer = XmlWriter::newFile("MyFile.xml", writerSettings);
        doc.writeTo(writer);
        writer.flush();
        writer.close();
    }
  • syed baber Profile Picture
    11,633 on at
    RE: Line break in XML file created from X++ code

    Thanks Jovan for your response. I tried to create an XML file in a job and getting the desired output when open it from Notepad, but from IE, getting below error :

    The XML page cannot be displayed

    Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.

    --------------------------------------------------------------------------------

    Switch from current encoding to specified encoding not supported. Error processing resource

    <?xml version="1.0" encoding="utf-8"?>

    When I removed the above line from XML, it opens in browser without any error, but in that case all errors are on the same line.

    Any idea how to resolve this  ?

    Thanks,

    Baber.

  • Martin Dráb Profile Picture
    236,513 Most Valuable Professional on at
    RE: Line break in XML file created from X++ code

    Did you try the code I gave you? It works all right for me in Edge and IE.

    It doesn't work in some other browsers, because it's up to each browsers how it displays XML. If you want a particular look, don't depend on that - instead, generate HTML from your XML and include you <br /> elements there. XML is all about data, not the look.

    You should also consider changing the structure of your XML. Having an individual element for each message probably makes better sense.

    @Jovan: He said that line breaks are correct in the XML file; his problem is only about displaying it in browsers.

  • syed baber Profile Picture
    11,633 on at
    RE: Line break in XML file created from X++ code

    Hi Martin,

    I tried the format you gave me. In IE it is not showing anything, but in Chrome I am getting below results:

    XML-file-error.jpg

    Let me tell you some more details. In existing code, the XML file is created using TextBuffer class by calling textBuffer.ToFile method. The contents of the file are specified as XML tags and under Description tag, I have a requirement to display each error on a separate line. The XML format you gave me, I already tried it by specifying CData and <br/> in my code, but it didn't work as expected.

    Am I missing something ?

  • Martin Dráb Profile Picture
    236,513 Most Valuable Professional on at
    RE: Line break in XML file created from X++ code

    I copied your code and simply removed your <br /> tags and added appropriate line breaks. If it stopped working, something must have failed when you copied the XML from the forum. You can easily repeat my steps by yourself.

    But again, if you need specific look, convert your XML to HTML and define the look - such as line breaks - there. Do you understand what I mean? I told it three times and you've never reacted to it. When you ask what you're missing, this is likely the thing.

    You said that you already have correct line breaks in XML ("The file is showing line breaks when I open it in Notepad") - if it's true, the code from Jovan won't give you anything new.

  • syed baber Profile Picture
    11,633 on at
    RE: Line break in XML file created from X++ code

    Hi Martin,

    I got your point. That is why I was using CData and <br/> tags to format text inside the description tag and expecting the text to be displayed on separate line when open from browser. What else I need to do to convert XML to HTML and displayed it in a browser ? Sorry I don't have much knowledge XML file manipulation and transforms.

    Thanks,

    Baber.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Tocauer Profile Picture

Martin Tocauer 4

#2
Community Member Profile Picture

Community Member 2

#2
Nayyar Siddiqi Profile Picture

Nayyar Siddiqi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans