Get InnerText of XML element from StreamReader

  • Thread starter Thread starter HillBilly
  • Start date Start date
H

HillBilly

I am at my wits end. I can get an XML element returned to me from a
StreamReader:

string responseString = streamReader.ReadToEnd();

The responseString string variable will contain a single XML element:

<friends>[true|false]</friends>

There is no Root element and I cannot figure out which class to use to read
the true or false InnerText property and then teturn that value to the
caller. WTF?
 
Interesting solution. That will do thank you.

Family Tree Mike said:
HillBilly said:
I am at my wits end. I can get an XML element returned to me from a
StreamReader:

string responseString = streamReader.ReadToEnd();

The responseString string variable will contain a single XML element:

<friends>[true|false]</friends>

There is no Root element and I cannot figure out which class to use to
read the true or false InnerText property and then teturn that value to
the caller. WTF?


This may not be the best way, but it works...

static void Main(string[] args)
{
string xml = "<friends>true</friends>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
bool wtf;
bool.TryParse(doc.DocumentElement.InnerText, out wtf);
Console.WriteLine(wtf);
Console.ReadLine();
}
 
HillBilly said:
I am at my wits end. I can get an XML element returned to me from a
StreamReader:

string responseString = streamReader.ReadToEnd();

The responseString string variable will contain a single XML element:

<friends>[true|false]</friends>

There is no Root element and I cannot figure out which class to use to
read the true or false InnerText property and then teturn that value to
the caller. WTF?


This may not be the best way, but it works...

static void Main(string[] args)
{
string xml = "<friends>true</friends>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
bool wtf;
bool.TryParse(doc.DocumentElement.InnerText, out wtf);
Console.WriteLine(wtf);
Console.ReadLine();
}


You can also read directly from the stream, if you are streaming it, into
the Xml Document:
http://snurl.com/k8bbh

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
I am at my wits end. I can get an XML element returned to me from a
StreamReader:
string responseString = streamReader.ReadToEnd();
The responseString string variable will contain a single XML element:
<friends>[true|false]</friends>

There is no Root element and I cannot figure out which class to use to
read the true or false InnerText property and then teturn that value to
the caller. WTF?

This may not be the best way, but it works...

        static void Main(string[] args)
        {
            string xml = "<friends>true</friends>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            bool wtf;
            bool.TryParse(doc.DocumentElement.InnerText, out wtf);
            Console.WriteLine(wtf);
            Console.ReadLine();
        }

Hi,

You have to create a XmlDocument , StreamReader has no knowledge of
the content of the stream, it just return a string, you need a Xml
parser to get a document you can query
 
Gregory A. Beamer said:
HillBilly said:
I am at my wits end. I can get an XML element returned to me from a
StreamReader:

string responseString = streamReader.ReadToEnd();

The responseString string variable will contain a single XML element:

<friends>[true|false]</friends>

There is no Root element and I cannot figure out which class to use to
read the true or false InnerText property and then teturn that value to
the caller. WTF?


This may not be the best way, but it works...

static void Main(string[] args)
{
string xml = "<friends>true</friends>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
bool wtf;
bool.TryParse(doc.DocumentElement.InnerText, out wtf);
Console.WriteLine(wtf);
Console.ReadLine();
}


You can also read directly from the stream, if you are streaming it, into
the Xml Document:
http://snurl.com/k8bbh

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************

When I tried to pass the Stream to the Load method I kept getting an
XmlException error probably because I have the following code wrong...

Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);

XmlDocument doc = new XmlDocument();
doc.Load(responseStream);
string result = doc.InnerText;
 
When I tried to pass the Stream to the Load method I kept getting an
XmlException error probably because I have the following code wrong...

Stream responseStream = response.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);

XmlDocument doc = new XmlDocument();
doc.Load(responseStream);
string result = doc.InnerText;


Try the suggestions here:
http://snurl.com/knk2d


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top