locked
DataSet ReadXml to skip autoid RRS feed

  • Question

  • User-1129879462 posted

    I have an xml file like the below one, read the file using DataSet.ReadXML method, and returns dataset with tables with additional column Criteria_Id which is breaking the application. How can I determine a column has been automatically added? or How can I stop autogenerating such columns??

    <ReportName><Criteria><ProfRegion><![CDATA['INR']]></ProfRegion><ProfDGOffice><![CDATA[All]]></ProfDGOffice><ProfDGOrgUnit><![CDATA[All]]></ProfDGOrgUnit><Approver_txt><![CDATA[All]]></Approver_txt><PA><![CDATA[All]]></PA><TimeStatus><![CDATA[All]]></TimeStatus><ProfCategory><![CDATA[All]]></ProfCategory><ProfType><![CDATA[All]]></ProfType><Exemptstatus><![CDATA[All]]></Exemptstatus><NationalPractice><![CDATA[All]]></NationalPractice><TimePrd><![CDATA[LW]]></TimePrd><StartDate><![CDATA[3/23/2020]]></StartDate><EndDate><![CDATA[3/29/2020]]></EndDate><WebServerReportPath><![CDATA[http://urlt/]]></WebServerReportPath><Prof_txt><![CDATA[All]]></Prof_txt><RequireFollowup><![CDATA[N]]></RequireFollowup><ShowMissingTime><![CDATA[N]]></ShowMissingTime><ProfStatus><![CDATA[All]]></ProfStatus><WebServerReportPath><![CDATA[http://rurl/]]></WebServerReportPath></Criteria></ReportName>

    Tuesday, March 31, 2020 3:39 PM

All replies

  • User475983607 posted

    There is no Criteria_Id in the XML shown.  Can you share the error message or enough code to reproduce this issue?

    Tuesday, March 31, 2020 3:57 PM
  • User1535942433 posted

    Hi krisrajz,

    Accroding to your description and codes,as far as I think,when you use dataset readxml method to read the file,it will create another tabel because of the repeat node base on the dependencies of dataset.So ,I suggest you could preloaded dataset to datatable and set XmlReadMode to be auto or IgnoreSchema.

    Besides,I suggest you best to use linq to xml.Duplicate nodes are not allowed in dataset.

    More details,you could refer to below codes:

    DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                dt.Columns.Add("ProfRegion", typeof(string));
                dt.Columns.Add("ProfDGOffice", typeof(string));
                dt.Columns.Add("ProfDGOrgUnit", typeof(string));
                dt.Columns.Add("Approver_txt", typeof(string));
                dt.Columns.Add("PA", typeof(string));
                dt.Columns.Add("TimeStatus", typeof(string));
                dt.Columns.Add("ProfCategory", typeof(string));
                dt.Columns.Add("ProfType", typeof(string));
                dt.Columns.Add("Exemptstatus", typeof(string));
                dt.Columns.Add("NationalPractice", typeof(string));
                dt.Columns.Add("TimePrd", typeof(string));
                dt.Columns.Add("StartDate", typeof(string));
                dt.Columns.Add("EndDate", typeof(string));
                dt.Columns.Add("WebServerReportPath", typeof(string));
                dt.Columns.Add("Prof_txt", typeof(string));
                dt.Columns.Add("RequireFollowup", typeof(string));
                dt.Columns.Add("ShowMissingTime", typeof(string));
                dt.Columns.Add("ProfStatus", typeof(string));
    
                ds.Tables.Add(dt);
                ds.ReadXml(Server.MapPath("XMLFile4.xml"),XmlReadMode.IgnoreSchema);
    
                DataTable dt = ds.Tables[0];

    More details,you could refer to below article:

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview

    Best regards,

    Yijing Sun

    Wednesday, April 1, 2020 8:14 AM