none
Feature Request: Add 'ColumnMapping.XmlContent' for that DataTable.WriteXml(). RRS feed

  • Question

  • Currently, using 'ColumnMapping.Element' results in a garbled mess of escape codes when a field that contains Xml is serialized using DataTable.WriteXml().


    <?xml version="1.0" standalone="yes"?> 
    <DocumentElement> 
      <MyTable Age="34" Color="Cyan"
        <Address>&lt;root&gt;&lt;street&gt;1234 Any Steet&lt;/street&gt;&lt;city&gt;Anytown&lt;/city&gt;&lt;/root&gt;</Address> 
      </MyTable> 
      <MyTable Age="23" Color="Magenta"
        <Address>&lt;root&gt;&lt;street&gt;3456 Any Steet&lt;/street&gt;&lt;city&gt;Anycity&lt;/city&gt;&lt;/root&gt;</Address> 
      </MyTable> 
    </DocumentElement> 


    If a new 'ColumnMapping.XmlContent' were supported then it might look something like this instead....

    <?xml version="1.0" standalone="yes"?> 
    <DocumentElement> 
      <MyTable Age="34" Color="Cyan"
        <Address> 
          <root> 
            <street>1234 Any Steet</street> 
            <city>Anytown</city> 
          </root> 
        </Address> 
      </MyTable> 
      <MyTable Age="23" Color="Magenta"
        <Address> 
          <root> 
            <street>3456 Any Steet</street> 
            <city>Anycity</city> 
          </root> 
        </Address> 
      </MyTable> 
    </DocumentElement> 


    The reason I am asking for this is that I am using Excel's import Xml feature to analyze data and it quits at the <Address> level rather than being able to delve further into the <Address> node to extract the <street> and <city>.  Obviously I could write my own WriteXml() for DataTable (and corresponding ReadXml() and all the grief that entails), but I would much prefer if the framework supported it natively.

    • Changed type Chris Robinson- MSFT Wednesday, January 28, 2009 3:18 PM Because Customer request was solved
    Wednesday, January 7, 2009 9:14 PM

All replies

  • When the DataColumn.DataType == typeof(String), the DataSet has no concept that the value might actually be xml.
    Using DataType=typeof(System.Data.SqlTypes.SqlXml) instead.

    DataTable data = new DataTable("table with xml");
    data.Columns.Add("C1_SqlXml", typeof(SqlXml));
    data.Rows.Add(new object[] {
       new SqlXml(XmlReader.Create(new StringReader("<root><X>1</X><Y>2</Y></root>"), readerSettings)),
    });
    data.WriteXml(Console.Out, XmlWriteMode.IgnoreSchema);

    Using typeof(XElement) also works, but has a problem deserializing the data by duplicating the node for the column as part of the data.

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, January 7, 2009 10:38 PM
    Moderator
  • Wow...I would never have thought to try that, but it worked like a charm.  Where did you come up with/find this solution?

    I see your point that making a new mapping type of XmlContent doesn't really make sense, especially if the DataColumn is not of type 'string'. 

    Thanks a ton.

    aaron
    Thursday, January 8, 2009 9:29 AM