none
ReadXML from Stringreader

    Question

  • I have a problem reading XML data from a string. Does anybody has a idea whats wrong

     

    // store data in a file named filename

    dataSetSource.Tables["Adresse"].WriteXml(FileName, XmlWriteMode.DiffGram);

     

    // 1: read structure and data from file works fine

    string[] nsArray = new string[0];

    dataSetDestination.Clear();

    dataSetDestination.InferXmlSchema(FileName, nsArray);

    dataSetDestination.Tables["Adresse"].ReadXml(FileName);

    // 2. read data from string

    dataSetDestination.Clear();

    string MyStringData = System.IO.File.ReadAllText(FileName);

     

    // read data structure in dataSetDestination works fine

    System.IO.TextReader MyReader = new System.IO.StringReader(MyStringData);

    string[] MyArray = new string[0];

    dataSetDestination.InferXmlSchema(MyReader, MyArray);

     

    // 2.1.

    // reading the data from MyReader gets a runtime error

    //error dataSetDestination.Tables["Adresse"].ReadXml(MyReader);

     

    // 2.2.

    // reading data from the file works fine

    System.IO.File.WriteAllText(TempName, MyStringData);

    dataSetDestination.Tables["Adresse"].ReadXml(TempName);

    dataSetDestination.AcceptChanges();

     

    many thanks in advance

    best regards

    Klaus Mühlböck

     

     

     

    Tuesday, July 17, 2007 1:32 PM

Answers

  • I see two issues: first of all if you have the data in a file why are you trying to read it in a string first if you have it working by simply passing the file name to the ReadXml method? You might run into all sorts of issues with decoding the file contents properly in a string.

    The second issue is that you use the StringReader MyReader twice, once in the InferXmlSchema, then in the ReadXml method. You will need a second StringReader over the original string to ensure the ReadXml works properly I think.

    If you need more help then tell us exactly what kind of runtime error you get.

    Tuesday, July 17, 2007 2:30 PM

All replies

  • I see two issues: first of all if you have the data in a file why are you trying to read it in a string first if you have it working by simply passing the file name to the ReadXml method? You might run into all sorts of issues with decoding the file contents properly in a string.

    The second issue is that you use the StringReader MyReader twice, once in the InferXmlSchema, then in the ReadXml method. You will need a second StringReader over the original string to ensure the ReadXml works properly I think.

    If you need more help then tell us exactly what kind of runtime error you get.

    Tuesday, July 17, 2007 2:30 PM
  • Many thanks for our reply. In my real application i get the data from a webservice in a string. The problem was, that i used the stringreader twice. I add a second one and now its fine.

    Thursday, July 26, 2007 12:23 PM