No, there is no appropriate answer in stack overflow yet. I am getting ArrayOfXElement in .net Core through Connected Services. But, I need to convert it into DataSet. How can I do that? I wrote the following code using all the examples I had, but I think I am playing with wrong nodes/elements. I do see schema as well as data in the nodes.
public static DataSet ToDataSet(this ArrayOfXElement xElementsForSchool)
{
DataSet dataSet = new DataSet();
foreach (XElement xElement in xElementsForSchool.Nodes)
{
string xml = xElement.ToString();
DataTable table = new DataTable();
table.ReadXml(new System.IO.StringReader(xml));
dataSet.Tables.Add(table);
}
return dataSet;
}
ArrayOfXElement consists of two Node elements. Contest on the first Node is as below
<xs:schema id="Results" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="Results" msdata:IsDataSet="true" msdata:Locale="">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="School">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" msdata:DataType="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Content of 2nd Node is as below.
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<Results xmlns="">
<School diffgr:id="School1" msdata:rowOrder="0">
<ID>83d08e0a-411d-4546-9d7f-0002ee22a3f3</ID>
<Name>Canoga Park</Name>
</School>
</Results>
</diffgr:diffgram>
Use this function
public DataSet ToDataSet(ArrayOfXElement arrayOfXElement)
{
var strSchema = arrayOfXElement.Nodes[0].ToString();
var strData = arrayOfXElement.Nodes[1].ToString();
var strXml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n\t<DataSet>";
strXml += strSchema + strData;
strXml += "</DataSet>";
DataSet ds = new DataSet("TestDataSet");
ds.ReadXml(new MemoryStream(Encoding.UTF8.GetBytes(strXml)));
return ds;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With