Click here to Skip to main content
15,880,972 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have sample XML like this :

XML
<?xml version="1.0"?>
<methodResponse>
    <params>
        <param>
            <value>
                <array>
                    <data>
                        <value>
                            <string>PLNPASC</string>
                        </value>
                        <value>
                            <string>20160907222141</string>
                        </value>
                        <value>
                            <string>538731734541</string>
                        </value>
                        <value>
                            <string></string>
                        </value>
                        <value>
                            <string></string>
                        </value>
                        <value>
                            <string>887849</string>
                        </value>
                        <value>
                            <string>12000</string>
                        </value>
                    </data>
                </array>
            </value>
        </param>
    </params>
</methodResponse>




and i want get value with null value in data array without xml dataReader

Hope you help

What I have tried:

I try some method but no result....
Posted
Updated 7-Sep-16 8:28am
v2
Comments
xszaboj 7-Sep-16 11:50am    
What is the reason not to use XML dataReader? Can you use XDocument?
ulungss 7-Sep-16 12:33pm    
I assume the manual way would be faster in retrieving data in XML, if you open datareader use reflector, which is long and convoluted code. I need the speed of transaction data.
Maciej Los 7-Sep-16 13:21pm    
What you mean by "i want get value with null value in data array"?
Bernhard Hiller 8-Sep-16 3:09am    
Who created such an abomination of XML? All properties simply defined as value/string, while they seem to have some meaning; e.g. second value seems to be a DateTime. I guess the order of items is relevant...

Not sure why you don't want to use xml data reader so i'm using XDocument and linq to xml.

This code will show all value XML elements where the thing is null or empty.

C#
var myxml = @"<?xml version='1.0'?>
<methodResponse>
    <params>
        <param>
            <value>
                <array>
                    <data>
                        <value>
                            <string>PLNPASC</string>
                        </value>
                        <value>
                            <string>20160907222141</string>
                        </value>
                        <value>
                            <string>538731734541</string>
                        </value>
                        <value>
                            <string></string>
                        </value>
                        <value>
                            <string></string>
                        </value>
                        <value>
                            <string>887849</string>
                        </value>
                        <value>
                            <string>12000</string>
                        </value>
                    </data>
                </array>
            </value>
        </param>
    </params>
</methodResponse>";

            XDocument xdoc = XDocument.Parse(myxml);


            var elements = xdoc.Descendants(XName.Get("value"));

            foreach (XElement el in elements.Where(m => string.IsNullOrEmpty(m.Value)))
            {
                Console.WriteLine("el.Name: " + el.Name);
                Console.WriteLine("el.Value" + el.Value);
            }
 
Share this answer
 
Comments
Maciej Los 7-Sep-16 13:27pm    
Have you tested your solution? What happens if you use Where(m =>!string.IsNullOrEmpty(m.Value))?
David_Wimbley 7-Sep-16 14:17pm    
I did run it prior to posting. The output provided using that Where clause is below.

el.Name: value
el.Value
el.Name: value
el.Value

I may have misunderstood what OP is asking so if you want all values that are not null, remove the exclamation point (note for OP).
Please, read my comment to the question...

If you would like to get data from that path: methodResponse->params->param->value->array->data->value->string, you can use Linq To Xml[^] with that query:

C#
var result = xdoc.Descendants("string").Where(m =(string)m.Value==string.Empty);

Above query returns:
<string></string>
<string></string>


C#
var result = xdoc.Descendants("string").Where(m =(string)m.Value!=string.Empty);

Result:
<string>PLNPASC</string>
<string>20160907222141</string>
<string>538731734541</string>
<string>887849</string>
<string>12000</string>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900