Solved

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

Posted on 2013-12-03
3
441 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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. 
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

21 Experts available now in Live!

Get 1:1 Help Now