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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Drew Yallop

    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
  • Guest's Avatar

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

    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?

    [color=blue]
    >-----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
    >.
    >[/color]

    Comment

    • Jon Skeet [C# MVP]

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

      Drew Yallop <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
      > 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?[/color]

      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 - <skeet@pobox.co m>
      Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

      If replying to the group, please do not mail me too

      Comment

      • Drew Yallop

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

        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[color=blue]
        >-----Original Message-----
        >Drew Yallop <anonymous@disc ussions.microso ft.com> wrote:[color=green]
        >> I read an XML file with a stream reader in VB.Net.[/color][/color]
        When I[color=blue][color=green]
        >> look at the stream reader output in debug mode (by
        >> passing cursor over the stream reader object)the[/color][/color]
        format[color=blue][color=green]
        >> is a perfect replica of the file as displayed when I[/color][/color]
        open[color=blue][color=green]
        >> the xml file in VS .net 2003 IDE.
        >>
        >> When I perform the same procedure in C# the stream[/color][/color]
        reader[color=blue][color=green]
        >> obkect displays a chaotic mess. Lots of whitespace[/color][/color]
        after[color=blue][color=green]
        >> and "\r" and "\n" after each element. The problem is[/color][/color]
        that[color=blue][color=green]
        >> I cannot perform any manipulation on this file in C#
        >> because none of the Xpath strings work after the file[/color][/color]
        is[color=blue][color=green]
        >> 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[/color][/color]
        all[color=blue][color=green]
        >> the extra stuff in the stream. I do not have this[/color][/color]
        problem[color=blue][color=green]
        >> with the VB.Net procedure.
        >>
        >> Does C# handle xml files differently?[/color]
        >
        >It sounds like you're seeing differences in the[/color]
        debugger. The actual[color=blue]
        >libraries used are going to be the same, so no, C#[/color]
        doesn't handle XML[color=blue]
        >files differently to VB.NET, it just uses different ways[/color]
        of displaying[color=blue]
        >objects in the debugger. Of course, this is assuming[/color]
        that you really[color=blue]
        >*are* loading the XML file in the same way in both[/color]
        languages... could[color=blue]
        >you post the code you're using?
        >
        >It sounds like the debugger in VB.NET is trimming[/color]
        whitespace for you,[color=blue]
        >whereas it isn't in C#. (If that's the case, I prefer[/color]
        the C# way - if[color=blue]
        >there's whitespace in a string, I want to see it!)
        >
        >--
        >Jon Skeet - <skeet@pobox.co m>
        >http://www.pobox.com/~skeet
        >If replying to the group, please do not mail me too
        >.
        >[/color]

        Comment

        • Drew Yallop

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

          Dear Anon,

          No difference using the load method.


          Drew[color=blue]
          >-----Original Message-----
          >If you try
          >
          >XmlDocument dc = new XmlDocument();
          >dc.Load("file. xml");
          >
          >does that load in the xml file correctly versus creating[/color]
          a[color=blue]
          >stream method and reading in the file 1 line at a time?
          >
          >[color=green]
          >>-----Original Message-----
          >>I read an XML file with a stream reader in VB.Net. When[/color][/color]
          I[color=blue][color=green]
          >>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[/color][/color]
          open[color=blue][color=green]
          >>the xml file in VS .net 2003 IDE.
          >>
          >>When I perform the same procedure in C# the stream[/color][/color]
          reader[color=blue][color=green]
          >>obkect displays a chaotic mess. Lots of whitespace[/color][/color]
          after[color=blue][color=green]
          >>and "\r" and "\n" after each element. The problem is[/color][/color]
          that[color=blue][color=green]
          >>I cannot perform any manipulation on this file in C#
          >>because none of the Xpath strings work after the file[/color][/color]
          is[color=blue][color=green]
          >>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[/color][/color]
          all[color=blue][color=green]
          >>the extra stuff in the stream. I do not have this[/color][/color]
          problem[color=blue][color=green]
          >>with the VB.Net procedure.
          >>
          >>Does C# handle xml files differently?
          >>
          >>Best regards,
          >>Drew Yallop
          >>.
          >>[/color]
          >.
          >[/color]

          Comment

          • Jon Skeet [C# MVP]

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

            Drew Yallop <anonymous@disc ussions.microso ft.com> wrote:[color=blue]
            > Thanks for the quick reply. Here is the code:[/color]

            <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 - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            Working...