xml to csv convert c#

460 Views Asked by At

i am writting a c# fonction that convert xml file to csv file but i get an error which i cannot find a solution can any one help?

protected static string page_load()
{
string xmlString = System.IO.File.ReadAllText(@"D:....file.xml");
DataSet ds = new DataSet();
StringReader stringReader = new StringReader(xmlString);
ds.ReadXml(stringReader);
DataTable dtstring = new DataTable();
dtString = ds.Tables[0];
}

at line 4 : ds.ReadXml(stringReader); i'am getting "Failed to initialize the configuration system"

<?xml version=""1.0"" encoding=""utf-8"" ?> 
    <DOCUMENT> 
       <ABCS> 
         <ABC> 
           <abc_id style_sid=""-4939636236138949558"" style_code=""""/> 
           <abc item_sid=""-4939635934019714433"" upc=""30109"" use_qty_decimals=""0"" prod_cost="""" reclass_item_sid=""""/> 
           <abc_c_d> 
            <abc_c_ds no=""1"" value=""""/>
            <abc_c_ds no=""2"" value=""""/> 
            <abc_c_ds no=""3"" value=""""/> 
            <abc_c_ds no=""4"" value=""""/> 
            <abc_c_ds no=""5"" value=""""/>
            <abc_c_ds no=""6"" value=""""/> 
            <abc_c_ds no=""7"" value=""""/>
            <abc_c_ds no=""8"" value=""""/> 
            <abc_c_ds no=""9"" value=""""/> 
            <abc_c_ds no=""10"" value=""""/> 
            <abc_c_ds no=""11"" value=""""/>
            <abc_c_ds no=""12"" value=""""/> 
            <abc_c_ds no=""13"" value=""""/> 
            <abc_c_ds no=""14"" value=""""/>
          </abc_c_d>
       </ABC>
    </ABCS> 
  </DOCUMENT>
2

There are 2 best solutions below

2
On

Your XML is not a valid XML file and you need to escape double quote characters in your attributes if it is intended to be there.

How to escape double quotes in XML attributes values?

Replace:

"

with

&quot;
0
On

I tried this and its working. Problem must be with the double quotations you have used in XML to read through File reader. Check the XML

string xmlString = @"<?xml version=""1.0"" encoding=""utf-8"" ?> 
    <DOCUMENT> 
       <ABCS> 
         <ABC> 
           <abc_id style_sid=""-4939636236138949558"" style_code=""""/> 
           <abc item_sid=""-4939635934019714433"" upc=""30109"" use_qty_decimals=""0"" prod_cost="""" reclass_item_sid=""""/> 
           <abc_c_d> 
            <abc_c_ds no=""1"" value=""""/>
            <abc_c_ds no=""2"" value=""""/> 
            <abc_c_ds no=""3"" value=""""/> 
            <abc_c_ds no=""4"" value=""""/> 
            <abc_c_ds no=""5"" value=""""/>
            <abc_c_ds no=""6"" value=""""/> 
            <abc_c_ds no=""7"" value=""""/>
            <abc_c_ds no=""8"" value=""""/> 
            <abc_c_ds no=""9"" value=""""/> 
            <abc_c_ds no=""10"" value=""""/> 
            <abc_c_ds no=""11"" value=""""/>
            <abc_c_ds no=""12"" value=""""/> 
            <abc_c_ds no=""13"" value=""""/> 
            <abc_c_ds no=""14"" value=""""/>
          </abc_c_d>
       </ABC>
    </ABCS> 
  </DOCUMENT>"; 

DataSet ds = new DataSet();
StringReader stringReaders = new StringReader(xmlString);
ds.ReadXml(stringReaders);
DataTable dtstring = new DataTable();
dtstring = ds.Tables[0];

I just removed the double quotations to read from file and it works fine. Following is the new XML

<?xml version="1.0" encoding="utf-8" ?> 
    <DOCUMENT> 
       <ABCS> 
         <ABC> 
           <abc_id style_sid="-4939636236138949558" style_code=""/> 
           <abc item_sid="-4939635934019714433" upc="30109" use_qty_decimals="0" prod_cost="" reclass_item_sid=""/> 
           <abc_c_d> 
            <abc_c_ds no="1" value=""/>
            <abc_c_ds no="2" value=""/> 
            <abc_c_ds no="3" value=""/> 
            <abc_c_ds no="4" value=""/> 
            <abc_c_ds no="5" value=""/>
            <abc_c_ds no="6" value=""/> 
            <abc_c_ds no="7" value=""/>
            <abc_c_ds no="8" value=""/> 
            <abc_c_ds no="9" value=""/> 
            <abc_c_ds no="10" value=""/> 
            <abc_c_ds no="11" value=""/>
            <abc_c_ds no="12" value=""/> 
            <abc_c_ds no="13" value=""/> 
            <abc_c_ds no="14" value=""/>
          </abc_c_d>
       </ABC>
    </ABCS> 
  </DOCUMENT>

string xmlString = System.IO.File.ReadAllText(@"Path\MyFile.xml");
            DataSet ds = new DataSet();
            StringReader stringReaders = new StringReader(xmlString);
            ds.ReadXml(stringReaders);
            DataTable dtstring = new DataTable();
            dtstring = ds.Tables[0];