473,695 Members | 2,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17696
If you try

XmlDocument dc = new XmlDocument();
dc.Load("file.x ml");

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*******@disc ussions.microso ft.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.co m>
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.XPat h;
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.L oadXml(xmlStrin g);
XmlNodeList itemNodeList =
myXmlDocument.S electNodes(xp);
int nodeCount = itemNodeList.Co unt;
foreach (XmlNode parentNode in
itemNodeList){
Console.WriteLi ne
(parentNode.Out erXml);

}
//
// TODO: Add code to start
application here
//
}
}
}
Imports System.Data
Imports System.Xml.XmlT extReader
Imports System.Xml.XPat h
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(Cu rDir()
& "\Item.xml" )
Dim xmlString As String = MyStreamReader. ReadToEnd
MyStreamReader. Close()
MyXmlDocument.L oadXml(xmlStrin g)
Dim ItemNodeList As XmlNodeList =
MyXmlDocument.S electNodes(xp)
Dim nodeCount As Integer = ItemNodeList.Co unt
For Each xn In ItemNodeList
Console.WriteLi ne(xn.OuterXml)
' Console.WriteLi ne(xn.InnerText )

Next
Console.ReadLin e()
End Sub

End Class
-----Original Message-----
Drew Yallop <an*******@disc ussions.microso ft.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.co m>
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*******@disc ussions.microso ft.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.co m>
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
1417
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> <total_amount>0.01</total_amount> <settlement_amount>0.00</settlement_amount> <action>X</action> <response_code>9</response_code> <response_desc>This order reference has already been processed .
4
1563
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 date="...">Other content</test:info> .... I have some .net 1.1 code that reads the nodes from the file one by one: FileStream stream = new FileStream(@"file.xml", FileMode.Open);
2
1881
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 result = reader.ReadBytes((int)stream.Length); reader.Close(); return result;
2
6417
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 that for some reason when the file goes over 15k the program gets stuck. All rows are read and inserted correctly into SQL. The program is not generating duplicate entries. It is just there waiting for something
2
7531
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> <check>1</check>
9
22499
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 namespaces and classes should I use and how? -- dba123
7
6599
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 are at least 8 bits (one byte), so, I'm able to step through a file stream with a binary reader and retrive the data. The last file type, however, has me stumped at the moment. The header spec specifies the item lengths in bits. Most of the...
8
3222
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. Therefore, when process finishes, "Exited" event is raised. This executable writes a long file for over 1-5 minutes, and I, from my application must read that file while is being generated.
9
6867
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 end of it? I cant help but think there is a better way to go about this. any suggestions would be greatly appreciated. Thanks
0
8642
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9126
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8861
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7673
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4349
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3021
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2283
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.