Solved

Query an xml file

Posted on 2014-01-17
3
588 Views
Last Modified: 2014-01-22
I have an xml file, that consists of the following structure.

<XtraSerializer version="1.0" application="PivotGridControl">
  <property name="#LayoutVersion" isnull="true" />
  <property name="$PivotGridControl" iskey="true" value="PivotGridControl">
    <property name="Fields" iskey="true" value="41">
      <property name="Item1" isnull="true" iskey="true">
        <property name="AreaIndex">0</property>
        <property name="Name">fieldCode</property>
        <property name="Area">RowArea</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="Item2" isnull="true" iskey="true">
        <property name="AreaIndex">1</property>
        <property name="Name">fieldGroup</property>
        <property name="Area">RowArea</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="Item3" isnull="true" iskey="true">
        <property name="AreaIndex">2</property>
        <property name="Name">fieldProduct</property>
        <property name="Area">ColumnArea</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>
</XtraSerializer>

I need to loop through all the Items under the element Fields and get the Name and Area attribtutes for each Item
Basically loop through this node
    <property name="Fields" iskey="true" value="41">
0
Comment
Question by:countrymeister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39789681
Hi countrymeister;

The following is using Linq to XML to return the property nodes who's attributes are either name="Name" or name="Area"

// If the XML document is stored on the file system use this line and replace the file path with the one that points to the loction and name of yours.
XDocument doc = XDocument.Load("C:/Working Directory/XtraSerializer.xml");
// Or use this line if the XML is stored in a string in your program replacing VariableNameHere with the actual variable name hollding the XML.
XDocument doc = XDocument.Parse(VariableNameHere);

var results = from nodes in doc.Descendants("property")
              where nodes.Attribute("name").Value == "Fields"
              from node in nodes.Elements("property").Descendants("property")
              where node.Attribute("name").Value == "Name" || node.Attribute("name").Value == "Area"
              select node;

Open in new window

0
 
LVL 1

Author Comment

by:countrymeister
ID: 39789703
Hi ! FernandoSoto

I need all the Items that have aboth these attributes, because sone of my nodes could have only the name and no area attribute.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39789921
Try this.

var results = from nodes in doc.Descendants("property")
              where nodes.Attribute("name").Value == "Fields"
              from node in nodes.Elements("property")
              let name = node.Elements("property").Where(n => n.Attribute("name").Value == "Name").FirstOrDefault()
              let area = node.Elements("property").Where(n => n.Attribute("name").Value == "Area").FirstOrDefault()
              where name != null && area != null
              select new
              {
                  name,
                  area
              };

foreach(var pairOfNodes in results)
{
    MessageBox.Show(pairOfNodes.name + " : " + pairOfNodes.area);
}

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

630 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