[Gambas-user] Getting the "un-specialed" XMLElement.TextContent string

B Bruen bbruen at ...2308...
Wed Jun 25 01:03:01 CEST 2014


On Tue, 24 Jun 2014 16:36:11 +0200
"Adrien Prokopowicz" <adrien.prokopowicz at ...626...> wrote:

> Le Tue, 17 Jun 2014 01:37:49 +0200, B Bruen <bbruen at ...2308...> a  
> écrit:
> 
> > (I'm sure I've done this before, but I can't find my prior code nor can  
> > I find it in either of the help sites.)
> >
> > I just want to set a string variable to the text content of an element.   
> > I am almost certain there was a way to have it automatically convert all  
> > the "special character"s, like &nbsp, &lt, etc etc to their "normal"  
> > representation.  Is there such a function, or was I just dreaming?
> >
> > tia
> > Bruce
> >
> 
> (Sorry about the late answer, I was quite busy these last few months ...)
> 
> The XmlNode.TextContent property is actually what you're looking for : it  
> defines the node's text content without these entities. For example, this  
> little code :
> 
>    Dim doc As New XmlDocument
> 
>    doc.FromString("<xml>Fish & chips</xml>") 'This can be doc.Open(),  
> doc.Content = "..." or whatever
> 
>    Print doc.ToString()
> 
>    Print doc.Root.TextContent
> 
> ... will output this :
> 
>    <?xml version="1.0" encoding="UTF-8"?><xml>Fish & chips</xml>
>    Fish & chips
> 
> Moreover, you have the static couple of methods XmlNode.Serialize() and  
> XmlNode.Deserialize() that allow you to convert strings back and forth  
> between the "with entities" and "without entities" representations.
> 
> Regards,
> -- 
> Adrien Prokopowicz
> 
Adrien,

That's what I thought. But now I found my problem.

The special characters are in the attributes of the element, not in the text content.  Here's an example:

(racenode is an XMLElement)

? racenode.Tostring(true)
<Race RaceNo="8" RaceTime="2014-06-25T15:55:00" RaceName="C,G&E 0 - 68 HANDICAP" Distance="1211" WeatherChanged="N" WeatherCond="3" WeatherDesc="Showery" TrackChanged="N" TrackCond="5" TrackDesc="Heavy">
( inner content excised )
</Race>

I'm looking at a bit of code that is:
    $title = raceNode.Attributes["RaceName"]
which ends up with the raw value, viz
    C,G&E 0 - 68 HANDICAP

Is there a way to get an attribute value "de-specialed"?

tia
Bruce
-- 
B Bruen <bbruen at ...2308...>




More information about the User mailing list