locked
DataTable to XElement

    Question

  • I am trying to export the contents of a DataTable to XElement via the WriteXml method, and then to include that XElement into a another series of nested XElements. I have tried the following code but I am getting 'Illegal characters in path' exeception. The code I am using is

     

    Code Snippet

    StringWriter sr = new StringWriter();

    DataTable.WriteXml(sr, System.Data.XmlWriteMode.WriteSchema, true);

    string contents = sr.ToString();

     

    XElement test = XElement.Load(contents);

     

    Strangely though if I copy and paste the xml string in contents, save it as an xml file and use the file in the XElement.Load() it loads the xml data in fine!!

     

    Any help would be appreciated...

     

    Wednesday, July 11, 2007 4:30 PM

Answers

  • Probably you meant to use XElement.Parse() instead of XElement.Load(). You want to parse a string, not load a URL. Give it a try, it will work as expected.

     

    Even better, you can avoid the expensive conversion to string by providing an XmlWriter that builds an XDocument straight off the DataTable:

    Code Snippet
     XDocument d = new XDocument();
     using (XmlWriter w = d.CreateWriter()) {
       DataTable.WriteXml(w, System.Data.XmlWriteMode.WriteSchema, true);
     }
     Console.WriteLine(d.Root);

     

    If you're trying to accumulate the contents of the DataTable into a container XElement, the XmlWriter exposed by XElement is what you need. When XmlWriter.Close() is invoked, the XmlWriter generated content is Add()-ed to the container XElement:

    Code Snippet

    XElement container = new XElement("container");

    using (XmlWriter w = container.CreateWriter()) {

      DataTable.WriteXml(w, System.Data.XmlWriteMode.WriteSchema, true);
     }

    Console.WriteLine(container);

     

     

    Ion

    Wednesday, July 11, 2007 6:10 PM

All replies

  • Probably you meant to use XElement.Parse() instead of XElement.Load(). You want to parse a string, not load a URL. Give it a try, it will work as expected.

     

    Even better, you can avoid the expensive conversion to string by providing an XmlWriter that builds an XDocument straight off the DataTable:

    Code Snippet
     XDocument d = new XDocument();
     using (XmlWriter w = d.CreateWriter()) {
       DataTable.WriteXml(w, System.Data.XmlWriteMode.WriteSchema, true);
     }
     Console.WriteLine(d.Root);

     

    If you're trying to accumulate the contents of the DataTable into a container XElement, the XmlWriter exposed by XElement is what you need. When XmlWriter.Close() is invoked, the XmlWriter generated content is Add()-ed to the container XElement:

    Code Snippet

    XElement container = new XElement("container");

    using (XmlWriter w = container.CreateWriter()) {

      DataTable.WriteXml(w, System.Data.XmlWriteMode.WriteSchema, true);
     }

    Console.WriteLine(container);

     

     

    Ion

    Wednesday, July 11, 2007 6:10 PM
  • excellent, many thanks for the reply, works a treat!!
    Thursday, July 12, 2007 9:15 AM