jack
jack

Reputation: 1

how to convert var xml to dataset or databale in net core?

after call soap serivce, I get a var data but it is xml, I want convert it into dataset or datatable

my code:

        ...
        using (WebResponse Serviceres = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream()))
            {
                //reading stream  
                var ServiceResult = rd.ReadToEnd();

                ...
            }
        }

values xml:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">    
<soap:Body>
<EVN_GET_LOCATION_3200EVN_KTSX_KDResponse xmlns="http://tempuri.org/">  
<EVN_GET_LOCATION_3200EVN_KTSX_KDResult>
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Table1"><xs:complexType><xs:sequence><xs:element name="UNIT_CODE" type="xs:string" minOccurs="0" />
      <xs:element ....

How to convert variable serviceresult into dataset or datatable

Upvotes: 0

Views: 274

Answers (1)

Jiayao
Jiayao

Reputation: 578

You can use ReadXmlMode if you want to convert xml to a dataset. The ReadXml method provides a way to read either data only, or both data and schema into a DataSet from an XML document, whereas the ReadXmlSchema method reads only the schema.

Here's a simple example:

public DataSet ReadToDataSet(string xml)
{
    var byteData = Encoding.ASCII.GetBytes(xml);
    using (MemoryStream memStream = new MemoryStream(byteData))
    {
        DataSet ds = new DataSet();
        ds.ReadXml(memStream);  
        return ds;
    }
}

Upvotes: 0

Related Questions