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
.