Convert Dataset to XML

前端 未结 7 1714
野性不改
野性不改 2020-11-30 05:14

I\'ve been stuck with this problem for a few hours and can\'t seem to figure it out, so I\'m asking here :)

Alright, I\'ve got this function:

private         


        
相关标签:
7条回答
  • 2020-11-30 05:35

    Write like below code part

    DataTable dt = new DataTable("MyData");
    dt.WriteXml(@Application.StartupPath + "\\DataBaseValues.xml");
    

    Or, You can convert directly the dataSet also as said by Oded like,

    private void WriteXmlToFile(DataSet thisDataSet)
    {
       if (thisDataSet == null) 
       {
          return;
       }
    
       // Create a file name to write to.
       string filename = "myXmlDoc.xml";
    
       // Create the FileStream to write with.
       System.IO.FileStream myFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Create);
    
       // Create an XmlTextWriter with the fileStream.
       System.Xml.XmlTextWriter myXmlWriter = 
       new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
    
       // Write to the file with the WriteXml method.
       thisDataSet.WriteXml(myXmlWriter);   
       myXmlWriter.Close();
    }
    
    0 讨论(0)
  • 2020-11-30 05:36

    You can use ds.WriteXml, but that will require you to have a Stream to put the output into. If you want the output in a string, try this extension method:

    public static class Extensions
    {
        public static string ToXml(this DataSet ds)
        {
            using (var memoryStream = new MemoryStream())
            {
                using (TextWriter streamWriter = new StreamWriter(memoryStream))
                {
                    var xmlSerializer = new XmlSerializer(typeof(DataSet));
                    xmlSerializer.Serialize(streamWriter, ds);
                    return Encoding.UTF8.GetString(memoryStream.ToArray());
                }
            }
        }
    }
    

    USAGE:

    var xmlString = ds.ToXml();
    // OR
    Response.Write(ds.ToXml());
    
    0 讨论(0)
  • 2020-11-30 05:40

    if ds is your dataset..

    you can use:

    ds.getXml();
    

    this helps in getting XML

    0 讨论(0)
  • 2020-11-30 05:45

    We can use this also

     
      Private Function DsToXML(DataSet ds) as System.Xml.XmlDataDocument
    
        Dim xmlDoc As System.Xml.XmlDataDocument 
        Dim xmlDec As System.Xml.XmlDeclaration
        Dim xmlWriter As System.Xml.XmlWriter
        xmlWriter = New XmlTextWriter(context.Response.OutputStream,System.Text.Encoding.UTF8)
    
        xmlDoc = New System.Xml.XmlDataDocument(ds)
        xmlDoc.DataSet.EnforceConstraints = False
        xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing)
        xmlDoc.PrependChild(xmlDec)
        xmlDoc.WriteTo(xmlWriter)
        Retuen xmlDoc
      End Eunction
    
    0 讨论(0)
  • 2020-11-30 05:46

    Simply use Dataset.getXml():

    doc.LoadXml(ds.GetXml());
    
    0 讨论(0)
  • 2020-11-30 05:46

    Use DataSet.WriteXml - it will output the dataset as XML.

    0 讨论(0)
提交回复
热议问题