XML RTL Questions

Hi,
I have this piece of rtl2 code

  var outerElement := new XmlElement withName('outer');
  var innerElement := new XmlElement withName('inner');
  outerElement.Add(innerElement);
  var text := new XmlText(innerElement);
  text.Value := 'hello';
  
  var document := XmlDocument.WithRootElement(outerElement);

  var options := new XmlFormattingOptions;
  options.NewLineForElements := true;
  options.WriteNewLineAtEnd := true;
  var doc := document.ToString(true,options);
  
  NSLog('%@',doc);
  
  document.SaveToFile('/Users/JohnMoshakis/Downloads/format.xml',options);

I get this

<outer><inner/></outer>

Whats the proper way to add xmltext ? Also why is there no formatting ?

Cheers,
John

Not sure what this is asking. whats the result you want, <inner>hello</inner>? if so, just set `innerElement.Value := “hello”.

as it stands, you’re never doing anything with the text object yu create (assigned from setting its value), so it wont show in the document, because its never added anywhere…

define “no formatting”? XmlFormattingOptions has a boatload off options for how you want to your XMNL to look (if there’s any concrete scenario you cant achieve, let me know).

here’s the one I use(s) for Fire/Ebuild/etc:

            XmlStyleFire = new XmlFormattingOptions();
            XmlStyleFire.WhitespaceStyle = XmlWhitespaceStyle.PreserveWhitespaceAroundText;
            XmlStyleFire.EmptyTagSyle = XmlTagStyle.PreferSingleTag;
            XmlStyleFire.Indentation = "\u0020\u0020\u0020\u0020";
            XmlStyleFire.NewLineForElements = true;
            XmlStyleFire.NewLineForAttributes = false;
            XmlStyleFire.NewLineSymbol = XmlNewLineSymbol.LF;
            XmlStyleFire.WriteNewLineAtEnd = true;
            XmlStyleFire.WriteBOM = false;
            XmlStyleFire.SpaceBeforeSlashInEmptyTags = false;

            XmlStyleVisualStudio = new XmlFormattingOptions();
            XmlStyleVisualStudio.WhitespaceStyle = XmlWhitespaceStyle.PreserveWhitespaceAroundText;
            XmlStyleVisualStudio.EmptyTagSyle = XmlTagStyle.PreferSingleTag;
            XmlStyleVisualStudio.Indentation = "\u0020\u0020";
            XmlStyleVisualStudio.NewLineForElements = true;
            XmlStyleVisualStudio.NewLineForAttributes = false;
            XmlStyleVisualStudio.NewLineSymbol = XmlNewLineSymbol.CRLF;
            XmlStyleVisualStudio.SpaceBeforeSlashInEmptyTags = true;
            XmlStyleVisualStudio.WriteNewLineAtEnd = false;
            XmlStyleVisualStudio.WriteBOM = true;

the first was my preferred one, but a few years back Fire has switched to exclusively using the XmlStyleVisualStudio now, to avoid project file changes when switching IDEs.

Yes I was wanting hello. It was confusing because the constructor for XmlText takes the parent element.

Initially by creating a new XmlFormattingOptions and not setting anything I got everything on a single line

I then added
options.NewLineForElements := true;
options.WriteNewLineAtEnd := true;

I would of thought that that would of been enough to yield something that looked formatted, as per what I requested.

  options.NewLineForElements := true;
  options.WriteNewLineAtEnd := true;
  options.WhitespaceStyle := XmlWhitespaceStyle.PreserveWhitespaceAroundText;

Adding the WhitespaceStyle to the previous 2 lines, seems to be required for the newlineforlements to work ?

i’l have to check with the team; it seems that this .ctor should probably not be public…

Possibly; the default is PreserveAllWhitespace which, as it says on the box, preserves all space exactly as it was read it on set up — and since (I assume) you’re not manually adding and whitespace via code this would mean there is no whitespace to emit…