By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,363 Members | 3,622 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,363 IT Pros & Developers. It's quick & easy.

Write error on Read() ??

P: n/a
I'm writing a console app in c# and am encountering a strange problem. I'm
trying to use redirection of the standard input stream to read input from a
(xml) file. The following code snippet is from this app:
===============================
static void Main(string[] args)
{
if (args.Length > 0) Console.SetIn(new StreamReader(args[0]));
//executes if I don't use the "<", ">" redirection syntax when invoking

XmlTextReader xmlin = new XmlTextReader(Console.In);
xmlin.WhitespaceHandling = WhitespaceHandling.None;

if (args.Length > 1) Console.SetOut(new
StreamWriter(args[1]));//executes if I don't use the "<", ">" redirection
syntax when invoking

XmlTextWriter xmlw = new XmlTextWriter(Console.Out);
string SchemaResource = "DBDumper.DumpSpec0.xsd";
XmlValidatingReader xmlvr = new XmlValidatingReader(xmlin);
Assembly a = Assembly.GetExecutingAssembly();
Debug.Assert (a.GetManifestResourceInfo(SchemaResource) != null);
XmlTextReader xsdreader = new XmlTextReader(new
StreamReader(a.GetManifestResourceStream(SchemaRes ource)));
xmlvr.Schemas.Add(null, xsdreader);
try
{
xmlvr.Read();
....
=============================
So, assuming my app's executable is foo.exe, things work fine if I type in

foo input.xml output.xml

Also,
foo input.xml > output.xml

works fine. But, if I type in:

foo < input.xml > output.xml

I get an error on the xmlvr.Read() in the last line above, complaining that
the stream does not support a Write!!

Can anyone give me help on this? I've stared at it now for a while and don't
see the problem.

Thanks in advance,
Bill
Nov 15 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Hi Bill,

If you want to get " foo < input.xml > output.xml" work, you should
parse the command line parameter for multi-parameters.

I think you can use string.split() method to parse the command string into
an array of string, then you can apply your command line program logic.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Bill Cohagan" <bi**@teraXNOSPAMXquest.com>
| Subject: Write error on Read() ??
| Date: Sat, 27 Sep 2003 16:24:02 -0500
| Lines: 50
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#x**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups:
microsoft.public.dotnet.general,microsoft.public.d otnet.languages.csharp
| NNTP-Posting-Host: cs24313-53.austin.rr.com 24.243.13.53
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:187725
microsoft.public.dotnet.general:110132
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I'm writing a console app in c# and am encountering a strange problem. I'm
| trying to use redirection of the standard input stream to read input from
a
| (xml) file. The following code snippet is from this app:
| ===============================
| static void Main(string[] args)
| {
| if (args.Length > 0) Console.SetIn(new StreamReader(args[0]));
| //executes if I don't use the "<", ">" redirection syntax when invoking
|
| XmlTextReader xmlin = new XmlTextReader(Console.In);
| xmlin.WhitespaceHandling = WhitespaceHandling.None;
|
| if (args.Length > 1) Console.SetOut(new
| StreamWriter(args[1]));//executes if I don't use the "<", ">" redirection
| syntax when invoking
|
| XmlTextWriter xmlw = new XmlTextWriter(Console.Out);
| string SchemaResource = "DBDumper.DumpSpec0.xsd";
| XmlValidatingReader xmlvr = new XmlValidatingReader(xmlin);
| Assembly a = Assembly.GetExecutingAssembly();
| Debug.Assert (a.GetManifestResourceInfo(SchemaResource) != null);
| XmlTextReader xsdreader = new XmlTextReader(new
| StreamReader(a.GetManifestResourceStream(SchemaRes ource)));
| xmlvr.Schemas.Add(null, xsdreader);
| try
| {
| xmlvr.Read();
| ....
| =============================
| So, assuming my app's executable is foo.exe, things work fine if I type in
|
| foo input.xml output.xml
|
| Also,
| foo input.xml > output.xml
|
| works fine. But, if I type in:
|
| foo < input.xml > output.xml
|
| I get an error on the xmlvr.Read() in the last line above, complaining
that
| the stream does not support a Write!!
|
| Can anyone give me help on this? I've stared at it now for a while and
don't
| see the problem.
|
| Thanks in advance,
| Bill
|
|
|

Nov 15 '05 #2

P: n/a
Jeffrey-
Well, that's a shock! I would have thought that the framework would
provide a "transparent" way of implementing redirection of standard in,
standard out, and standard error. In particular, if I hardwire the logic
myself by parsing the command line, then my application will not work if
invoked by another application that binds these standard input/output
streams. To use a built in command as an example, I can (at a command
prompt) do:

dir foo.* > dirlist.txt

and get the result of the directory listing written to the dirlist.txt file.
I can also invoke the "dir" command from another app by using the
"startinfo" property of a process object. (See MSDN article [305994 - HOWTO:
Write a Wrapper for a Command-Line Tool with Visual C# .NET]. This is
precisely the functionality I want to have for my own application.

The approach you suggest would not support this since the code would expect
a command line containing the file names and wouldn't react as expected to
having a calling process rebind these streams. Surely MS didn't intend that
this functionality not be possible in user written console apps!!

I would also point out that my code appears to work fine for the output
stream; i.e., the STDOUT stream is "automatically" rebound to the designated
file. It is just the input stream that is a problem -- and my guess is that
the problem is somehow associated with trying to use it as an xmltextreader
(rather than just a textreader).

I'd appreciate it if you'd research this a bit further and let me know if
there's a (framework?) bug here or point me to some docs that explain why
this code doesn't work. In any case I do appreciate your quick response.

If you'd like I can simplify the code snippet a bit by eliminating the
validation portion; i.e., just use the constructed xmltextreader rather than
the validating reader when inovking the Read() method. You should still see
the error. To test this of course you'd need to create a (trivial) XML input
file to use.

Regards,
Bill

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:YU**************@cpmsftngxa06.phx.gbl...

Hi Bill,

If you want to get " foo < input.xml > output.xml" work, you should
parse the command line parameter for multi-parameters.

I think you can use string.split() method to parse the command string into
an array of string, then you can apply your command line program logic.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Bill Cohagan" <bi**@teraXNOSPAMXquest.com>
| Subject: Write error on Read() ??
| Date: Sat, 27 Sep 2003 16:24:02 -0500
| Lines: 50
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#x**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups:
microsoft.public.dotnet.general,microsoft.public.d otnet.languages.csharp
| NNTP-Posting-Host: cs24313-53.austin.rr.com 24.243.13.53
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:187725
microsoft.public.dotnet.general:110132
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I'm writing a console app in c# and am encountering a strange problem. I'm | trying to use redirection of the standard input stream to read input from a
| (xml) file. The following code snippet is from this app:
| ===============================
| static void Main(string[] args)
| {
| if (args.Length > 0) Console.SetIn(new StreamReader(args[0]));
| //executes if I don't use the "<", ">" redirection syntax when invoking
|
| XmlTextReader xmlin = new XmlTextReader(Console.In);
| xmlin.WhitespaceHandling = WhitespaceHandling.None;
|
| if (args.Length > 1) Console.SetOut(new
| StreamWriter(args[1]));//executes if I don't use the "<", ">" redirection | syntax when invoking
|
| XmlTextWriter xmlw = new XmlTextWriter(Console.Out);
| string SchemaResource = "DBDumper.DumpSpec0.xsd";
| XmlValidatingReader xmlvr = new XmlValidatingReader(xmlin);
| Assembly a = Assembly.GetExecutingAssembly();
| Debug.Assert (a.GetManifestResourceInfo(SchemaResource) != null);
| XmlTextReader xsdreader = new XmlTextReader(new
| StreamReader(a.GetManifestResourceStream(SchemaRes ource)));
| xmlvr.Schemas.Add(null, xsdreader);
| try
| {
| xmlvr.Read();
| ....
| =============================
| So, assuming my app's executable is foo.exe, things work fine if I type in |
| foo input.xml output.xml
|
| Also,
| foo input.xml > output.xml
|
| works fine. But, if I type in:
|
| foo < input.xml > output.xml
|
| I get an error on the xmlvr.Read() in the last line above, complaining
that
| the stream does not support a Write!!
|
| Can anyone give me help on this? I've stared at it now for a while and
don't
| see the problem.
|
| Thanks in advance,
| Bill
|
|
|

Nov 15 '05 #3

P: n/a
Jeffrey-

I've figured out the problem. In my case the XML file in question was
written using UTF-8 encoding -- which apparently includes some binary
"chars" at the beginning of the file. If I remove those characters then my
use of Console.In to create an xmltextreader works as expected. So, the
problem was pilot error; although I think the "Write error on Read()" error
was certainly a misleading one!

Thanks again for the response -- though I believe the suggestion to parse
the command line was a red herring. :-)

Bill

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:YU**************@cpmsftngxa06.phx.gbl...

Hi Bill,

If you want to get " foo < input.xml > output.xml" work, you should
parse the command line parameter for multi-parameters.

I think you can use string.split() method to parse the command string into
an array of string, then you can apply your command line program logic.

Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "Bill Cohagan" <bi**@teraXNOSPAMXquest.com>
| Subject: Write error on Read() ??
| Date: Sat, 27 Sep 2003 16:24:02 -0500
| Lines: 50
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#x**************@TK2MSFTNGP10.phx.gbl>
| Newsgroups:
microsoft.public.dotnet.general,microsoft.public.d otnet.languages.csharp
| NNTP-Posting-Host: cs24313-53.austin.rr.com 24.243.13.53
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:187725
microsoft.public.dotnet.general:110132
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| I'm writing a console app in c# and am encountering a strange problem. I'm | trying to use redirection of the standard input stream to read input from a
| (xml) file. The following code snippet is from this app:
| ===============================
| static void Main(string[] args)
| {
| if (args.Length > 0) Console.SetIn(new StreamReader(args[0]));
| //executes if I don't use the "<", ">" redirection syntax when invoking
|
| XmlTextReader xmlin = new XmlTextReader(Console.In);
| xmlin.WhitespaceHandling = WhitespaceHandling.None;
|
| if (args.Length > 1) Console.SetOut(new
| StreamWriter(args[1]));//executes if I don't use the "<", ">" redirection | syntax when invoking
|
| XmlTextWriter xmlw = new XmlTextWriter(Console.Out);
| string SchemaResource = "DBDumper.DumpSpec0.xsd";
| XmlValidatingReader xmlvr = new XmlValidatingReader(xmlin);
| Assembly a = Assembly.GetExecutingAssembly();
| Debug.Assert (a.GetManifestResourceInfo(SchemaResource) != null);
| XmlTextReader xsdreader = new XmlTextReader(new
| StreamReader(a.GetManifestResourceStream(SchemaRes ource)));
| xmlvr.Schemas.Add(null, xsdreader);
| try
| {
| xmlvr.Read();
| ....
| =============================
| So, assuming my app's executable is foo.exe, things work fine if I type in |
| foo input.xml output.xml
|
| Also,
| foo input.xml > output.xml
|
| works fine. But, if I type in:
|
| foo < input.xml > output.xml
|
| I get an error on the xmlvr.Read() in the last line above, complaining
that
| the stream does not support a Write!!
|
| Can anyone give me help on this? I've stared at it now for a while and
don't
| see the problem.
|
| Thanks in advance,
| Bill
|
|
|

Nov 15 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.