Link to home
Start Free TrialLog in
Avatar of countrymeister
countrymeister

asked on

Query an xml file

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">
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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

Avatar of countrymeister
countrymeister

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial