Chong
Chong

Reputation: 604

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

Upvotes: 1

Views: 2965

Answers (3)

Ayman
Ayman

Reputation: 11

You can use some thing like this:

XmlTextReader xmlreader = new XmlTextReader(strfilename);
xmlreader.ReadToFollowing("sometext");
xmlreader.ReadToDescendant("textundersometext");

Upvotes: 1

kerrubin
kerrubin

Reputation: 1616

Some thing like that ? (where xmlTest is the string containing your XML)

        XmlDocument doc = new XmlDocument();
        // Get the Xml
        doc.LoadXml(xmlTest);
        // Get your DataSet Node
        XmlNode node = doc.SelectSingleNode("Object/DataSet");
        DataSet ds = new DataSet();
        // Read your node
        ds.ReadXml(new StringReader(node.OuterXml));
        // Read your daat
        string value = ds.Tables["TableOne"].Rows[0]["ItemID"];

Upvotes: 1

Shashi
Shashi

Reputation: 2898

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

Upvotes: 1

Related Questions