.NETGURU
Help with converting XmlTextWriter to XmlDocument in Webservice
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngxml' list.


Steve Borgwardt
Hi,
I have been trying to get a webservice to return a XmlDocument to the
client for a stock quote. However the XML feed that I am trying to
retrieve doesn't seem to be formed properly, to load it in a
XmlDocument.=20
So, I have navigated to the XmlNode that I want to start at and using
the XmlTextWriter, I create a new Xml document with the nodes that I
want.

However, when I return the XmlWriter, (using a MemoryStream) and try
loading it into a new XmlDocument, the webservice returns this error:
-------------------
System.Xml.XmlException: The root element is missing.
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader,
Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at quotesWS.nasdaq_quote.GetSingleNode(String symbol2) in
c:\documents and
settings\steveb\vswebcache\bigmomma\quotesws\nasdaq_quote.asmx.cs:line
158
-------------------------

Below is my code that generates this error.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
public class nasdaq_quote : System.Web.Services.WebService
    {
    =09
        XmlDocument myXmlDocument =3D new XmlDocument();
        XmlTextReader reader2 =3D null;
        XmlTextWriter theWriter =3D null;
        XmlNode node =3D null;
        XmlDocument theDoc =3D new XmlDocument();
        MemoryStream ms =3D new MemoryStream(); =20

[WebMethod]
    public XmlDocument GetSingleNode(string symbol2)=20
        {
            //The Path to the Yahoo Quotes Service
            string fullpath2
=3D@"http://quotes.nasdaq.com/quote.dll?page=3Dxml&mode=3Dstock&symbol=3D=
"+symbo
l2+"";=A0

            //Create a HttpWebRequest object=20
            HttpWebRequest webreq2 =3D
(HttpWebRequest)WebRequest.Create(fullpath2);
            //Get a HttpWebResponse object=20
            HttpWebResponse webresp2 =3D
(HttpWebResponse)webreq2.GetResponse();
            //Create a StreamReader object and pass the
stream as a parameter
            StreamReader strm2 =3D new
StreamReader(webresp2.GetResponseStream(), Encoding.ASCII);
                        =09

            reader2 =3D new XmlTextReader(strm2);
            =09
            myXmlDocument.Load(reader2);
        =09
            theWriter =3D new XmlTextWriter(ms,Encoding.UTF8);
            theWriter.Formatting =3D Formatting.Indented;
            theWriter.Indentation =3D 4;

            theWriter.WriteStartDocument();
            theWriter.WriteStartElement("root");

            XmlElement root =3D myXmlDocument.DocumentElement;
            foreach (XmlNode n in root.ChildNodes)
            {
                if (n.NodeType =3D=3D XmlNodeType.Element)=20
                {
=09
theWriter.WriteStartElement(n.Name);
                    foreach (XmlNode childNode in
n.ChildNodes)
                    {
                =09
=09
theWriter.WriteStartElement(childNode.Name);
=09
theWriter.WriteString(childNode.InnerText);
=09
theWriter.WriteEndElement();
                    =09
                    }
                    theWriter.WriteEndElement();
                }

            =09
            }
        =09
            theWriter.WriteEndElement();
            theWriter.WriteEndDocument();
        =09
         XmlTextReader xtr =3D new XmlTextReader(ms);
            theDoc.Load(xtr);

            theWriter.Close();
        =09

            return theDoc;
}
}

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
Any help would be greatly appreciated.
I am basically trying to just get the stock information from this
stream:
http://quotes.nasdaq.com/quote.dll?page=3Dxml&mode=3Dstock&symbol=3DMSFT

Thanks

*********************************
Don't be a dummy.=20
Go to http://www.dale-brown.com/nodummy.htm
for more information.
*********************************
Steve Borgwardt,MCSE,MCP+I
Operations Supervisor
Brown & Martin, Inc.
Ph. 262-789-1565 x103
Fax 262-789-1569
Click here to reveal e-mail address

Reply to this message...
 
    
SHEATHER,Kristoffer
Post the output of your text writer.. This should show you/us where the
problem is.

-----Original Message-----
From: Steve Borgwardt [mailto:Click here to reveal e-mail address]=20
Sent: Friday, August 16, 2002 12:58 AM
To: aspngxml
Subject: [aspngxml] Help with converting XmlTextWriter to XmlDocument in
Webservice

Hi,
I have been trying to get a webservice to return a XmlDocument to the
client for a stock quote. However the XML feed that I am trying to
retrieve doesn't seem to be formed properly, to load it in a
XmlDocument.=20
So, I have navigated to the XmlNode that I want to start at and using
the XmlTextWriter, I create a new Xml document with the nodes that I
want.

However, when I return the XmlWriter, (using a MemoryStream) and try
loading it into a new XmlDocument, the webservice returns this error:
-------------------
System.Xml.XmlException: The root element is missing.
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader,
Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at quotesWS.nasdaq_quote.GetSingleNode(String symbol2) in
c:\documents and
settings\steveb\vswebcache\bigmomma\quotesws\nasdaq_quote.asmx.cs:line
158
-------------------------

Below is my code that generates this error.
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
public class nasdaq_quote : System.Web.Services.WebService
    {
=09=09
        XmlDocument myXmlDocument =3D new XmlDocument();
        XmlTextReader reader2 =3D null;
        XmlTextWriter theWriter =3D null;
        XmlNode node =3D null;
        XmlDocument theDoc =3D new XmlDocument();
        MemoryStream ms =3D new MemoryStream();=20=20=20=20=20

[WebMethod]
    public XmlDocument GetSingleNode(string symbol2)=20
        {
            //The Path to the Yahoo Quotes Service
            string fullpath2
=3D@"http://quotes.nasdaq.com/quote.dll?page=3Dxml&mode=3Dstock&symbol=3D";+=
symbo
l2+"";

            //Create a HttpWebRequest object=20
            HttpWebRequest webreq2 =3D
(HttpWebRequest)WebRequest.Create(fullpath2);
            //Get a HttpWebResponse object=20
            HttpWebResponse webresp2 =3D
(HttpWebResponse)webreq2.GetResponse();
            //Create a StreamReader object and pass the
stream as a parameter
            StreamReader strm2 =3D new
StreamReader(webresp2.GetResponseStream(), Encoding.ASCII);
=09=09=09=09=09=09=09

            reader2 =3D new XmlTextReader(strm2);
=09=09=09=09
            myXmlDocument.Load(reader2);
=09=09=09
            theWriter =3D new XmlTextWriter(ms,Encoding.UTF8);
            theWriter.Formatting =3D Formatting.Indented;
            theWriter.Indentation =3D 4;

            theWriter.WriteStartDocument();
            theWriter.WriteStartElement("root");

            XmlElement root =3D myXmlDocument.DocumentElement;
            foreach (XmlNode n in root.ChildNodes)
            {
                if (n.NodeType =3D=3D XmlNodeType.Element)=20
                {
=09
theWriter.WriteStartElement(n.Name);
                    foreach (XmlNode childNode in
n.ChildNodes)
                    {
=09=09=09=09=09
=09
theWriter.WriteStartElement(childNode.Name);
=09
theWriter.WriteString(childNode.InnerText);
=09
theWriter.WriteEndElement();
=09=09=09=09=09=09
                    }
                    theWriter.WriteEndElement();
                }

=09=09=09=09
            }
=09=09=09
            theWriter.WriteEndElement();
            theWriter.WriteEndDocument();
=09=09=09
         XmlTextReader xtr =3D new XmlTextReader(ms);
            theDoc.Load(xtr);

            theWriter.Close();
=09=09=09

            return theDoc;
}
}

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
Any help would be greatly appreciated.
I am basically trying to just get the stock information from this
stream:
http://quotes.nasdaq.com/quote.dll?page=3Dxml&mode=3Dstock&symbol=3DMSFT

Thanks

*********************************
Don't be a dummy.=20
Go to http://www.dale-brown.com/nodummy.htm
for more information.
*********************************
Steve Borgwardt,MCSE,MCP+I
Operations Supervisor
Brown & Martin, Inc.
Ph. 262-789-1565 x103
Fax 262-789-1569
Click here to reveal e-mail address

| [aspngxml] member Click here to reveal e-mail address =3D YOUR ID=20
| http://www.asplists.com/asplists/aspngxml.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

Notice:
The information contained in this e-mail message and any attached files may
be confidential information, and may also be the subject of legal
professional privilege. If you are not the intended recipient any use,
disclosure or copying of this e-mail is unauthorised. If you have received
this e-mail in error, please notify the sender immediately by reply e-mail
and delete all copies of this transmission together with any attachments.

Reply to this message...
 
    
Steve Borgwardt
Here is the output of the MemoryStream converted to a string which =
contains the XmlTextWriter:
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D
<?xml version=3D"1.0" encoding=3D"utf-8" ?>=20
<string xmlns=3D"http://tempuri.org/";>=EF=BB=BF<?xml version=3D"1.0" =
encoding=3D"utf-8"?> <root> <equity-quote> <issue-name>JOHNSON CONTROLS =
INC</issue-name> <market-status>O</market-status> =
<market-center-code>NYSE</market-center-code> <issue-type-code>Common =
Stock</issue-type-code> <todays-high-price>84.1</todays-high-price> =
<todays-low-price>82.56</todays-low-price> =
<fifty-two-wk-high-price>93.2</fifty-two-wk-high-price> =
<fifty-two-wk-low-price>54.9</fifty-two-wk-low-price> =
<last-sale-price>83.21</last-sale-price> =
<net-change-price>-0.58</net-change-price> =
<net-change-pct>-0.69%</net-change-pct> =
<share-volume-qty>195600</share-volume-qty> =
<previous-close-price>83.79</previous-close-price> =
<best-bid-price>0</best-bid-price> <best-ask-price>0</best-ask-price> =
<best-bid-price>0</best-bid-price> <best-ask-price>0</best-ask-price> =
<current-pe-ratio>13.8683</current-pe-ratio> =
<total-outstanding-shares-qty>88768000</total-outstanding-shares-qty> =
<current-yield-pct>1.64</current-yield-pct> =
<earnings-actual-eps-amt>6</earnings-actual-eps-amt> =
<cash-dividend-amt>1.32</cash-dividend-amt> =
<cash-dividend-ex-date>20020911</cash-dividend-ex-date> =
<sp500-beta-num>0.75</sp500-beta-num> <trade-datetime>20020816 =
11:04:08</trade-datetime> =
<issuer-web-site-url>http://www.johnsoncontrols.com</issuer-web-site-url>=
<trading-status>ACTIVE</trading-status> =
<market-capitalization-amt>7386385280</market-capitalization-amt> =
</equity-quote> </root></string>=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

It appears that every element is properly formed and has the closing =
tags. The only thing I notice is the whitespace in between each element. =
I am not to familiar with XML, is this causing the problem? If so, how =
do I remove the whitespace?

Thanks

-----Original Message-----
From: SHEATHER,Kristoffer [mailto:Click here to reveal e-mail address]=20
Sent: Friday, August 16, 2002 1:10 AM
To: aspngxml
Subject: [aspngxml] RE: Help with converting XmlTextWriter to =
XmlDocument in Webservice

Post the output of your text writer.. This should show you/us where the =
problem is.

-----Original Message-----
From: Steve Borgwardt [mailto:Click here to reveal e-mail address]=20
Sent: Friday, August 16, 2002 12:58 AM
To: aspngxml
Subject: [aspngxml] Help with converting XmlTextWriter to XmlDocument in =
Webservice

Hi,
I have been trying to get a webservice to return a XmlDocument to the =
client for a stock quote. However the XML feed that I am trying to =
retrieve doesn't seem to be formed properly, to load it in a =
XmlDocument.=20
So, I have navigated to the XmlNode that I want to start at and using =
the XmlTextWriter, I create a new Xml document with the nodes that I =
want.

However, when I return the XmlWriter, (using a MemoryStream) and try =
loading it into a new XmlDocument, the webservice returns this error:
-------------------
System.Xml.XmlException: The root element is missing.
at System.Xml.XmlTextReader.Read()
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, =
Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at quotesWS.nasdaq_quote.GetSingleNode(String symbol2) in =
c:\documents and =
settings\steveb\vswebcache\bigmomma\quotesws\nasdaq_quote.asmx.cs:line
158
-------------------------

Below is my code that generates this error. =
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
public class nasdaq_quote : System.Web.Services.WebService
    {
    =09
        XmlDocument myXmlDocument =3D new XmlDocument();
        XmlTextReader reader2 =3D null;
        XmlTextWriter theWriter =3D null;
        XmlNode node =3D null;
        XmlDocument theDoc =3D new XmlDocument();
        MemoryStream ms =3D new MemoryStream(); =20

[WebMethod]
    public XmlDocument GetSingleNode(string symbol2)=20
        {
            //The Path to the Yahoo Quotes Service
            string fullpath2 =
=3D@"http://quotes.nasdaq.com/quote.dll?page=3Dxml&mode=3Dstock&symbol=3D=
"+symbo
l2+"";

            //Create a HttpWebRequest object=20
            HttpWebRequest webreq2 =3D =
(HttpWebRequest)WebRequest.Create(fullpath2);
            //Get a HttpWebResponse object=20
            HttpWebResponse webresp2 =3D (HttpWebResponse)webreq2.GetResponse();
            //Create a StreamReader object and pass the
stream as a parameter
            StreamReader strm2 =3D new StreamReader(webresp2.GetResponseStream(), =
Encoding.ASCII);
                        =09

            reader2 =3D new XmlTextReader(strm2);
            =09
            myXmlDocument.Load(reader2);
        =09
            theWriter =3D new XmlTextWriter(ms,Encoding.UTF8);
            theWriter.Formatting =3D Formatting.Indented;
            theWriter.Indentation =3D 4;

            theWriter.WriteStartDocument();
            theWriter.WriteStartElement("root");

            XmlElement root =3D myXmlDocument.DocumentElement;
            foreach (XmlNode n in root.ChildNodes)
            {
                if (n.NodeType =3D=3D XmlNodeType.Element)=20
                {
=09
theWriter.WriteStartElement(n.Name);
                    foreach (XmlNode childNode in
n.ChildNodes)
                    {
                =09
=09
theWriter.WriteStartElement(childNode.Name);
=09
theWriter.WriteString(childNode.InnerText);
=09
theWriter.WriteEndElement();
                    =09
                    }
                    theWriter.WriteEndElement();
                }

            =09
            }
        =09
            theWriter.WriteEndElement();
            theWriter.WriteEndDocument();
        =09
         XmlTextReader xtr =3D new XmlTextReader(ms);
            theDoc.Load(xtr);

            theWriter.Close();
        =09

            return theDoc;
}
}

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D
Any help would be greatly appreciated.
I am basically trying to just get the stock information from this
stream: =
http://quotes.nasdaq.com/quote.dll?page=3Dxml&mode=3Dstock&symbol=3DMSFT

Thanks

*********************************
Don't be a dummy.=20
Go to http://www.dale-brown.com/nodummy.htm
for more information.
*********************************
Steve Borgwardt,MCSE,MCP+I
Operations Supervisor
Brown & Martin, Inc.
Ph. 262-789-1565 x103
Fax 262-789-1569
Click here to reveal e-mail address

| [aspngxml] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp =3D JOIN/QUIT=20
| http://www.asplists.com/search =3D SEARCH Archives

Notice:
The information contained in this e-mail message and any attached files =
may
be confidential information, and may also be the subject of legal
professional privilege. If you are not the intended recipient any use,
disclosure or copying of this e-mail is unauthorised. If you have =
received
this e-mail in error, please notify the sender immediately by reply =
e-mail
and delete all copies of this transmission together with any =
attachments.

| [aspngxml] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp =3D JOIN/QUIT
| http://www.asplists.com/search =3D SEARCH Archives

Reply to this message...
 
 
System.IO.MemoryStream
System.IO.StreamReader
System.Net.HttpWebRequest
System.Net.HttpWebResponse
System.Net.WebRequest
System.Text.Encoding
System.Web.Services.WebService
System.Web.UI.WebControls.Xml
System.Xml.Formatting
System.Xml.XmlDocument
System.Xml.XmlElement
System.Xml.XmlException
System.Xml.XmlNode
System.Xml.XmlNodeType
System.Xml.XmlReader
System.Xml.XmlTextReader
System.Xml.XmlTextWriter
System.Xml.XmlWriter




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification