Loading a datatable into a xml and xml back into a datatable

假装没事ソ 提交于 2021-01-28 05:39:45

问题


So I have a I query data into a DataTable

using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
       DataSet dataSet = new DataSet();
       adapter.Fill(dataSet, "AccessRights");

       return dataSet.Tables[0];
}

Now I start to construct the XML to send back to the client

string tableData = null;
using(StringWriter sw = new StringWriter())
{                    
      rightsTable.WriteXml(sw);
      tableData = sw.ToString();                    
}

 StringBuilder build = new StringBuilder();               
 using (XmlWriter writer = XmlWriter.Create(build, new XmlWriterSettings { OmitXmlDeclaration = true }))
 {
      writer.WriteStartElement("useraccess");
      writer.WriteCData(xmlTable.OuterXml);
      writer.WriteEndElement();
 }

Now on the Client I need to get the DataTable from the Xml

XmlCDataSection cDataNode = (XmlCDataSection)xdoc.SelectSingleNode("./mp_getindividualaccessrights_response/data/useraccess").ChildNodes[0];
string xmlDataString = cDataNode.Data.ToString();
StringReader sReader = new StringReader(xmlDataString);
string s = sReader.ToString();
DataSet ds = new DataSet();

ds.ReadXml(sReader);

return ds.Tables[0];

This works if there is data in the database which will return the columns and rows in the DataTable

My Problem comes in if there are no results in the DataBase, then I want the columns to be stored in the xml string (When dt.WriteXml()) even though there is no row data. But What happens is when dt.WriteXml() the xml string that is return is an empty tag which I do not want. Basically I want the tag to have the columns in the database query even though there are no data rows e.g.

I know for a fact the DataTable has columns (dt.Tables[0].Columns.Count) I get a number greater then 0 and when I (dt.Tables[0].Rows.Count) I get 0. Which is fine cause When I queried I did not find the results I was looking for but the columns are still present. But the moment I DataTable.WriteXML() the columns disappear.

I want the columns to be displayed in the xml string regardless or rows were found or not. But DataTable.WriteXml does not seem to be doing this correctly.

Can some one please assist me on this issue.

Edit : Added some test code to better Illustrate this issue

DataTable dt = new DataTable();
        DataColumn[] dtc = new DataColumn[] { new DataColumn("Name"), new DataColumn("Age"), new DataColumn("Surname") };
        DataSet ds = new DataSet();
        ds.Tables.Add(dt);
        dt.Columns.AddRange(dtc);

        string xmlString = null;
        using (StringWriter sw = new StringWriter())
        {
            ds.WriteXml(sw);
            xmlString = sw.ToString();
        }

        XmlDocument doc = new XmlDocument();
        StringBuilder build = new StringBuilder();
        using (XmlWriter writer = XmlWriter.Create(build, new XmlWriterSettings { OmitXmlDeclaration = true }))
        {
            writer.WriteStartElement("Root");
            writer.WriteCData(xmlString);
            writer.WriteEndElement();
        }

        XmlDocument xdoc = new XmlDocument();
        xdoc.LoadXml(build.ToString());

        XmlCDataSection cDataNode = (XmlCDataSection)xdoc.SelectSingleNode("./Root").ChildNodes[0];
        string xmlDataString = cDataNode.Data.ToString();
        StringReader sReader = new StringReader(xmlDataString);
        DataSet ds1 = new DataSet();

        ds1.ReadXml(sReader);

        Console.WriteLine(ds1.Tables[0].Columns.Count);

        Console.ReadLine();

DataSet to xml output

From the above image : This returns cause there are no rows in my DataTable, but the DataTable does have columns which is not reflected in the xml which I need. So basically I need to have the xml to look as follows

<DataSet>
   <Name></Name>
   <Age></Age>
   <Surname></Surname>
<DataSet>

But the ds.WriteXml() does not allow for this.


回答1:


This is what you need to do in order for columns to be displayed regardless if there is row data or not. The Schema will preserve the data table meta data. Just make sure there is a XmlWriteMode and XmlReadMode set to Schema

ds.WriteXml(sReader, XmlWriteMode.WriteSchema);
ds.ReadXml(sReader, XmlReadMode.ReadSchema);

This will retain all table data including Column name's and Column data type.



来源:https://stackoverflow.com/questions/12455539/loading-a-datatable-into-a-xml-and-xml-back-into-a-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!