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

D

Drew Yallop

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
 
G

Guest

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

Jon Skeet [C# MVP]

Drew Yallop said:
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!)
 
D

Drew Yallop

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
 
J

Jon Skeet [C# MVP]

Drew Yallop said:
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.)
 

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