Get InnerText of XML element from StreamReader

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?
 
H

HillBilly

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();
}
 
G

Gregory A. Beamer

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

Ignacio Machin ( .NET/ C# MVP )

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
 
H

HillBilly

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;
 
G

Gregory A. Beamer

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! |
*******************************************
 

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