Saturday 15 February 2014

Convert XML to DataSet

Convert XML to DataSetyou have to use sqltypes.xsd as schemaDescriptionIf you have an XML and need to convert it to Dataset you can use this code the below method take Xml string as parameter then return .


C#
       private DataSet ConvertToDSSchema(string xmlData) 
        { 
            try 
            { 
                XDocument DatasetDoc = XDocument.Parse(xmlData); 
                XAttribute SchemaAtt = DatasetDoc.Elements().ElementAt(0).Elements().ElementAt(0).Elements().ElementAt(0).Attribute("schemaLocation"); 
                if (SchemaAtt != null) 
                    SchemaAtt.Value = ConfigurationManager.AppSettings["path"] + "\\sqltypes.xsd"; 
                DataSet DsControl = new DataSet(); 
                DsControl.ReadXml(new XmlTextReader(new StringReader(DatasetDoc.ToString())), XmlReadMode.Fragment); 
                return DsControl; 
            } 
            catch (Exception ex) 
            { 
                this.HandleException(ex); 
                return null; 
            } 
 
 
        }
Sample:click here

No comments:

Post a Comment