We help IT Professionals succeed at work.

XML Problem while using C#

Chris_Lombardi
on
615 Views
Last Modified: 2008-01-09
I have written the below code and I am trying to have the information collected from the XML document put into a label so I can reference this is my ASP.Net code. What am I doing wrong? Sorry for the n00b question, I am still learning the language.



<script language=c# runat=server>

protected XmlDocument oXmlDoc;
public void Page_Load(object sender, System.EventArgs e)
{
 oXmlDoc = new XmlDocument();
 try
 {
  oXmlDoc.Load("http://xml.corporate-ir.net/irxmlclient.asp?compid=119620&reqtype=quotes ");
 }
 catch(Exception ex)
 {
  Response.Write("<br>Error: <br>" + ex.Message);
 }
 XmlNode oNode = oXmlDoc.DocumentElement;
 XmlNodeList StockQuote = oNode.SelectNodes("/IRXML/StockQuotes/Stock_Quote/Trade");
 XmlNodeList LastTrade = oNode.SelectNodes("/IRXML/StockQuotes/Stock_Quote/Date");
 
Label lbltrade, lblquote;
 
lbltrade.Text = LastTrade.Item(0).InnerText;
lblquote.Text = StockQuote.Item(0).InnerText;
 
 
}
 
</script>
Comment
Watch Question

Author

Commented:
Compiler Error Message: CS0165: Use of unassigned local variable 'lbltrade'

That is the error I am getting
That means an object is being used before it has been instantiated.
I don't use the built-in form object's provided by microsoft, but it would appear you need to do this...
Label lbltrade = new Label();
Label lblquote = new Label();

Author

Commented:
odd

Author

Commented:
for some reason i cannot post my code anymore. weird. In any case, That worked, but when trying to display the data in the label, nothing shows. Even when I replaced the data i retrieved from the XML document with plain text.. Any ideas?

My assumption is that you need to give the label more information. Like where it lives on the form. Are there more properties to the label, like XY coordinates, Location, etc? Are there multiple constructors on the label that could point you in the right direction?
When you do this kind of ASP.Net development, don't you usually drag and drop labels onto a UI, and then use them?

Author

Commented:
Maybe I am going about this the wrong way. That script I wrote lives at the top of my ASP.NET page and I just want those 2 labels to show up in a certain location when the web site is loaded. There is no form input.
I understand there is no form input. But when you design a web page using ASP.Net in the drag-and-drop sort of way, do you not drag and drop labels onto a form?
In the past (XML aside) how would you make the webpage say "Hello World?"

Then again, maybe you are going about this the wrong way. Can you tell me more about your environment and the website, maybe I could help you get something done quickly.
Is this Visual Studio 2005 and ASP.Net?

Author

Commented:
Static Web Site with an .aspx extension. There is not much to it, I converted out of ColdFusion ASAP so we do not have much functionality in place. All I need to do is grab 2 elements from an XML file which my script does successfully and have them written to the homepage. The script trigges when the page is loaded, I just don't know how to make these a variable I can access later down the page.

I code everything in notepad still for the most part and if I were to write Hello World I would use teh Response.Write(); method.
So then this would work eh?
Response.Write(LastTrade.Item(0).InnerText);
Response.Write(StockQuote.Item(0).InnerText);

Author

Commented:
Negative. The error I receive is  Compiler Error Message: CS0103: The name 'LastTrade' does not exist in the current context


That script lives at the top of the page and then there is some HTML and then I have the below code wrapped in an HTML table.

<% Response.Write(LastTrade.Item(0).InnerText);
Response.Write(StockQuote.Item(0).InnerText); %>

I see,  the code you posted showed it all together.
I am trying to wrap my head around doing this as a static page in notepad. I'll get back to you in a second.
Mr Lombardi,
Do this....
Add the variablea at the top of your page just above where you declared your xml doc
private string LastTrade;
private string StockQuote;
Then set them in your routine.
After your HTML add this
<% Response.Write(LastTrade.Item(0).InnerText);
Response.Write(StockQuote.Item(0).InnerText); %>
%>

Author

Commented:
It works great when it is all together as seen below, but the problem is wherever I put the script, it always show at the top of the page which is why I am trying to make variables that i can use elsewhere on the page.


protected XmlDocument oXmlDoc;
public void Page_Load(object sender, System.EventArgs e)
{
 oXmlDoc = new XmlDocument();
 try
 {
  oXmlDoc.Load("http://xml.corporate-ir.net/irxmlclient.asp?compid=119620&reqtype=quotes ");
 }
 catch(Exception ex)
 {
  Response.Write("<br>Error: <br>" + ex.Message);
 }
 XmlNode oNode = oXmlDoc.DocumentElement;
 XmlNodeList StockQuote = oNode.SelectNodes("/IRXML/StockQuotes/Stock_Quote/Trade");
 XmlNodeList LastTrade = oNode.SelectNodes("/IRXML/StockQuotes/Stock_Quote/Date");
 
Response.Write(LastTrade.Item(0).InnerText);
Response.Write(StockQuote.Item(0).InnerText);
 
 
}
 

Author

Commented:
trying now
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
SUCCESS!!!!!!!!!!!!!!!!!!!!! http://www.drs.com/index2.aspx The stock price and date are showing up perfectly! It needs to be formatted but it is showing and that is a huge win! Thank you very much.

So the logic is I should have declared the variable outside the Page_Load() method otherwise the variable would be locked in that method and not accessible via variables further down the page?
This worked for me:
Not pretty, but it works.
<HTML>
<BODY>
This is my test page<BR>
<script language=c# runat=server>


protected System.Xml.XmlDocument oXmlDoc;
private string m_ValueIWant;
public void Page_Load(object sender, System.EventArgs e)
{
 oXmlDoc = new System.Xml.XmlDocument();
 try
 {
  //oXmlDoc.Load("http://xml.corporate-ir.net/irxmlclient.asp?compid=119620&reqtype=quotes ");
    oXmlDoc.LoadXml(@"<test><DocElem>50</DocElem></test>");
 }
 catch(Exception ex)
 {
  Response.Write("<br>Error: <br>" + ex.Message);
 }
 System.Xml.XmlElement oElement = oXmlDoc.DocumentElement;

m_ValueIWant= oElement.InnerText;

}
 
</script>
<BR>This is the Value I want: <%=m_ValueIWant%>!
</BODY>
</HTML>
Yes, all variables have scope. When you declare them outside the load routine then the scope is the entire page of code. This is often called module scope. Coders often detone that with an "m".

private string m_StockQuote;

Good Luck,
Joe
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.