Search code examples
c#readxml

Some Problem In Using DataSet.ReadXML


i want to use dataset.readxml for retrieve some data from xml file.But,my xml format is not well format,one part is for dataset and another part for other things.so,how i retrieve the dataset part.i am developing in c#.net 2008.xml file format is like below.

<Object type="Sample">
  <Object type="Tool">
    <Property name="Text">Prescription1</Property>
    <Property name="Name">Prescription1</Property>        
  <Object type="TextBox">    
    <Property name="Text">Singapore</Property>
    <Property name="Name">TextBox2</Property>    
  </Object>      
  <DataSet>                          //This Part
    <TableOne>      
      <ItemID>001</ItemID>
      <ItemName>Item001</ItemName>
      <Price>100</Price>      
    </TableOne>
    <TableOne>
      <ItemID>002</ItemID>
      <ItemName>Item002</ItemName>
      <Price>200</Price>      
    </TableOne>
  </DataSet>
</Object>

regards

Chong


Solution

  • as xml is not well format it is not possible to use the DOM parser. So you can the string manipulation

     string fileContent =  System.IO.File.ReadAllText(@"sample.xml");
            int firstIndex =  fileContent.IndexOf("<DataSet>", 0);
            int lastIndex = fileContent.IndexOf("</DataSet>", firstIndex);
            string data = fileContent.Substring(firstIndex + "<DataSet>".Length, lastIndex - firstIndex - "<DataSet>".Length);  
    

    Data contents the your dataset part