469,610 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Convert string into XML format

Hi !
I have a big problem, i'm using XMLdocument to add elements to a XML file.
Elements are like this:

<name>Paolo</name>
<comment>this is <a comment></comment>
<description>In italian "is" is = !</description>

the problem is that i want to encode text in name,comment and description.

I have tryed to use InnerText property, but it doesn'work for encoding (like
was written on a msdn example...................)

Any idea ?
Nov 12 '05 #1
5 26551
Alessandro wrote:
I have a big problem, i'm using XMLdocument to add elements to a XML file.
Elements are like this:

<name>Paolo</name>
<comment>this is <a comment></comment>
<description>In italian "is" is = !</description>

the problem is that i want to encode text in name,comment and description.

Could you please elaborate what does that mean and what is your problem
actually?
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #2
Ok.. my problem is that i have string with that chars: ><'", i

Dim oXMLchild As XmlElement
oXMLchild = oXml.CreateElement("name")
oXMLchild.InnerText = "Paul"
oXMLappointment.AppendChild(oXMLchild)
oXMLchild = oXml.CreateElement("comment")
oXMLchild.InnerText = "this is <a comment>"
oXMLappointment.AppendChild(oXMLchild)
oXMLchild = oXml.CreateElement("description")
oXMLchild.InnerText = "In italian "is" is = !"
oXMLappointment.AppendChild(oXMLchild)
oXMLappointment.save("appointments.xml")

if i try to open the XML document i have an error : char invalid at line
......

so i want to save xmlfile with strings corectly encoded (for example <
become &lt; > become &gt;)

"Oleg Tkachenko"
<oleg@NO!SPAM!PLEASEtkachenko.com> ha scritto nel messaggio
news:Om**************@TK2MSFTNGP10.phx.gbl...
Alessandro wrote:
I have a big problem, i'm using XMLdocument to add elements to a XML file. Elements are like this:

<name>Paolo</name>
<comment>this is <a comment></comment>
<description>In italian "is" is = !</description>

the problem is that i want to encode text in name,comment and
description. Could you please elaborate what does that mean and what is your problem
actually?
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #3
Alessandro wrote:
oXMLchild.InnerText = "In italian "is" is = !"


I have no idea how you managed to compile this line! You have to escape
" character withing string literal:
oXMLchild.InnerText = "In italian ""is"" is = !"
Having fixed this your code works fine for me provided that I've saved
vb source file in Unicode.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #4
Ok... the " was only an error for te newsgroup code....
I make a mistake on other code...... but now works ! Your solition to my
no-problem helped me to find the solution to the real one !
thanks.

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> ha scritto nel messaggio
news:uK**************@tk2msftngp13.phx.gbl...
Alessandro wrote:
oXMLchild.InnerText = "In italian "is" is = !"


I have no idea how you managed to compile this line! You have to escape
" character withing string literal:
oXMLchild.InnerText = "In italian ""is"" is = !"
Having fixed this your code works fine for me provided that I've saved
vb source file in Unicode.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #5
Beware that encoded xml is Considered Harmful
(http://www.xml.com/pub/a/2003/08/20/embedded.html). Maybe you should
properly use mixed content model for that purpose. For example:

<comment>this is a <comment/></comment>

I can't see the utility of an <a comment> tag, which is invalid XML anyways.
So even if you want it encoded to escape XML validity for the enclosing
document, I have trouble understanding how are you going to use that
afterwards, when you come to process the <comment> contents.
Anyways, if you MUST encode it, one way of doing it is using an
XmlTextWriter to get the encoded string:

System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Xml.XmlTextWriter tw = new System.Xml.XmlTextWriter(new
System.IO.StringWriter(sb));
tw.WriteString("this is <a comment>");
tw.Flush();
Console.WriteLine(sb.ToString());

The stringbuilder would contain the encoded string you need to pass to the
InnerText property.
"Alessandro" <ge********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi !
I have a big problem, i'm using XMLdocument to add elements to a XML file.
Elements are like this:

<name>Paolo</name>
<comment>this is <a comment></comment>
<description>In italian "is" is = !</description>

the problem is that i want to encode text in name,comment and description.

I have tryed to use InnerText property, but it doesn'work for encoding (like was written on a msdn example...................)

Any idea ?

Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Joebloggs | last post: by
2 posts views Thread by Jason | last post: by
3 posts views Thread by GM | last post: by
4 posts views Thread by Ashraf Ansari | last post: by
4 posts views Thread by thomasc1020 | last post: by
14 posts views Thread by rtillmore | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.