473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading Xml file using stream reader: different result VBNet vs. C#

I read an XML file with a stream reader in VB.Net. When I
look at the stream reader output in debug mode (by
passing cursor over the stream reader object)the format
is a perfect replica of the file as displayed when I open
the xml file in VS .net 2003 IDE.

When I perform the same procedure in C# the stream reader
obkect displays a chaotic mess. Lots of whitespace after
and "\r" and "\n" after each element. The problem is that
I cannot perform any manipulation on this file in C#
because none of the Xpath strings work after the file is
loaded into an XmlDocument object nor when the
XmlDocument is passed to an XSLT transform, i.e. Xpath
can't find any of the elements, presumably because of all
the extra stuff in the stream. I do not have this problem
with the VB.Net procedure.

Does C# handle xml files differently?

Best regards,
Drew Yallop
Nov 15 '05 #1
5 17669
If you try

XmlDocument dc = new XmlDocument();
dc.Load("file.xml");

does that load in the xml file correctly versus creating a
stream method and reading in the file 1 line at a time?

-----Original Message-----
I read an XML file with a stream reader in VB.Net. When I
look at the stream reader output in debug mode (by
passing cursor over the stream reader object)the format
is a perfect replica of the file as displayed when I open
the xml file in VS .net 2003 IDE.

When I perform the same procedure in C# the stream reader
obkect displays a chaotic mess. Lots of whitespace after
and "\r" and "\n" after each element. The problem is that
I cannot perform any manipulation on this file in C#
because none of the Xpath strings work after the file is
loaded into an XmlDocument object nor when the
XmlDocument is passed to an XSLT transform, i.e. Xpath
can't find any of the elements, presumably because of all
the extra stuff in the stream. I do not have this problem
with the VB.Net procedure.

Does C# handle xml files differently?

Best regards,
Drew Yallop
.

Nov 15 '05 #2
Drew Yallop <an*******@discussions.microsoft.com> wrote:
I read an XML file with a stream reader in VB.Net. When I
look at the stream reader output in debug mode (by
passing cursor over the stream reader object)the format
is a perfect replica of the file as displayed when I open
the xml file in VS .net 2003 IDE.

When I perform the same procedure in C# the stream reader
obkect displays a chaotic mess. Lots of whitespace after
and "\r" and "\n" after each element. The problem is that
I cannot perform any manipulation on this file in C#
because none of the Xpath strings work after the file is
loaded into an XmlDocument object nor when the
XmlDocument is passed to an XSLT transform, i.e. Xpath
can't find any of the elements, presumably because of all
the extra stuff in the stream. I do not have this problem
with the VB.Net procedure.

Does C# handle xml files differently?


It sounds like you're seeing differences in the debugger. The actual
libraries used are going to be the same, so no, C# doesn't handle XML
files differently to VB.NET, it just uses different ways of displaying
objects in the debugger. Of course, this is assuming that you really
*are* loading the XML file in the same way in both languages... could
you post the code you're using?

It sounds like the debugger in VB.NET is trimming whitespace for you,
whereas it isn't in C#. (If that's the case, I prefer the C# way - if
there's whitespace in a string, I want to see it!)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
Hi John,

Thanks for the quick reply. Here is the code:

using System;
using System.Xml;
using System.Data;
using System.Xml.XPath;
using System.Text;
using System.IO;

namespace Xmldoctest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the
application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
XmlDocument myXmlDocument = new XmlDocument();
DataSet myDS = new DataSet();
XmlNode xn;
string xp = "//SalesPrice";
StreamReader myStreamReader = new StreamReader
("item.xml");
String xmlString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myXmlDocument.LoadXml(xmlString);
XmlNodeList itemNodeList =
myXmlDocument.SelectNodes(xp);
int nodeCount = itemNodeList.Count;
foreach (XmlNode parentNode in
itemNodeList){
Console.WriteLine
(parentNode.OuterXml);

}
//
// TODO: Add code to start
application here
//
}
}
}
Imports System.Data
Imports System.Xml.XmlTextReader
Imports System.Xml.XPath
Imports System.Xml
Imports System.Text
Imports System.IO

Public Class test
Public Shared Sub Main()
Dim MyXmlDocument As XmlDocument = New XmlDocument
Dim myDS As New DataSet
Dim xn As XmlNode
Dim xp As String = "//SalesPrice"
Dim MyStreamReader As New StreamReader(CurDir()
& "\Item.xml")
Dim xmlString As String = MyStreamReader.ReadToEnd
MyStreamReader.Close()
MyXmlDocument.LoadXml(xmlString)
Dim ItemNodeList As XmlNodeList =
MyXmlDocument.SelectNodes(xp)
Dim nodeCount As Integer = ItemNodeList.Count
For Each xn In ItemNodeList
Console.WriteLine(xn.OuterXml)
' Console.WriteLine(xn.InnerText)

Next
Console.ReadLine()
End Sub

End Class
-----Original Message-----
Drew Yallop <an*******@discussions.microsoft.com> wrote:
I read an XML file with a stream reader in VB.Net. When I look at the stream reader output in debug mode (by
passing cursor over the stream reader object)the format is a perfect replica of the file as displayed when I open the xml file in VS .net 2003 IDE.

When I perform the same procedure in C# the stream reader obkect displays a chaotic mess. Lots of whitespace after and "\r" and "\n" after each element. The problem is that I cannot perform any manipulation on this file in C#
because none of the Xpath strings work after the file is loaded into an XmlDocument object nor when the
XmlDocument is passed to an XSLT transform, i.e. Xpath
can't find any of the elements, presumably because of all the extra stuff in the stream. I do not have this problem with the VB.Net procedure.

Does C# handle xml files differently?
It sounds like you're seeing differences in the

debugger. The actuallibraries used are going to be the same, so no, C# doesn't handle XMLfiles differently to VB.NET, it just uses different ways of displayingobjects in the debugger. Of course, this is assuming that you really*are* loading the XML file in the same way in both languages... couldyou post the code you're using?

It sounds like the debugger in VB.NET is trimming whitespace for you,whereas it isn't in C#. (If that's the case, I prefer the C# way - ifthere's whitespace in a string, I want to see it!)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
.

Nov 15 '05 #4
Dear Anon,

No difference using the load method.
Drew
-----Original Message-----
If you try

XmlDocument dc = new XmlDocument();
dc.Load("file.xml");

does that load in the xml file correctly versus creating astream method and reading in the file 1 line at a time?

-----Original Message-----
I read an XML file with a stream reader in VB.Net. When Ilook at the stream reader output in debug mode (by
passing cursor over the stream reader object)the format
is a perfect replica of the file as displayed when I openthe xml file in VS .net 2003 IDE.

When I perform the same procedure in C# the stream readerobkect displays a chaotic mess. Lots of whitespace afterand "\r" and "\n" after each element. The problem is thatI cannot perform any manipulation on this file in C#
because none of the Xpath strings work after the file isloaded into an XmlDocument object nor when the
XmlDocument is passed to an XSLT transform, i.e. Xpath
can't find any of the elements, presumably because of allthe extra stuff in the stream. I do not have this problemwith the VB.Net procedure.

Does C# handle xml files differently?

Best regards,
Drew Yallop
.

.

Nov 15 '05 #5
Drew Yallop <an*******@discussions.microsoft.com> wrote:
Thanks for the quick reply. Here is the code:


<snip>

Okay - well, I've compiled and run both of those and they produce the
same output for me. What does your XML file look like, and what
different output are you getting? (On the console, not in the
debugger.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jim Bayers | last post by:
When we send in credit card info, we get this in reply: <auth_resp xmlns="urn:auth"><auth_info_resp id="A1"><order_id>2XXXJl</order_id> <shopper_id></shopper_id>...
4
by: Oliver Sturm | last post by:
I have an XML file that contains fragments, meaning there's no root element. Node names are in my own "test" namespace. Looks like this: <test:info date="...">Content</test:info> <test:info...
2
by: Cool Guy | last post by:
I use the following method to read a file: static byte ReadFile(string path) { FileStream stream = new FileStream(path, FileMode.Open); BinaryReader reader = new BinaryReader(stream); byte...
2
by: Antonio Tirado | last post by:
(ASP.NET c# Question) Hi, I have this odd problem: I receive a CSV File through file upload. Using an OleDB.Datareader i read each row of the file and insert it into SQL. The problem is...
2
by: Joe | last post by:
Anyone can suggest the best method of reading XML and adding data to ListView? Here is the xml data structure:: <xml> <site> <url>http://www.yahoo.com</url> <lastupdate></lastupdate>...
9
by: dba123 | last post by:
I need some help and direction on what classes and an example or two (article) on how to read an Excel Worksheet and insert one column into a database table column. I am using .NET 2.0 only. What...
7
by: gene kelley | last post by:
I have an application where I need to read some header information found in 3 different types of file types. Two of the file types were fairly straight forward as the items to read in the header...
8
by: Lonifasiko | last post by:
Hi, Using Process class I asynchronously launch an executable (black box executable) file from my Windows application. I mean asynchronously because I've got an EventHandler for "Exited" event....
9
by: Justin Rich | last post by:
looking for the best approach to reading a real time log file. The file gets updated pretty quickly, always appended to the end. do i really need to just keep re-opening the file and reading the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.