Read xml on file to dataset error

2.4k views Asked by At

I tried different things?

First try: Here I have this error "Data at the root level is invalid. Line 1, position 1."

StringReader StringStream = new StringReader(strFullPath);
DataSet ds = new DataSet();
ds.ReadXml(StringStream);

Second try:

 using (StreamReader mySR = new StreamReader(strFullPath,  Encoding.GetEncoding("iso-8859-1")  )) 
   {
     XmlDocument lgnXml = new XmlDocument();
     lgnXml.Load(mySR);   This line has no errors,  Only to test....

     lrs1.ReadXml(mySR,XmlReadMode.ReadSchema);  But in this line I have the error "Root element is missing."

   }

Here is the first part of the xml (structure)

    <NewDataSet>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Table" msdata:UseCurrentLocale="true" msdata:PageSize="0" msdata:CurrentPosition="0" msdata:ConnectionString="" msdata:Sort="" msdata:LoadSchemaOnly="False" msdata:Filter="" msdata:AbsolutePosition="1" msdata:SqlQuery="" msdata:AbsolutePage="0" msdata:Disconnected="False">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="Table">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="LanguageID" type="xs:int" minOccurs="0" />
                <xs:element name="Compania" type="xs:string" minOccurs="0" />
                <xs:element name="Facilidad" type="xs:string" minOccurs="0" />
                <xs:element name="Cuenta" type="xs:string" minOccurs="0" />
                <xs:element name="Numero" type="xs:string" minOccurs="0" />

And the data

<Table>
<LanguageID>2</LanguageID>
<Compania>Company</Compania>
<Facilidad>Facility</Facilidad>
<Cuenta>Account</Cuenta>
<Numero>Number</Numero>
<Nombre>Name</Nombre>

There are more many fields...

Thanks, Jose

2

There are 2 answers

0
rene On

Your first try only works if you put the XML in the StringReader constructor:

 StringReader StringStream = new StringReader(@"<Table>
<LanguageID>2</LanguageID>
<Compania>Company</Compania>
<Facilidad>Facility</Facilidad>
<Cuenta>Account</Cuenta>
<Numero>Number</Numero>
<Nombre>Name</Nombre>
</Table>");
DataSet ds = new DataSet();
ds.ReadXml(StringStream);

ds.Dump(); // LinqPad testcase, shows loaded dataset

You second sample should work if you leave out your test line

lgnXml.Load(mySR);   

because that will line will read the stream till the end. The method lrs1.ReadXml( has then no more characters to process hence the error.

0
Korvo On

Try view Xml content (not tested):

 using (StreamReader mySR = new StreamReader(strFullPath,  Encoding.GetEncoding("iso-8859-1")  )) {
     XmlDocument lgnXml = new XmlDocument();
     lgnXml.Load(mySR);//This line has no errors,  Only to test....

     while ((line = mySR.ReadLine()) != null)
     {
        Console.WriteLine(line);//"Debug"
     }
   }

Is quite likely that your xml is not coming around, or is truncated.

Note: In your xml has <Table>, but no </Table>. Has <NewDataSet>, but no </NewDataSet>