Search code examples
c#.netxmlxmldocumentreadxml

C# DataSet ReadXML error: "the table xxx is already allocated as a child of another table yyy". No schema


I'm using ReadXML from DataSet to read a XML document without any schema. There are 2 'duration' tags each in a different parent tag. That is why ReadXML complaining:

System.Data.DataException: The table 'duration' is already allocated as a child of another table 'video'. Cannot set table 'asset' as parent table.

This is the XML doc it's trying to read:

http://pastebin.com/39VStzcw

(I couldn't paste XML here properly)

This is the C# code that i use:

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())  
        {  
            resultDs.ReadXml(response.GetResponseStream());  
        }  
        return resultDs;

Is there any other way I can read this XML doc?

Please enlight,

Simplecode


Solution

  • There are many ways to read XML, depends on your personal taste. Here are some examples:

    I tried reading with DataSet.ReadXml and it creates tables fine without any problem. Only difference is, I was accessing XML from local computer, not through web like you:

    const string xmlPath = @"XmlFile.xml";
    var ds = new DataSet();
    var fs = new FileStream(xmlPath, FileMode.Open);
    
    try
    {
        ds.ReadXml(fs);
        foreach (DataTable table in ds.Tables)
        {
            Console.WriteLine("Table name: " + table.TableName);
        }
        Console.WriteLine("Test");
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
    finally
    {
        fs.Close();
    }