XML file error - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

XML file error

digitalgeekdigitalgeek Posts: 59Member
I am trying to open an XML... the file was written by VB.Net using the dataset.WriteXML method

When I use the ReadXML method to open it, I get the message: The data at the root level is invalid. Line 1, Position 1.

I have open the file in IE with out any problems.

I am not using a schema file... please let me know if you think this is why.

Comments

  • rlcrlc Posts: 448Member
    I have used the DS function with the Schemea and without. What overload are you using for the ReadXML function?

    ~rlc
  • digitalgeekdigitalgeek Posts: 59Member
    : I have used the DS function with the Schemea and without. What overload are you using for the ReadXML function?
    :
    : ~rlc
    :
    dim ds as new dataset
    dim fs as IO.StreamReader("FileName.XML")
    dim xr as XML.XmlTextReader(fs)
    ds.ReadXml(xr)


  • rlcrlc Posts: 448Member
    [b][red]This message was edited by rlc at 2005-9-13 18:51:28[/red][/b][hr]
    I think the problem is you are not using "NEW" the create instances of the test reader and stream. If you just do...
    [code]ds.readxml()[/code]
    It will work but you may lose some of the formatting.. I am not sure the exact difference. I have a function that loads tables in a DB during install using the reader method I will paste a relvant snip below...

    ~rlc

    EDIT: P.S. this method assumes the XML file has the schema

    [code]
    ' load each xml file into the database
    Try

    Dim lSqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(m_ConnectString)
    Dim lCommand As SqlClient.SqlCommand
    Dim lBuilder As SqlClient.SqlCommandBuilder
    Dim lDataAdapter As SqlClient.SqlDataAdapter
    Dim lDataSet As DataSet
    Dim lCurrFileName As String
    Dim lCurrTableName As String
    Dim lXmlReader As Xml.XmlTextReader

    For Each lCurrFileName In Directory.GetFiles(lXmlPath, "*.xml")


    lCurrTableName = Path.GetFileNameWithoutExtension(lCurrFileName)
    If Not m_IsUpdate OrElse Array.IndexOf(m_UpdateData, lCurrTableName) <> -1 Then
    ' do this table
    WriteEvent("Loading data from " & lCurrFileName)
    lCommand = New SqlClient.SqlCommand("SELECT * FROM [" & lCurrTableName & "]", lSqlConnection)
    lDataAdapter = New SqlClient.SqlDataAdapter(lCommand)
    lBuilder = New SqlClient.SqlCommandBuilder(lDataAdapter)
    lDataSet = New DataSet(lCurrTableName)
    lXmlReader = New Xml.XmlTextReader(lCurrFileName)
    lDataSet.ReadXml(lXmlReader, XmlReadMode.ReadSchema)
    lDataAdapter.Update(lDataSet)
    lXmlReader.Close()
    End If
    Next

    lSqlConnection.Close()

    Catch ex As SqlClient.SqlException
    WriteEvent("Error inserting data: " & ex.Message)
    End Try
    [/code]


    : : I have used the DS function with the Schemea and without. What overload are you using for the ReadXML function?
    : :
    : : ~rlc
    : :
    : dim ds as new dataset
    : dim fs as IO.StreamReader("FileName.XML")
    : dim xr as XML.XmlTextReader(fs)
    : ds.ReadXml(xr)
    :
    :
    :


  • digitalgeekdigitalgeek Posts: 59Member
    : [b][red]This message was edited by rlc at 2005-9-13 18:51:28[/red][/b][hr]
    : I think the problem is you are not using "NEW" the create instances of the test reader and stream. If you just do...
    : [code]ds.readxml()[/code]


    I was using the "new" instance on the declaration... sorry I didn't include that in my previous message... but the above worked perfect, and is a much simpler method than having to use my own file stream and textreader... thanks for the help

    I will look closer at you code as it looks like something I may need...


Sign In or Register to comment.