Tab characters in an XML file

J

JB

Hi everyone,

I'm having problems with Tab characters in an XML file.
I want to store a Tab character as part of an element value e.g.
<Separator>TAB</Separator>
I'm using XmlWriter to write my file and XmlReader to read it.

At first my tab character was stored in the element, but was ignored
when it was read back (giving me an empty string as a content).
Herfried K. Wagner suggested on this group to use the
'xml:space="preserve"' attribute. It worked fine, except that now I
have to generate my whole file without indentation otherwise the
reader generates an exception.

Ideally I would like
1) To be able to store Tab characters in an element value
2) Have an indented XML file e.g.

<Config>
<Some Container Element>
<Separator>TAB</Separator>
</Some Container Element>
<Some Other Element>
</Some Other Element>
<Config>

I've tried to play with the values of the XmlReader and XmlWriter
settings, but can only manage to get either 1) or 2) but not both at
the same time.

Can somebody help me with that?

Thanks
JB
 
C

Cor Ligthert[MVP]

JB,

Probably you need an &arg litteral. I don't know which it is, but as I have
to do it as well can you search for it on Internet yourself.

Cor
 
C

Cor Ligthert[MVP]

Duh,

did not read the message from Stephany yet, it is of course &amp

Cor
 
S

Stephany Young

No Cor, it is NOT &amp. It IS &amp;.

The semi-colon is not for decoration, it is critical.

Any, if you're talking about a TAB character, there is no way it is &amp;.
It would be .

& = escape character
# = numeric literal
x = hexadecimal
9 = ASCII (also ANSI) for TAB character
; = escape sequence terminator
 
M

Martin Honnen

JB said:
I'm having problems with Tab characters in an XML file.
I want to store a Tab character as part of an element value e.g.
<Separator>TAB</Separator>
I'm using XmlWriter to write my file and XmlReader to read it.

At first my tab character was stored in the element, but was ignored
when it was read back (giving me an empty string as a content).

Can you show us your code? I can't reproduce the problem, the following
test code

Dim fileName As String = "..\..\XmlTest1.xml"

Dim writerSettings As New XmlWriterSettings()
writerSettings.Indent = True

Using writer As XmlWriter = XmlWriter.Create(fileName,
writerSettings)
writer.WriteStartDocument()
writer.WriteStartElement("root")
writer.WriteStartElement("foo")
writer.WriteElementString("bar", ChrW(9).ToString())
writer.WriteEndDocument()
End Using

Using reader As XmlReader = XmlReader.Create(fileName)
While reader.Read()
If reader.NodeType = XmlNodeType.Element And
reader.LocalName = "bar" Then
Dim bar As String = reader.ReadString()
Console.WriteLine("|{0}|", bar)
Console.WriteLine(bar = ChrW(9).ToString())
End If
End While
End Using

results in the output

| |
True

suggesting that the tab character is written and read back.

Make sure you don't use an XmlReader with XmlReaderSettings where
IgnoreWhitespace is set to True as in that case the tab would be ignored.
 
J

JB

Can you show us your code? I can't reproduce the problem, the following
test code

Dim fileName As String = "..\..\XmlTest1.xml"

Dim writerSettings As New XmlWriterSettings()
writerSettings.Indent = True

Using writer As XmlWriter = XmlWriter.Create(fileName,
writerSettings)
writer.WriteStartDocument()
writer.WriteStartElement("root")
writer.WriteStartElement("foo")
writer.WriteElementString("bar", ChrW(9).ToString())
writer.WriteEndDocument()
End Using

Using reader As XmlReader = XmlReader.Create(fileName)
While reader.Read()
If reader.NodeType = XmlNodeType.Element And
reader.LocalName = "bar" Then
Dim bar As String = reader.ReadString()
Console.WriteLine("|{0}|", bar)
Console.WriteLine(bar = ChrW(9).ToString())
End If
End While
End Using

results in the output

| |
True

suggesting that the tab character is written and read back.

Make sure you don't use an XmlReader with XmlReaderSettings where
IgnoreWhitespace is set to True as in that case the tab would be ignored.

Hi All,

Thanks for all your suggestions.
Following Martin's code I isolated the problem.
I read my file sequentially and I use the function
ReadElementContentAsString.
If I write the file without indentation, ReadElementContentAsString
works fine.
If I write the file with indentation, ReadElementContentAsString
causes an exception: "'Whitespace' is an invalid XmlNodeType. Line 3,
position 8."

I don't really want to have to rewrite all my code to look like Method
2, so I'm thinking that it might be simpler to escape all my string
contents to transform any special character into the escaped version.
Is there an option to do this automatically for my entire XML file or
do I have to do it "manually" for each field with some sort of
replace?

See my code below:

Using reader As XmlReader = XmlReader.Create(fileName)
reader.ReadStartElement("root")
reader.ReadStartElement("foo")

'Method 1 - Doesn't work
' Dim bar As String = reader.ReadElementContentAsString("bar",
"")

'Method 2 - Works!
reader.Read()
Debug.Assert(reader.NodeType = XmlNodeType.Element And
reader.LocalName = "bar")
Dim bar As String = reader.ReadString()

Console.WriteLine("|{0}|", bar)
Console.WriteLine(bar = ChrW(9).ToString())

reader.ReadEndElement()
reader.ReadEndElement()
End Using
 
M

Martin Honnen

JB said:
Following Martin's code I isolated the problem.
I read my file sequentially and I use the function
ReadElementContentAsString.
If I write the file without indentation, ReadElementContentAsString
works fine.
If I write the file with indentation, ReadElementContentAsString
causes an exception: "'Whitespace' is an invalid XmlNodeType. Line 3,
position 8."

Well that exception has nothing to do with the 'bar' element containing
a tab character or not. It occurs because
reader.ReadStartElement("foo")

after this call the reader is positioned on a node of type Whitespace
and therefore ReadElementContentAsString() is not an allowed operation.
For that to work you first have to make sure the reader is positioned on
the start element.
 
M

Martin Honnen

JB said:
Thanks for all your suggestions.
Following Martin's code I isolated the problem.
I read my file sequentially and I use the function
ReadElementContentAsString.
If I write the file without indentation, ReadElementContentAsString
works fine.
If I write the file with indentation, ReadElementContentAsString
causes an exception: "'Whitespace' is an invalid XmlNodeType. Line 3,
position 8."

If you want to use the XmlReader in a way symmetric to the XmlWriter use
I suggested then you can use

reader.ReadStartElement("root")
reader.ReadStartElement("foo")
Dim bar As String = reader.ReadElementString("bar")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top