0

I have an SQL table with column Name:

ID -> DataType int,
PO_No -> DataType nvarchar(13), 
FLG -> DataType nchar(10)

When export to XML

<ID TYPE="3" LENGTH="4"/>
<PO_No TYPE="200" LENGTH="13"/>
<FLG TYPE="129" LENGTH="1"/>

So how to convert it? This is C# code to export the datatable to XML

for (int i = 0; i < dtm.Rows.Count; i++)
  {
   XmlElement DATAMNodex = doc.CreateElement(string.Empty, "DATA", string.Empty);                                                 
   Tbl_PO_MNode.AppendChild(DATAMNodex);
   for (int l = 0; l < dtm.Columns.Count; l++)
    {
        XmlElement POMChildNodex =                                                     doc.CreateElement(dtm.Columns[l].ToString().Trim());
        XmlAttribute MLenghtAttribute = doc.CreateAttribute("LENGHT");
        MLenghtAttribute.Value = ???;
        XmlAttribute MTypeAttribute = doc.CreateAttribute("TYPE");
        MTypeAttribute.Value = ???;                                                    POMChildNodex.Attributes.Append(MLenghtAttribute);                                                    POMChildNodex.Attributes.Append(MTypeAttribute);
        POMChildNodex.InnerText = dtm.Rows[i][l].ToString().Trim();                                                    DATAMNodex.AppendChild(POMChildNodex);
    }
  }

What is converted to XML data type that I do not know.

Ali Azam
  • 2,047
  • 1
  • 16
  • 25
Cát Tường Vy
  • 398
  • 6
  • 32
  • Just use the DataTable method dt.WriteXml("filename"); – jdweng Dec 26 '17 at 07:56
  • Possible duplicate of [How can I convert a DataTable to an XML file in C#?](https://stackoverflow.com/questions/5259759/how-can-i-convert-a-datatable-to-an-xml-file-in-c) – Arijit Mukherjee Dec 26 '17 at 08:12
  • No, I can export DataTable to XML file now, but my problem is could not output the DataType of SQL column to XML datatype . For example : SQL datatype nvarchar(13) -> output to XML datatype is TYPE="200" LENGTH="13" – Cát Tường Vy Dec 26 '17 at 08:22
  • The save schema : dt.WriteXml("filename", XmlWriteMode.WriteSchema); – jdweng Dec 26 '17 at 09:53
  • I used XMLDocument.Save(fileName); So How to get the schema? – Cát Tường Vy Dec 26 '17 at 09:58
  • I can get the SQL Table schema with this query: SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'TBL_PO_H' but I don't know how to convert it to XML datatype with format : TYPE="???" LENGHT="???". How can C# code know nvarchar type equal with 200 in XML type? – Cát Tường Vy Dec 27 '17 at 02:02

0 Answers0