innerText of xml doc

M

martin

Hi,

I would like to write the text of an element to an xml doc as that it look
like this

<Address>&quot;Marty Jones&quot; &lt;[email protected]&gt;</Address>

however I can't seen to get the quote marks to come out correctly

the innertext property will change
"<" to &lt;
">" to &gt;

but not " to &quot;

when I write &quot; in the innerText string I get &amp;quot;

I tried to use innerXml instead of innertext but it didn't like that, I
guess because I am not inserting into a cdata section.

my failed attempts are below,

_message.DocumentElement.SelectSingleNode("Addresses/Address").InnerText =
"&quot;Marty Jones&quot; &lt;[email protected]&gt;";
<Address>&amp;quot;Marty Jones&amp;quot;
&amp;lt;[email protected]&amp;gt;</Address>

_message.DocumentElement.SelectSingleNode("Addresses/Address").InnerText =
"\"Marty Jones\" <[email protected]>";
<Address>"Marty Jones" &lt;[email protected]&gt;</Address>

any help is appreciated

cheers

martin.
 
P

Peter Rilling

Xml knows nothing about &quot; so it is unable to process this entity. You
might take a look at the HtmlEncode and HtmlDecode methods which might work
on these entities.
 
B

bruce barker

you can not do it with InnerText.

when you set innerText, it only quotes the chars it needs to. it does not
need to convert " to &quot as quotes are legal between tags. if you want to
control the quoting, then innerHtml is the answer

-- bruce (sqlwork.com)
 
M

martin

Thats fine,

so why is & being changed to &amp;

and < to &lt; , > to &gt;

but not " to &quot;

all I want is for &quot to appear in the element, the trouble is it always
change a & to &amp;

I'll try HTML encode but I'm not holding out much hope cause they'll still
be a & in the expression.

cheers

martin.
 
M

martin

Hi bruce,

Can you give an exemple of using innerHTML please,

I am using xmldocument class and this is not a member,
I can't see any other innerhtml property, perhaps I am missing something.

cheers

martin.
 
E

Eric Lawrence [MSFT]

Try the .xml property.

--
Thanks,

Eric Lawrence
Program Manager
Assistance and Worldwide Services

This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

martin

Hi Eric,

Thanks for the reply, but where will I find this .xml property.

I am using xmldocument and this does not have an xml property.
It has an innerXML property but this does not meet my needs. ("&quot;" still
get output as "&amp;quot;" and this is not what I require)

I need to have the string "&quot;" appear in <address><address> tags as

<Addresses>
<Address>&qout;Somebody's Name&quot; &lt;[email protected]&gt;</Address>
</Addresses>


if you could show me sample code to produce the above fragment then I would
very much appreciate it. My attempt is below.

cheers

martin.

Imports System.IO

Imports System.Xml

Module Module1

Sub Main()

Dim xmlDoc As New XmlDocument

Dim xmlElemAddress As XmlNode

Dim root As XmlElement

Try

xmlDoc.LoadXml("<Addresses/>")

xmlElemAddress = xmlDoc.CreateNode(XmlNodeType.Element, "Address", "")

xmlElemAddress.InnerText = "&quot;Somebody's Name&quot; <me@webs>"

root = xmlDoc.DocumentElement

root.AppendChild(xmlElemAddress)

xmlDoc.Save(Console.Out)

Catch ex As Exception

Console.WriteLine("***ERROR***")

Console.WriteLine(ex.Message.ToString())

Finally

End Try

End Sub

End Module
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top