473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Tab characters in an XML file

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
Jun 27 '08 #1
7 3643
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

"JB" <jb*********@gmail.comschreef in bericht
news:36**********************************@d77g2000 hsb.googlegroups.com...
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
Jun 27 '08 #2
Duh,

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

Cor

"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:A7**********************************@microsof t.com...
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

"JB" <jb*********@gmail.comschreef in bericht
news:36**********************************@d77g2000 hsb.googlegroups.com...
>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
Jun 27 '08 #3
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 &#x9;.

& = escape character
# = numeric literal
x = hexadecimal
9 = ASCII (also ANSI) for TAB character
; = escape sequence terminator
"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:5C**********************************@microsof t.com...
Duh,

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

Cor

"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:A7**********************************@microsof t.com...
>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

"JB" <jb*********@gmail.comschreef in bericht
news:36**********************************@d77g200 0hsb.googlegroups.com...
>>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
Jun 27 '08 #4
JB wrote:
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.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #5
JB
On 15 Jun, 15:09, Martin Honnen <mahotr...@yahoo.dewrote:
JB wrote:
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.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
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
Jun 27 '08 #6
JB wrote:
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.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #7
JB wrote:
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")

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Roy W. Andersen | last post by:
I've been searching google about this for days but can't find anything, so I'm hoping someone here can help me out. I'm trying to create zip-files without needing the zip-file extension in PHP,...
0
by: Stuart Newton | last post by:
We have just migrated a pile of ASP-driven content-managed websites from Win2000/SQL7 to Win2003/SQL2000 and noticed that, on some of them, certain characters such as the pound sign (£), 66..99...
1
by: rmgalante | last post by:
I have a Windows Service that reads and writes an XML file to disk periodically. I use the XmlSerializer to serialize and deserialize the XML file on disk. I am writing the file using an...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
3
by: Frank Niessink | last post by:
Hi list, First of all, I wish you all a happy 2006. I have a small question that googling didn't turn up an answer for. So hopefully you'll be kind enough to send me in the right direction. ...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
2
by: joakim.hove | last post by:
Hello, I am having great problems writing norwegian characters æøå to file from a python application. My (simplified) scenario is as follows: 1. I have a web form where the user can enter his...
3
by: =?Utf-8?B?SG9seXNtb2tl?= | last post by:
Hi there, I am having a problem im my webservices method when trying to save a file with latin characters to disk passed through WSE. I have noticed that when trying to read the file name from...
4
by: adiel_g | last post by:
I am trying to display some data that I received into a form. The form is in english. The data is in cyrillic (russian) which uses codepage 1251. I am trying to change the button text to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.