Solved

Query an XML document to check for a particular value for a given element name

Posted on 2013-12-03
3
445 Views
Last Modified: 2013-12-05
I have the following XML document, where I need to check if there exists an property with name = FieldName  and the value is  MTD.

<XtraSerializer version="1.0">      
    <property name="Control" iskey="true" value="Control">
         <property name="Item38" isnull="true" iskey="true">
                  <property name="SortByConditions" iskey="true" value="0" />
                  <property name="Visible">false</property>
                  <property name="SerializedSortByField" isnull="true" />
                  <property name="Caption">MTD</property>
                  <property name="FieldName">MTD</property>
                  <property name="Area">RowArea</property>
                  <property name="Name">fieldMTD</property>
                  <property name="FilterValues" isnull="true" iskey="true">
                        <property name="DeferFilterString" />
                        <property name="ValuesCore" iskey="true" value="0" />
                        <property name="Values">~Xtra#Array0, </property>
                        <property name="ShowBlanks">true</property>
                        <property name="FilterType">Excluded</property>
            </property>
      </property>
      <property name="Item4" isnull="true" iskey="true">
                  <property name="SortByConditions" iskey="true" value="0" />
                  <property name="Visible">false</property>
                  <property name="SerializedSortByField" isnull="true" />
                  <property name="Caption">ID</property>
                  <property name="FieldName">ID</property>
                  <property name="Name">fieldID</property>
                  <property name="FilterValues" isnull="true" iskey="true">
                        <property name="DeferFilterString" />
                        <property name="ValuesCore" iskey="true" value="0" />
                        <property name="Values">~Xtra#Array0, </property>
                        <property name="ShowBlanks">true</property>
                        <property name="FilterType">Excluded</property>
            </property>
      </property>
</XtraSerializer>
0
Comment
Question by:countrymeister
3 Comments
 
LVL 9

Expert Comment

by:BlueYonder
ID: 39693093
You can use LINQ2XML.  Check out http://msdn.microsoft.com/en-us/library/bb308960.aspx
0
 
LVL 1

Author Comment

by:countrymeister
ID: 39693411
Blue Yonder, Can you provide a solution
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39694559
Hi countrymeister;

The following query will return a boolean True or False Depending on whether name attribute equals FieldName and the inner text is "MTD".

// Change the Load parameter to the file path to XML document
XDocument xdoc = XDocument.Load("C:/Working Directory/xtra.xml");

// The variable existTest returns True/Fasle                
var existTest = xdoc.Descendants("property").Any(p =>  p.Attribute("name").Value == "FieldName" && p.Value == "MTD");

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now