& char in XML document

  • Thread starter Thread starter cj
  • Start date Start date
C

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.
 
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.
 
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.
 
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
 
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
 
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.
 
Back
Top