none
How to query xml data when loaded into datatable RRS feed

  • Question

  • this is my sample xml. see it first please

    <?xml version = "1.0" encoding = "utf-8"?>
    <comment>
       <commenttext>blah blah blah</commenttext>
       <ignoredata>true</ignoredata>
       <ignorevalue>100</ignorevalue>
    </comment>
    <?xml version = "1.0" encoding = "utf-8"?>
    <comment>
       <commenttext>blah1 blah1 blah1</commenttext>
       <ignoredata>true</ignoredata>
    </comment>
    <comment>
       <commenttext>blah2 blah2 blah2</commenttext>
       <ignoredata>false</ignoredata>
    </comment>
    <comment>
       <commenttext>blah3 blah3 blah3</commenttext>
       <ignoredata>true</ignoredata>
    </comment>
    <comment>
       <commenttext>blah4 blah4 blah4</commenttext>
       <ignoredata>true</ignoredata>
       <ignorevalue/>
    </comment>
    <comment>
       <commenttext>blah5 blah5 blah5</commenttext>
       <ignoredata>true</ignoredata>
       <ignorevalue>201</ignorevalue>
    </comment>

    i have two questions on above xml.

    1) how to determine how many records has ignoredata == true and ignorevalue element is missing ?

    2) i will convert my the above datatable to list and after that how could i iterate only in those records which has ignoredata == true and ignorevalue element is missing to update ignorevalue with hard code value like -1

    in my abobe xml some time ignoredata has true value but there is no ignorevalue element in that records and some time again ignoredata has true value but there is empty tag of ignorevalue 

    please share code example for my above 2 questions. thanks

    Saturday, March 16, 2019 7:59 PM

All replies

  • That is not valid XML.  A valid XML file has a very strict format: there is only has one <?xml..?> entry at the very beginning, and there must be one root entry that contains all of the others.  So, you could have something like this:

    <?xml version = "1.0" encoding = "utf-8"?>
    <comments>
       <comment>
          <commenttext>blah blah blah</commenttext>
          <ignoredata>true</ignoredata>
          <ignorevalue>100</ignorevalue>
       </comment>
       <comment>
          <commenttext>blah1 blah1 blah1</commenttext>
          <ignoredata>true</ignoredata>
       </comment>
       <comment>
          <commenttext>blah2 blah2 blah2</commenttext>
          <ignoredata>false</ignoredata>
       </comment>
    </comments>

    The answers to your other questions depend on which XML package you will be using.  Have you chosen one yet?  With most, you'll iterate through the <comment> records, and you can count them in whatever way you need to.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Sunday, March 17, 2019 6:46 AM
  • yes you are right. my questions are

    1) how to determine how many records has ignoredata == true and ignorevalue element is missing in record ?

    2) i will convert my the above datatable to list and after that how could i iterate only in those records which has ignoredata == true and ignorevalue element is missing to update ignorevalue with hard code value like -1

    in my abobe xml some time ignoredata has true value but there is no ignorevalue element in that records and some time again ignoredata has true value but there is empty tag of ignorevalue 

    Monday, March 18, 2019 7:51 AM
  • this is right xml

    <?xml version = "1.0" encoding = "utf-8"?>
    <comments>
       <comment>
          <commenttext>blah blah blah</commenttext>
          <ignoredata>true</ignoredata>
          <ignorevalue>100</ignorevalue>
       </comment>
       <comment>
          <commenttext>blah1 blah1 blah1</commenttext>
          <ignoredata>true</ignoredata>
       </comment>
       <comment>
          <commenttext>blah2 blah2 blah2</commenttext>
          <ignoredata>false</ignoredata>
       </comment>
    </comments>

    i have loaded the above xml into dataset and after that i need to compose a query which tell me how many records are there in dataset which has ignoredata true value and ignorevalue element missing in records or ignorevalue is there as empty tag in records like <ignorevalue/>

    if my question is not clear then please let me know so i can try to detail it. if you understand then please share right code. i am not working with xelement class XElement.Parse(input);

    Monday, March 18, 2019 7:42 PM
  • Try something like:

    var doc  = new XmlDocument();
    doc.Load("fileName.xml");
    doc.SelectNodes("//Comments/Comment").Count

    Monday, March 18, 2019 8:20 PM