How to parse XML in C# (tooltips)

N

Nat

Hi

<..I'm useing c# asp.net, and it's webapplications..>

I have a content.xml file with some content id's, and a text

(fx.: <content Id = bla bla bla bla></content id>

The text in content.xml is used in some tooltips. My problem is now
that in some of tooltips the text from the conten.xml is to large, so
you can't get around to read it before it's gone. So I tried to put
the java window.open() into the write statement - but how do I get the
content id's text to show in the new window?

Some of the code that displays the tooltip looks like this:

protected override void Render(HtmlTextWriter w)
{
if (helpString!=null)
{
w.Write("<span class='tooltipIconOuter'><img
title='"+this.helpString+"' class='tooltipIconInner' align='center'
src='../Lib/images/questionmark.gif'#
onclick='window.open();'></span>+");
}

I've also heard that I could parse the .xml, what does that mean, and
how is that done?

Thanks, Nat
 
B

Ben Rush

XML parsing is done using the classes as defined in the System.Xml namespace
to break up the xml content into more manageable bits that you can consume
more easily.

Since you have an xml file, take a look at the System.Xml.XmlDocument class.
It's not the most efficient (it sticks the whole XML file in memory and
processes it there), but it's by far the easiest to use.

Ben
 
V

Vjekoslav Babic

The text in content.xml is used in some tooltips. My problem is now
that in some of tooltips the text from the conten.xml is to large, so
you can't get around to read it before it's gone. So I tried to put
the java window.open() into the write statement - but how do I get the
content id's text to show in the new window?

Some of the code that displays the tooltip looks like this:

protected override void Render(HtmlTextWriter w)
{
if (helpString!=null)
{
w.Write("<span class='tooltipIconOuter'><img
title='"+this.helpString+"' class='tooltipIconInner' align='center'
src='../Lib/images/questionmark.gif'#
onclick='window.open();'></span>+");
}

You need to get the reference to the new window and then write to its
document property. Try using this code instead:

protected override void Render(HtmlTextWriter w)
{
if (helpString!=null)
{
w.Write("<span class='tooltipIconOuter'><img
title = '"+this.helpString+"' class='tooltipIconInner' align='center'
src = '../Lib/images/questionmark.gif'#
onclick = 'myWin = window.open(); myWin.document.write(\"" + helpString
I've also heard that I could parse the .xml, what does that mean, and
how is that done?

Parsing an XML file is a process of converting a plain text representation
of the XML file into a logical representation, such as XML DOM. Parsing is
done in the background by various classes in System.Xml namespace, so you
don't need to worry about parsing itself. What you probably need is how to
extract specific information from the XML, where XPath is the easiest way
you can go. Try this:

// MyXMLFile.xml
<?xml version="1.0" encoding="utf-8" ?>
<root>
<content id="tooltip1">My tooltip</content>
<content id="tooltip2">Another tooltip</content>
<content id="tooltip3">Yet another tooltip</content>
</root>

// somewhere in a .cs class file
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
string tooltip = "";
XmlNode tooltipNode = doc.SelectSingleNode("/root/content[@id='tooltip2']");
if (tooltipNode != null)
{
tooltip = tooltipNode.Value;
}

Hope this helps.

Regards,
 
N

Nat

Thanks for all the help

I've tried putting in the following code:

protected override void Render(HtmlTextWriter w)
{
if (helpString!=null)
{
w.Write("<span class='tooltipiconouter'><img title =
'"+this.helpString+"' class='tooltipIconInner' align='center' src =
'../Lib/images/questionmark.gif' #onclick = 'window.open();
document.write(\"" +helpString+ "\")'></span>");
}

The "tooltip" shows, but when I click the image the window doesn't
open.... I can't figure out why, because if I view the html source it
seems like it should work.

What am I missing to get the new window to open on click???

Thanks
 
N

Nat

Okay, I solved it my self by adding a 'a href', see code below:

protected override void Render(HtmlTextWriter w)
{
if (helpString!=null)
{
w.Write("<span class='tooltipiconouter'><img title =
'"+this.helpString+"' class='tooltipIconInner' align='center' src =
'../Lib/images/questionmark.gif' a href='#' onclick = 'myWin =
window.open(); myWin.document.write(\"" +helpString+
"\")'></a></span>");
}

Bur now I want to add some parameters in to the win.open(), så I can
control the window size, color and name - how do I write that into the
code above??

Thanks
 

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

Top