creating xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • koti9876
    New Member
    • Mar 2008
    • 3

    creating xml file

    hi,

    I am creating xml file like this

    cn = new OleDbConnection (CONN_STRING);
    da = new OleDbDataAdapte r("select satisfactoryite mcount as ConsumerRating from inspection where Inspectorperiod Id=4147 Order by CreatedDateTime ASC", cn);
    DataSet Inspection = new DataSet("Inspec tionSummaryRepo rt");
    da.Fill(Inspect ion);
    Inspection.Writ eXml("C:\\aa.xm l");

    it working fine and i am getting the output like this

    <?xml version="1.0" standalone="yes "?>
    <InspectionSumm aryReport>
    <Table>
    <ConsumerRating >0</ConsumerRating>
    </Table>
    </InspectionSumma ryReport>

    but i want out put like

    <?xml version="1.0" standalone="yes "?>
    <InspectionSumm aryReport>
    <ConsumerRating >0</ConsumerRating>
    </InspectionSumma ryReport>

    That is i dont want table tag.Any one help me urgent plz.

    Regards,
    Koti.
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    The common misconception about datasets is that they mimic the behaviour of recordsets which only held a single table of data. DataSets now are more like an "off-line" (I say off-line, but it may be connected or disconnected) version of your relational database... what I mean to say is that the layout of your data in your DataSet is a relational model - the DataSet is a shell as it were containing your table(s) of data in a hierarchical fashion

    DataSet
    |-Table
    | |-DataRow0
    | | |-DataColumn0
    | | \-DataColumn1
    etc

    So when you export using the WriteXML method, the entire DataSet is exported to XML, including all tables, relationships, keys etc. You need to export just the relevant DataTable - The DataTable class (which can be filled using the DataAdapter in the same manner as a DataSet) has the same WriteXML method - however, this still isn't in quite the correct format, you would need an XSLT (XML Stylesheet Language Transformation) to translate the resulting XML to the format you're looking for.

    The simplest way to write the XML format you want is to create a string containing the XML and then write the string to a text file (of course, XSLT is a handy tool in your arsenal. Understanding how it works will pay dividends in the long run).

    Code:
    DataTableReader oDTR = oDS.Tables(0).CreateDataReader();
    StringBuilder sOut = new StringBuilder("<?xml version ""1.0"" standalone=""yes""?>");
    sOut.Append("<InspectionSummaryReports>");
    If(oDTR.HasRows){
      While oDTR.Read{
    	sOut.Append("<Field1>" & oDTR(0) & "</Field1>");
    	// sOut.Append("<Field2>" & oDTR(1) & "</Field2>");
    	// etc.
      }
    }
    sOut.Append("</InspectionSummaryReports>");
    System.IO.file.WriteAllText("Output.xml", sOut.ToString());

    Comment

    Working...