Home All Groups Group Topic Archive Search About

& char in XML document

Author
13 Apr 2006 1:28 PM
cj
I'm receiving an xml formatted string that I pull data from by reading
it into an xml document like this:

Dim doc As New Xml.XmlDocument
doc.LoadXml(respstr)
Dim co_name As Xml.XmlNodeList = doc.GetElementsByTagName("co_name")
textbox1.text = co_name(0).innertext

Now I'm getting company names that have ampersands in them.  I was not
aware that was not allowed in xml and had no method of dealing with it.
  I can fix it like this:

textbox1.text = co_name(0).innertext.replace("&", "&")

But, is there another way?

I ask this question because someone showed me output they got from me
weeks ago and it had & in the company name instead of &  How the
heck did it work back then?  While the code is pretty much done now.
Back then it wasn't really even a program just a bunch of small groups
of code testing various ideas.

Author
13 Apr 2006 1:42 PM
Stephany Young
From what you've written it it rather confusing as to whether you are
recieving the 'bad' xml or whether you are sending the 'bad' xml. In one
sentence you say 'receiving' and in another you say 'got from me'.

If you are receiving the xml file from someone else and the xml is not
'well-formed' then return it to them for correction.

If you are building the xml file then using the XMLNode.InnerText = value
construct will ensure that any of the 5 'reserved' characters are handled
correctly.

By the way, the 5 reserved characters are < (&lt;) > (&gt) & (&amp;) '
(&apos;) and " (&quot).

If you are building the xml using string contenation then you will need to
replace the 'reserved' characters yourself.



Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:O12Y%235vXGHA.1204@TK2MSFTNGP04.phx.gbl...
> I'm receiving an xml formatted string that I pull data from by reading it
> into an xml document like this:
>
> Dim doc As New Xml.XmlDocument
> doc.LoadXml(respstr)
> Dim co_name As Xml.XmlNodeList = doc.GetElementsByTagName("co_name")
> textbox1.text = co_name(0).innertext
>
> Now I'm getting company names that have ampersands in them.  I was not
> aware that was not allowed in xml and had no method of dealing with it. I
> can fix it like this:
>
> textbox1.text = co_name(0).innertext.replace("&amp;", "&")
>
> But, is there another way?
>
> I ask this question because someone showed me output they got from me
> weeks ago and it had & in the company name instead of &amp;  How the heck
> did it work back then?  While the code is pretty much done now. Back then
> it wasn't really even a program just a bunch of small groups of code
> testing various ideas.
Author
13 Apr 2006 2:15 PM
cj
I'm receiving the xml file, or probably better stated a string, via
internet connection from another company and It is well-formed for xml
as & is represented as &amp;.  I have to take this data and give it to
folks here in a flat fixed width ascii file.  So &amp; has to become &.

Now keep in mind they don't want the entire xml file just certain fields
so I pull the desired fields out, string them together and write them to
  the ascii file.


Stephany Young wrote:
Show quoteHide quote
> From what you've written it it rather confusing as to whether you are
> recieving the 'bad' xml or whether you are sending the 'bad' xml. In one
> sentence you say 'receiving' and in another you say 'got from me'.
>
> If you are receiving the xml file from someone else and the xml is not
> 'well-formed' then return it to them for correction.
>
> If you are building the xml file then using the XMLNode.InnerText = value
> construct will ensure that any of the 5 'reserved' characters are handled
> correctly.
>
> By the way, the 5 reserved characters are < (&lt;) > (&gt) & (&amp;) '
> (&apos;) and " (&quot).
>
> If you are building the xml using string contenation then you will need to
> replace the 'reserved' characters yourself.
>
>
>
> "cj" <cj@nospam.nospam> wrote in message
> news:O12Y%235vXGHA.1204@TK2MSFTNGP04.phx.gbl...
>> I'm receiving an xml formatted string that I pull data from by reading it
>> into an xml document like this:
>>
>> Dim doc As New Xml.XmlDocument
>> doc.LoadXml(respstr)
>> Dim co_name As Xml.XmlNodeList = doc.GetElementsByTagName("co_name")
>> textbox1.text = co_name(0).innertext
>>
>> Now I'm getting company names that have ampersands in them.  I was not
>> aware that was not allowed in xml and had no method of dealing with it. I
>> can fix it like this:
>>
>> textbox1.text = co_name(0).innertext.replace("&amp;", "&")
>>
>> But, is there another way?
>>
>> I ask this question because someone showed me output they got from me
>> weeks ago and it had & in the company name instead of &amp;  How the heck
>> did it work back then?  While the code is pretty much done now. Back then
>> it wasn't really even a program just a bunch of small groups of code
>> testing various ideas.
>
>
Author
13 Apr 2006 2:36 PM
Stephany Young
Using your code, with the addition of:

  Dim respstr As String = "<docelement><co_name>abc &amp;
xyz</co_name></docelement>"

at the beginnning and:

  Console.WriteLine(co_name(0).InnerText)

I get abc & xyz in both the textbox and displayed in the output window.

Therefore the incomming '&amp;' is correctly being converted to '&', so it
is difficult to understand exactly what your problem is.



w
Show quoteHide quote
"cj" <cj@nospam.nospam> wrote in message
news:%23VKaFUwXGHA.1196@TK2MSFTNGP03.phx.gbl...
> I'm receiving the xml file, or probably better stated a string, via
> internet connection from another company and It is well-formed for xml as
> & is represented as &amp;.  I have to take this data and give it to folks
> here in a flat fixed width ascii file.  So &amp; has to become &.
>
> Now keep in mind they don't want the entire xml file just certain fields
> so I pull the desired fields out, string them together and write them to
> the ascii file.
>
>
> Stephany Young wrote:
>> From what you've written it it rather confusing as to whether you are
>> recieving the 'bad' xml or whether you are sending the 'bad' xml. In one
>> sentence you say 'receiving' and in another you say 'got from me'.
>>
>> If you are receiving the xml file from someone else and the xml is not
>> 'well-formed' then return it to them for correction.
>>
>> If you are building the xml file then using the XMLNode.InnerText = value
>> construct will ensure that any of the 5 'reserved' characters are handled
>> correctly.
>>
>> By the way, the 5 reserved characters are < (&lt;) > (&gt) & (&amp;) '
>> (&apos;) and " (&quot).
>>
>> If you are building the xml using string contenation then you will need
>> to replace the 'reserved' characters yourself.
>>
>>
>>
>> "cj" <cj@nospam.nospam> wrote in message
>> news:O12Y%235vXGHA.1204@TK2MSFTNGP04.phx.gbl...
>>> I'm receiving an xml formatted string that I pull data from by reading
>>> it into an xml document like this:
>>>
>>> Dim doc As New Xml.XmlDocument
>>> doc.LoadXml(respstr)
>>> Dim co_name As Xml.XmlNodeList = doc.GetElementsByTagName("co_name")
>>> textbox1.text = co_name(0).innertext
>>>
>>> Now I'm getting company names that have ampersands in them.  I was not
>>> aware that was not allowed in xml and had no method of dealing with it.
>>> I can fix it like this:
>>>
>>> textbox1.text = co_name(0).innertext.replace("&amp;", "&")
>>>
>>> But, is there another way?
>>>
>>> I ask this question because someone showed me output they got from me
>>> weeks ago and it had & in the company name instead of &amp;  How the
>>> heck did it work back then?  While the code is pretty much done now.
>>> Back then it wasn't really even a program just a bunch of small groups
>>> of code testing various ideas.
>>
Author
13 Apr 2006 5:40 PM
cj
Humm.  I'll have to write that into a test app myself then try to
explain the differences between it (assuming it works for me) and what
I'm doing in my program.  I'll have to get back to you.  I'm swamped at
the moment so it might be awhile, or given the holiday it might be next
week.  Anyway, thanks and I'll get back to you.

Stephany Young wrote:
Show quoteHide quote
> Using your code, with the addition of:
>
>   Dim respstr As String = "<docelement><co_name>abc &amp;
> xyz</co_name></docelement>"
>
> at the beginnning and:
>
>   Console.WriteLine(co_name(0).InnerText)
>
> I get abc & xyz in both the textbox and displayed in the output window.
>
> Therefore the incomming '&amp;' is correctly being converted to '&', so it
> is difficult to understand exactly what your problem is.
>
>
>
>  w
> "cj" <cj@nospam.nospam> wrote in message
> news:%23VKaFUwXGHA.1196@TK2MSFTNGP03.phx.gbl...
>> I'm receiving the xml file, or probably better stated a string, via
>> internet connection from another company and It is well-formed for xml as
>> & is represented as &amp;.  I have to take this data and give it to folks
>> here in a flat fixed width ascii file.  So &amp; has to become &.
>>
>> Now keep in mind they don't want the entire xml file just certain fields
>> so I pull the desired fields out, string them together and write them to
>> the ascii file.
>>
>>
>> Stephany Young wrote:
>>> From what you've written it it rather confusing as to whether you are
>>> recieving the 'bad' xml or whether you are sending the 'bad' xml. In one
>>> sentence you say 'receiving' and in another you say 'got from me'.
>>>
>>> If you are receiving the xml file from someone else and the xml is not
>>> 'well-formed' then return it to them for correction.
>>>
>>> If you are building the xml file then using the XMLNode.InnerText = value
>>> construct will ensure that any of the 5 'reserved' characters are handled
>>> correctly.
>>>
>>> By the way, the 5 reserved characters are < (&lt;) > (&gt) & (&amp;) '
>>> (&apos;) and " (&quot).
>>>
>>> If you are building the xml using string contenation then you will need
>>> to replace the 'reserved' characters yourself.
>>>
>>>
>>>
>>> "cj" <cj@nospam.nospam> wrote in message
>>> news:O12Y%235vXGHA.1204@TK2MSFTNGP04.phx.gbl...
>>>> I'm receiving an xml formatted string that I pull data from by reading
>>>> it into an xml document like this:
>>>>
>>>> Dim doc As New Xml.XmlDocument
>>>> doc.LoadXml(respstr)
>>>> Dim co_name As Xml.XmlNodeList = doc.GetElementsByTagName("co_name")
>>>> textbox1.text = co_name(0).innertext
>>>>
>>>> Now I'm getting company names that have ampersands in them.  I was not
>>>> aware that was not allowed in xml and had no method of dealing with it.
>>>> I can fix it like this:
>>>>
>>>> textbox1.text = co_name(0).innertext.replace("&amp;", "&")
>>>>
>>>> But, is there another way?
>>>>
>>>> I ask this question because someone showed me output they got from me
>>>> weeks ago and it had & in the company name instead of &amp;  How the
>>>> heck did it work back then?  While the code is pretty much done now.
>>>> Back then it wasn't really even a program just a bunch of small groups
>>>> of code testing various ideas.
>
Author
13 Apr 2006 2:35 PM
zacks
How are these special characters handled in XMLSerialization?
Author
13 Apr 2006 3:06 PM
Cor Ligthert [MVP]
Stephany,

I had to smile when I saw this.

> From what you've written it it rather confusing as to whether you are
> recieving ............................ In one sentence you say 'receiving'
> ..............

Sorry I could not resist to show it you because of the context from the
message and with no other meaning than the message was in.

Cor

Show quoteHide quote
>
> If you are receiving the xml file from someone else and the xml is not
> 'well-formed' then return it to them for correction.
>
> If you are building the xml file then using the XMLNode.InnerText = value
> construct will ensure that any of the 5 'reserved' characters are handled
> correctly.
>
> By the way, the 5 reserved characters are < (&lt;) > (&gt) & (&amp;) '
> (&apos;) and " (&quot).
>
> If you are building the xml using string contenation then you will need to
> replace the 'reserved' characters yourself.
>
>
>
> "cj" <cj@nospam.nospam> wrote in message
> news:O12Y%235vXGHA.1204@TK2MSFTNGP04.phx.gbl...
>> I'm receiving an xml formatted string that I pull data from by reading it
>> into an xml document like this:
>>
>> Dim doc As New Xml.XmlDocument
>> doc.LoadXml(respstr)
>> Dim co_name As Xml.XmlNodeList = doc.GetElementsByTagName("co_name")
>> textbox1.text = co_name(0).innertext
>>
>> Now I'm getting company names that have ampersands in them.  I was not
>> aware that was not allowed in xml and had no method of dealing with it. I
>> can fix it like this:
>>
>> textbox1.text = co_name(0).innertext.replace("&amp;", "&")
>>
>> But, is there another way?
>>
>> I ask this question because someone showed me output they got from me
>> weeks ago and it had & in the company name instead of &amp;  How the heck
>> did it work back then?  While the code is pretty much done now. Back then
>> it wasn't really even a program just a bunch of small groups of code
>> testing various ideas.
>
>