converting text string to stream

  • Thread starter Thread starter Andrus
  • Start date Start date
A

Andrus

I have method

public void LoadXML(Stream stream);

I need to pass xml string to this method:

LoadXML( @"<?xml version=""1.0"" encoding=""utf-8""?><Test/>" )

bot got error.

How to fix ?
 
Andrus said:
I have method
public void LoadXML(Stream stream);

I need to pass xml string to this method:

LoadXML( @"<?xml version=""1.0"" encoding=""utf-8""?><Test/>" )

bot got error.

First check whether there is an overload of LoadXML taking a TextReader e.g.
public void LoadXML(TextReader reader)
as in that case you could simply do
LoadXML(new StringReader(
@"<?xml version=""1.0"" encoding=""utf-8""?><Test/>"))

If you can't do that then using a MemoryStream might help e.g.
LoadXML(new MemoryStream(Encoding.UTF8.GetBytes(
@"<?xml version=""1.0"" encoding=""utf-8""?><Test/>")))
 
Andrus said:
I have method
public void LoadXML(Stream stream);

I need to pass xml string to this method:

LoadXML( @"<?xml version=""1.0"" encoding=""utf-8""?><Test/>" )

bot got error.

How to fix ?

You can place the string into a MemoryStream and then pass that into your
method that expects a Stream:

string yourString = @"<?xml version=""1.0"" encoding=""utf-8""?><Test/>";
byte[] buf = System.Text.Encoding.UTF8.GetBytes(yourString);
MemoryStream ms = new MemoryStream(buf);
LoadXML(ms);
 
I think you can follow two method.

1. Either save the content to some file and load the stream from
StreamReader
2. Or you can use the MemoryStream

eg

MemoryStream memoryStream = new MemoryStream(BitConverter.GetBytes
(yourstring));
 
Check this out
MemoryStream yourStream = new MemoryStream( BitConverter.GetBytes
(yourstring) );
 
I have method
public void LoadXML(Stream stream);

I need to pass xml string to this method:

LoadXML( @"<?xml version=""1.0"" encoding=""utf-8""?><Test/>" )

bot got error.

How to fix ?

You've received recommendations on how to fix this. I now recommend you find
the supplier of the code and berate him for not providing an overload to
this method which accepts a string.
 
Jeff,
You've received recommendations on how to fix this. I now recommend you
find the supplier of the code and berate him for not providing an overload
to this method which accepts a string.

This is free and open source AgMenu control from devexpress.com
Loader class has only methods:

namespace DevExpress.AgMenu
{
public class AgMenuLoader : List<AgMenuItem>
{
public AgMenuLoader();
public AgMenuLoader(Stream stream);

public string Source { get; set; }

public void LoadXML(Stream stream);
}
}

which deos not provide string overload. Source parameter acceps URI, not xml
string.

Can you enter this to AgMenu bug tracking system or send compait to
devexpress since you seem to
know .net programming style better than I.

Andrus.
 
Towards,
Check this out
MemoryStream yourStream = new MemoryStream( BitConverter.GetBytes
(yourstring) );

i'm creatign Silverlight 2 application.
This causes compile error

Error 1 The best overloaded method match for
'System.BitConverter.GetBytes(bool)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'string' to 'bool'

It seems that Silverlight does not have string overload of this method.

Andrus.
 
Towards,
Check this out
MemoryStream yourStream = new MemoryStream( BitConverter.GetBytes
(yourstring) );

I'm creating Silverlight 2 application.
This causes compile error

Error 1 The best overloaded method match for
'System.BitConverter.GetBytes(bool)' has some invalid arguments
Error 2 Argument '1': cannot convert from 'string' to 'bool'

It seems that Silverlight does not have string overload of this method.

Andrus.
 
I think you should be using Encoding.Default.GetBytes() and not
BitConverter.GetBytes().
 
If Silverlight supports StringReader (as suggested by Martin), then IMHO
that's a much better solution anyway.

As I wrote to Jeff there is no method in AbMenuLoader which can take object
returned by StringReader() as parameter:

namespace DevExpress.AgMenu
{
public class AgMenuLoader : List<AgMenuItem>
{
public AgMenuLoader();
public AgMenuLoader(Stream stream);

public string Source { get; set; }

public void LoadXML(Stream stream);
}
}

Andrus.
 
Pete,
Ah, okay. Well, then Martin's other suggestion should work instead (note
the use of an explicit Encoding instance, rather than using the "Default"
property...is that supported in Silverlight?)

Yes UTF8 is supported.
However menu does not show anything.
It is better for be discuss this another issue in AgMenu forum, isn't it ?

Andrus.
 
Jeff said:
I think you should be using Encoding.Default.GetBytes() and not
BitConverter.GetBytes().

Given that it is XML with encoding UTF-8 in PI then UTF8 sounds
as a better chance than default.

Arne
 

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

Back
Top