XML Formatting??

JaedenRuiner

Well-known member
Joined
Aug 13, 2007
Messages
340
Programming Experience
10+
Wow,

Why i ever took so long to look into the basis of XML I'll never know, but now that I'm here (finally) I am thrilled. Especially with the ease and simplicity the programmatic xml objects make it.

However, I was wondering about the formatting issue of the WhiteSpace and TextNodes.

Basically, you create elements and attributes for those elements within the XMLDocument structure, and when you output those elements you get something like this:
HTML:
<?xml version="1.0" encoding="Windows-1252"?>
<!DOCTYPE rexamship.cfg>
<settings user="default">
  <xlColumnMap>
    <column index="1" letter="A">
      <mapping purpose="0" />
    </column>
  </xlColumnMap>
</settings>

Pretty ain't it? :D Anyway, whenever you add a child node to the "terminated" element (<mapping />) it adjusts appropriately, adding the termintor element, (</mapping>). however, when it is a TextNode it adds it to the same line:
HTML:
    <column index="1" letter="A">
      <mapping purpose="0">My Text</mapping>
    </column>

And if you try to use the whitespace node or other such invention you get:
HTML:
    <column index="1" letter="A">
      <mapping purpose="0">
My Text
</mapping>
    </column>

Is there a way to determine the indentation level of a particular node, is it always 2 spaces per child indent, or something to that effect?
Yea, I know this sounds pointless, but I'm just curious in order to better understand the interpretation and uses of these different nodes.
As well, if you have an Element Node that creates a TextNode within it, any subsequent child nodes are not line-fed and indented on the next line.

Thanks
 
HTML:
      <mapping purpose="0">
My Text
</mapping>
That would be a correct view of your text node, not? A linefeed and the string "My Text" and another linefeed. So if this was your text and xml was outputted as:
HTML:
      <mapping purpose="0">
My Text
      </mapping>
or even:
HTML:
      <mapping purpose="0">
            My Text
      </mapping>
then your text node would not be same as input any longer because a lot of spaces was added.

You can also save the XmlDocument through a XmlWriter and use the XmlSettings option NewLineHandling.Entitize, that would output that textnode like this:
HTML:
<comment>
My Text
</comment>
With the XmlSettings you can also control indentation.
 
Hrm..

Okay, i'll have to look into those. I was basically generating my XML via the XmlDocument class, using the save/load methods, which caused unrelated strangeness between teh XmlDocument.Save(io.stream) and .Save(filename). The Io.Stream outputted the XmlDeclaration I made with the CreateXmlDeclaration command:
VB.NET:
<?xml version="1.0" encoding="Windows-1252"?>
Though, in my command I was trying to set the coding to ISO-8859-1 and it has never taken. however, i did _xml.Save(Console.Out) and that line printed, but when i did _xml.saber("myFile.xml") the <? ?> line was not listed in the text document.

I'll have to research the usage of the XmlWriter class and the settings that can be applied to them.
<Edit>
Actually i found that I need to probably create and XmlWriter Class and set the OmitDeclaration to false, and use the Xml.XmlDocument.Saver(Writer) method. Which would resolve that. As for the indentation, I mostly am looking for this:

VB.NET:
[U]The Way It Currently Exports[/U]
  <XLColumnMap>
    <column idx="1">TextNodeValue<dbmap>DB_Col_Name</dbmap><import>True</import></column>
  </XLColumnMap>

VB.NET:
[U]The Way I'd Like it to Export[/U]
  <XLColumnMap>
    <column idx="1">TextNodeValue
      <dbmap>DB_Col_Name</dbmap>
      <import>True</import>
    </column>
  </XLColumnMap>

By putting the TextNode as a child to the column node, it causes all subsequent child elements to not be indented or set to the next line. I would want all "elements" to automatically be on a new line with proper indentation, and TextNodes well, it can handle them however it wants. :D

Thanks.
 
Last edited:
but these are different text nodes (I left the node tag markers for clarification):
HTML:
>TextNodeValue<
HTML:
>TextNodeValue
      <
Clearly these are different texts. If you look at these sources in Internet Explorer Xml tree view (as seen without xsl stylesheet) they will be presented identical, but that is merely because the data is reformatted for tree view display purposes only, "View source" in IE and you get the correct original Xml source document in both cases. I mention IE is because it is the most obvious reason I can think of for anyone being confused about this. There is no rule, nor can be, to say that whitespace at start or end of a text node is not allowed and will be ignored, consequently you have to put up with seeing the text node exactly as it is or Entitize it when your text content is of such nature. And in opposite case as yours, you can't add arbitrary text to your text nodes and expect anyone else than yourself to be able to decipher what the text was originally or should be.

The place you have put the text value "TextNodeValue" is valid, but I don't like such structure, I would rather add that value as an attribute or xml child node of "column" like dbmap/import. Reason is it isn't easy to use with a XPath expression, it looks funny, and it messes up indentation ;)
 
Back
Top