Click here to Skip to main content
Click here to Skip to main content

Tagged as

Write your own GetXml function

, 23 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to write your own GetXml function.

The DataSet object in .NET has a GetXml function. The method takes data from all the tables and from all rows and creates XML. The problem is that you cannot change this XML. So I created a method that does something similar:

private string GetXmlFromDataset(DataSet dataSet, bool isStatefull) {
    XmlTextWriter xwriter = null;
    try
    {
        xwriter = new XmlTextWriter(new MemoryStream(), Encoding.UTF8);
        xwriter.WriteStartElement("NewDataSet");
        //start enumerate in table collection
        foreach (DataTable dt in dataSet .Tables )
        {
            int rowNum = 0;
            DateTime tmp = DateTime.MinValue;
            if (isStatefull)
                dt.Columns.Add(new DataColumn("IsDummy", typeof(bool)));
            if (isStatefull && dt.Rows.Count == 0)
            {
                dt.Rows.Add(dt.NewRow());
                dt.Rows[0]["IsDummy"] = true;
            }
            //start enumerate in rows collection
            foreach (DataRow dr in dt.Rows)
            {
                //start <table> element
                xwriter.WriteStartElement(dt.TableName);
                if (isStatefull)
                {
                    xwriter.WriteAttributeString("stat", "0");
                    xwriter.WriteAttributeString("rowNum", rowNum.ToString());
                }
                DateTime tmpDate = DateTime.MinValue  ;
                //start enumerate in columns collection
                foreach (DataColumn dc in dr.Table.Columns)
                {
                    string colValue = String.Empty;
                    switch (dc.DataType.Name)
                    {
                        case "DateTime":
                            if (DateTime.TryParse(dr[dc, 
                                     DataRowVersion.Current].ToString(), 
                                     out tmpDate))
                                colValue = tmpDate.ToString("yyyy-MM-ddTHH:mm:ss");
                            else 
                                colValue = dr[dc, DataRowVersion.Current].ToString();
                            break;
                        default:
                            colValue = dr[dc, DataRowVersion.Current].ToString();
                            break;
                    }
                    xwriter.WriteStartElement(dc.ColumnName.Replace(" ","_x0020_"));
                    if ( isStatefull || String.Compare(
                           dc.DataType.Name, "string", true) != 0)
                    {
                        xwriter.WriteAttributeString("datatype", dc.DataType.Name);
                    }
                    if (isStatefull)
                    {
                        xwriter.WriteAttributeString("stat", "0");
                    }
                    xwriter.WriteString(colValue);
                    xwriter.WriteEndElement();
                }
                //end <table> element
                xwriter.WriteEndElement();
                rowNum++;
            }
        }
        //end <newdataset> element
        xwriter.WriteEndElement();
        xwriter.Flush();
        xwriter.BaseStream.Seek(0, SeekOrigin.Begin);
        StreamReader reader = new StreamReader(xwriter.BaseStream);
        return reader.ReadToEnd();
    }
    finally
    {
        if (xwriter != null)
        {
            xwriter.Close();
            xwriter = null;
        }
    }
}

You can read about in my blog: http://gregnozik.blogspot.com/2011/09/getxml-dataset-method-not-good-enough.html.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Gregory Nozik
Software Developer (Senior) Superderivatives
Israel Israel
I’m a .net architect, focusing mainly on
writing a scalable components, My expertise is designing for optimal performance and scalability. Always in search
of new knowledge and ambitions.
 
Specialties
Design Patterns, UML, OOP/OOD, .NET, C#, C++, STL, ATL, COM/COM+, VB/VB.NET, IIS, ISAPI,ASP/ASP.NET, WebServices, WCF,HTML/JavaScript, Ajax, XML/XSL/XSD, SQL
 
Personal blog gregnozik.blogspot.com

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 23 Sep 2011
Article Copyright 2011 by Gregory Nozik
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid