0

I have the XMLDocument and I extract the following xml tags I want using the xmlDocument.SelectSingleNode("./tag") into a string and I want to load it inside a DataTable.

I tried using the dataTable.ReadXML(); but the overloads for this function do not allow a string argument.

Is there a better way of doing this?

Edit : Adding Code

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(string_With_Xml);
DataTable accessTable = new DataTable();
accessTable.ReadXml();

I Hope this adds more context to the question.

2
  • Please show an XML itself and DataTable structure you would like to have, btw LINQ-to-XML much more easier way to do this rather than old style XmlDocument Commented Sep 11, 2012 at 11:45
  • social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/… Commented Sep 11, 2012 at 11:47

3 Answers 3

4

You can try the following:

//Your xml
string TestSTring = @"<Contacts> 
                    <Node>
                        <ID>123</ID>
                        <Name>ABC</Name>
                    </Node>
                    <Node>
                        <ID>124</ID>
                        <Name>DEF</Name>
                    </Node>
            </Contacts>";
StringReader StringStream = new StringReader(TestSTring);
DataSet ds = new DataSet();
ds.ReadXml(StringStream);
DataTable dt = ds.Tables[0];
Sign up to request clarification or add additional context in comments.

5 Comments

My only problem with this is that if there are no data values in the XML I still want the DataTable to have the column names from the Xml even though there are no rows available is there a way of doing this?
@SpaceApple, if you delete the data, for example ABC, 123, and others, you will still get a datatable with columns but no rows.
I get a XmlException saying "Root Element Missing" when I try and read the xml straight into a DataTable instead of the DataSet.
@SpaceApple, that is why you should use DataSet
Now if there is no row data the xml returns <DataSet /> as an empty tag, which I do not want, I want it to return the columns even though there is no datarow. Thats why it breaks when loading in the datatable instead of the dataset. I don't know how to overcome this
0

you can write extension like this:

public static someType ReadXml(this DataTable dt, string yourParam1, string yourParam2)
{
   method body....
}

Comments

0

You can do the following approach, the byte array here can be loaded for a string using for example:

Encoding.UTF8.GetBytes(somestring)

Helper method to load the datatable, note fallback to DataSet's method ReadXml instead of Datatable ReadXml. This will not always be suitable in case your xml contains somehow several data tables, as this method always returns the first data table in the catch:

   public DataTable Convert(byte[] bytes)
    {
        var text = bytes.ToStringUtf8();
        if (string.IsNullOrWhiteSpace(text))
        {
            return null;
        }
        using (var stream = new MemoryStream(bytes))
        {
            try
            {
                var dt = new DataTable();
                dt.ReadXml(stream);
                return dt;
            }
            catch (InvalidOperationException ie)
            {
                Trace.WriteLine(ie);
                var ds = new DataSet();
                stream.Position = 0;
                ds.ReadXml(stream);
                if (ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                return null;
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.