-1

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>
helpcaller
  • 23
  • 1
  • 6

2 Answers2

0

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;
Egres
  • 68
  • 8
  • the xml is just an example but when i get it from D:....file.xml i get \"name\" so i dont think its a problem with " – helpcaller Sep 24 '18 at 09:58
  • I tried to add get valid XML file from your example and with that it works. Tested in .NET Framework 4.6.1 and .NET Framework 4. Problem must be in the XML file you are using and not sharing. – Egres Sep 24 '18 at 10:00
0

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];
ManishM
  • 583
  • 5
  • 7