How can I save dataset as an xml file???

Hi

I am trying to save the dataset as xml file (one row from the table).Saving xml is fine but it look like the following (the below is flat xml file without root nodes and child node)

But I want the data to go under xml Root Node, child node, children node and so on.

My question is How can I save the dataset as xml file with Root Node, child node, children node and so on Please help !!! Urgent!!

< xml version="1.0" standalone="yes" >
<MS>
<SDLTID>1</SDLTID>
<UTRN>303783jMJ</UTRN>
<TransPropertyType>01</TransPropertyType>
<TransInterestCreated>FP</TransInterestCreated>
<TransEffectiveDate>2006-04-05</TransEffectiveDate>
and so on...........
</MS>


Advance Thanks




Answer this question

How can I save dataset as an xml file???

  • shahrul

    Well, if you can combine all the fields from 4 tables into 1 table, then it stands to reason that each of these tables are related... so: use relationships.

    In your DataSet, define the relationships between each table. Let's say "SDR" is the primary table. You would then add a relation between the SDR table and each other child table. Once that is defined, you can modify the current XML writting loop. In the For-Each loop, the datarow variable will give you access to each child row defined in relationships. So it would be a simple matter of writing additional XML elements for each field in each child row of the current row.



  • Kevin8264

    Hi
    Can you please give some syntex, then I can build up from there. Also I have XML schema
    Will that be helpful for my needs
    Thank you


  • Maciej Koper

    Hi

    I have done the relation ship to the 4th table (Colum name in 4th table-TotalConsiderationStock1). IT works only after I drag to the form and use the following code (Higlighted in green). I couldn't use any If statement or row.IsTotalConsiderationStock1Null.

    xdc.WriteElementString("test1", TotalConsiderationStock1TextBox.Text)

    Am I doing any mistakes



  • Jim Perry

    Here is a routine that saves a DataTable (designed as you described) to an XML file (also as you described).

    Private Sub SaveXML(ByVal dt As MainDataSet.SDRDataTable, ByVal file As String)
    Dim xdc As New System.Xml.XmlTextWriter(file, Nothing)
    xdc.WriteStartDocument(True)
    xdc.WriteStartElement("Transportation")
    For Each row As MainDataSet.SDRRow In dt.Rows
    If Not row.IsBusNull Then
    xdc.WriteStartElement("Heavyvehicle")
    xdc.WriteElementString("Bus", row.Bus)
    xdc.WriteEndElement()
    End If
    If Not row.IsCarNull Then
    xdc.WriteStartElement("Lightvehicle")
    xdc.WriteElementString("Car", row.Car)
    xdc.WriteEndElement()
    End If
    If Not row.IsLorryNull Then
    xdc.WriteElementString("Lorry", row.Lorry)
    End If
    If Not row.IsCycleNull Then
    xdc.WriteElementString("Cycle", row.Cycle)
    End If
    Next
    xdc.WriteEndElement()
    xdc.WriteEndDocument()
    xdc.Close()

    End Sub

    There's also a link to an example project.

    That should give you what you're asking for. You should be able to work the rest out from here.

    Good Luck.



  • cmsmith81

    Hi
    I couldn't get your answer correctly also I am new for programming. CAN YOU PLEASE my first question and give some advise.

    Advance thanks


  • BALA SINGAM - My

    Hi
    You are right. I have only one table and one row at the moment. I followed your link and going to try them. Before that please advise the following.

    1. For example If the field in the table is empty or null, the xml file shouldn't contain empty element like <test1></test1> or <test1/>

    Can you give the syntex which is close to my needs.

    The database scenario is Tablename - SDR
    Column1 - Bus = FG
    Column2 - Car = 5
    Colum3 - Lorry = null
    Colum 4 - Cycle = Available


    The Xml file should be the one below (FIELD: Lorry not in the xml file as it has NO Value). This is the way I want. Sorry for pestering it may easy for you difficult for me.


    xml version="1.0" standalone="yes" >
    <Transportation>
    <Heavyvehicle>
    <Bus>FG</Bus>
    </Heavyvehicle>
    <Lightvehicle>
    <Car>5</Car>
    </Lightvehicle>
    <Cycle>Available</Cycle>
    </Transportation>

    The below is xml file I am getting now

    < xml version="1.0" standalone="yes" >
    <MS>
    <MS>
    <SDLTID>1</SDLTID>
    <UTRN>303783jMJ</UTRN>
    <TransPropertyType>01</TransPropertyType>
    <TransInterestCreated>FP</TransInterestCreated>
    <TransEffectiveDate>2006-04-05</TransEffectiveDate>
    <TransRestrictionsAffectingApply>no</TransRestrictionsAffectingApply>
    <TransRestrictionsDetails>test</TransRestrictionsDetails>
    <TransContractDate>2006-03-24</TransContractDate>
    <TransLandExchanged>no</TransLandExchanged>
    <TransLandExchangedPostCode>IG1 3Rd</TransLandExchangedPostCode>
    <TransPursuantToOption>no</TransPursuantToOption>
    <TaxClaimingRelief>no</TaxClaimingRelief>
    <TaxTotalConsideration>390000</TaxTotalConsideration>
    <TaxTotalConsiderationVAT>390000</TaxTotalConsiderationVAT>
    <TaxFormCode1>30</TaxFormCode1>
    <TaxLinkedTransaction>no</TaxLinkedTransaction>
    <TaxTotalDue>11700</TaxTotalDue>
    <TaxAmountPaid>11700</TaxAmountPaid>
    <TaxAmountPaidIncludesPenalties>no</TaxAmountPaidIncludesPenalties>
    <LandNoProperties>1</LandNoProperties>
    <LandCertificate>no</LandCertificate>
    <LandPostcode>SA19 7LY</LandPostcode>
    <LandAddress1>Gors Farm</LandAddress1>
    <LandAddress2>Salem</LandAddress2>
    <LandAddress3>Llandeilo</LandAddress3>
    <LandAddress4>Carmarthenshire</LandAddress4>
    <MadeUnlocked>false</MadeUnlocked>
    </MS>
    </MS>








  • Annihil8

    Hi Kimble

    Thank you very, very much. Thank you very much..................

    Bye for now


  • Darren Baldwin

    This discussion already spread between different threads.

    See http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=789398&SiteID=1



  • sharpMoon

    Ok, those requirements are specific enough that you'll need to write the file yourself.

    The links provided should get you started. You can also search these forums and the MSDN library for XMLDocument as well as XMLWriter.

    Either of those classes can be used to compose the document.



  • Sumit_Dagar_8eba6d

    Once you've created the relationship (and build the project), your SDRRow object should now have a method Get4thTableRows() (where 4thTable is the name of the 4th table). So in the loop you'll create an array of 4thTableRow and call the get method. Then, assuming there is only one matching child row, you'll continue the if-then statements using this new row.

    Private Sub SaveXML(ByVal dt As MainDataSet.SDRDataTable, ByVal file As String)
    Dim xdc As New System.Xml.XmlTextWriter(file, Nothing)
    xdc.WriteStartDocument(True)
    xdc.WriteStartElement("Transportation")
    For Each row As MainDataSet.SDRRow In dt.Rows
    If Not row.IsBusNull Then
    xdc.WriteStartElement("Heavyvehicle")
    xdc.WriteElementString("Bus", row.Bus)
    xdc.WriteEndElement()
    End If
    If Not row.IsCarNull Then
    xdc.WriteStartElement("Lightvehicle")
    xdc.WriteElementString("Car", row.Car)
    xdc.WriteEndElement()
    End If
    If Not row.IsLorryNull Then
    xdc.WriteElementString("Lorry", row.Lorry)
    End If
    If Not row.IsCycleNull Then
    xdc.WriteElementString("Cycle", row.Cycle)
    End If

    Dim tcsRows() As MainDataSet.4thTableRow
    tcsRows = row.Get4thTableRows

    If tcsRows.Length >0 Then
    If Not tcsRows(0).IsSomeColumnNull Then
    xdc.WriteElementString("test1", tcsRows(0).SomeColumn
    End If
    End If


    Next
    xdc.WriteEndElement()
    xdc.WriteEndDocument()
    xdc.Close()

    End Sub



  • Parker Lewis

    I don't get your question.

    Calling WriteXML and specifing IgnoreSchema results in a simple XML file of the dataset data. The root node is the dataset name, each table then has a child node for each row in the table and each of those child rows has children for each column in the table.

    What else do you want



  • Carroll Barrett

    hi

    1. I have table and picked up one row with 70 colums as dataset
    2. I used Dataset.writexml("..\test.xml")
    3. All 70 colums writtern as xml but under one root element.(like below)

    <root>
    <1>gh</1>
    <2>fg</2>
    <3>fg</4>
    <4>fg</4>
    <5>fg</5>
    </root>

    4. I want the xml as below
    <root>
    <test1>
    <1>gh</1>
    <2>fg</2>
    </test1>
    <test2>
    <3>fg</4>
    <test3>
    <4>fg</4>
    </test3>
    <5>fg</5>
    </test2>
    </root>

    advance thanks


  • Namza

    Hi Kimple

    Your codes work very well. I have another following question.

    1. I have created a dataset and added 3 table to the table adpter (i.e. I have gathered all columns from three table and made as one single Datatable) - its works fine

    So I have tried to add 4th table with 30 colums to make one single Datatable, so I can have single xml file. But I have ended up toomany fields error.

    My question is : How do I add another table or do something to save the data from all four table as a single xml file

    Many thanks


  • SenthilNathan

    First, you say "I have table and picked up one row with 70 colums as dataset" but that doesn't make sense because a DataSet does not contain DataRow objects. So the DataSet must have at least one DataTable and that DataTable contains the row.

    When the XML is written by the dataset, it gets the format:

    <DataSetName (Root)>
    <TableName (Child of Root)>
    <TableColumn1Name (Child of TableName)>Row value</TableColumn1Name>
    <TableColumn2Name>...
    etc.

    Now, you could use the .WriteXML method of the DataTable object instead of the DataSet - that may get you closer to what you want.

    But if that's still no good, just write the XML file yourself. Here are some links:
    http://msdn2.microsoft.com/en-us/library/tx3wa6ka.aspx
    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=424170&SiteID=1&PageID=1

    HTH



  • How can I save dataset as an xml file???