EDN Admin
Well-known member
Hi,
I am attempting to filter through a XML file and pull out each instance of name and parent using the following query:
<pre class="prettyprint XElement test = XElement.Load("thefile.xml");
var query = from param in test.Descendants("KEY")
where ((string)param.Attribute("name")).Contains("name")
select new
{
code = (string)param.Element("VALUE")
};[/code]
<br/>
Now this code does work, however I would like to filter for both the name and the parent in the same call, something like
<pre class="prettyprint var query = from param in test.Descendants("SINGLE")
where ((string)param.Element("KEY").Attribute("name")).Contains("name") || ((string)param.Element("KEY").Attribute("name")).Contains("parent")
select new
{
name = (string)param.Element("KEY").Attribute("name"),<br/><span class="x_Apple-tab-span" style="white-spacere parent = param.Element("KEY").Attribute("parent")
};[/code]
I understand my code under select has no chance of working, however I was hoping someone might have a suggestion as to how to do this, I have been searching for days and have yet to figure anything out. The XML file is below.
<pre class="prettyprint <RESPONSE>
<MULTIPLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>1</VALUE>
</KEY>
<KEY name="description
<VALUE null="null"/>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>1</VALUE>
</KEY>
<KEY name="name
<VALUE>Miscellaneous</VALUE>
</KEY>
<KEY name="parent
<VALUE>0</VALUE>
</KEY>
<KEY name="path
<VALUE>/1</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>50000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>1331562033</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>2</VALUE>
</KEY>
<KEY name="description
<VALUE>
Sassafras <span> is a genus of three<span> extant and one extinct<span> species of deciduous<span> trees<span> in the family Lauraceae<span>, native to eastern North America<span> and eastern Asia<span>.
</VALUE>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>5</VALUE>
</KEY>
<KEY name="name
<VALUE>Sassafras</VALUE>
</KEY>
<KEY name="parent
<VALUE>4</VALUE>
</KEY>
<KEY name="path
<VALUE>/4/5</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>20000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>0</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>1</VALUE>
</KEY>
<KEY name="description
<VALUE> test </VALUE>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>2</VALUE>
</KEY>
<KEY name="name
<VALUE>Sharmoodle</VALUE>
</KEY>
<KEY name="parent
<VALUE>0</VALUE>
</KEY>
<KEY name="path
<VALUE>/2</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>40000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>0</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>1</VALUE>
</KEY>
<KEY name="description
<VALUE/>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>4</VALUE>
</KEY>
<KEY name="name
<VALUE>Steve</VALUE>
</KEY>
<KEY name="parent
<VALUE>0</VALUE>
</KEY>
<KEY name="path
<VALUE>/4</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>10000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>0</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>1</VALUE>
</KEY>
<KEY name="description
<VALUE/>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>3</VALUE>
</KEY>
<KEY name="name
<VALUE>Testing</VALUE>
</KEY>
<KEY name="parent
<VALUE>0</VALUE>
</KEY>
<KEY name="path
<VALUE>/3</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>30000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>0</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
</MULTIPLE>
</RESPONSE>[/code]
<br/>
Thanks for all your help!
View the full article
I am attempting to filter through a XML file and pull out each instance of name and parent using the following query:
<pre class="prettyprint XElement test = XElement.Load("thefile.xml");
var query = from param in test.Descendants("KEY")
where ((string)param.Attribute("name")).Contains("name")
select new
{
code = (string)param.Element("VALUE")
};[/code]
<br/>
Now this code does work, however I would like to filter for both the name and the parent in the same call, something like
<pre class="prettyprint var query = from param in test.Descendants("SINGLE")
where ((string)param.Element("KEY").Attribute("name")).Contains("name") || ((string)param.Element("KEY").Attribute("name")).Contains("parent")
select new
{
name = (string)param.Element("KEY").Attribute("name"),<br/><span class="x_Apple-tab-span" style="white-spacere parent = param.Element("KEY").Attribute("parent")
};[/code]
I understand my code under select has no chance of working, however I was hoping someone might have a suggestion as to how to do this, I have been searching for days and have yet to figure anything out. The XML file is below.
<pre class="prettyprint <RESPONSE>
<MULTIPLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>1</VALUE>
</KEY>
<KEY name="description
<VALUE null="null"/>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>1</VALUE>
</KEY>
<KEY name="name
<VALUE>Miscellaneous</VALUE>
</KEY>
<KEY name="parent
<VALUE>0</VALUE>
</KEY>
<KEY name="path
<VALUE>/1</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>50000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>1331562033</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>2</VALUE>
</KEY>
<KEY name="description
<VALUE>
Sassafras <span> is a genus of three<span> extant and one extinct<span> species of deciduous<span> trees<span> in the family Lauraceae<span>, native to eastern North America<span> and eastern Asia<span>.
</VALUE>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>5</VALUE>
</KEY>
<KEY name="name
<VALUE>Sassafras</VALUE>
</KEY>
<KEY name="parent
<VALUE>4</VALUE>
</KEY>
<KEY name="path
<VALUE>/4/5</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>20000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>0</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>1</VALUE>
</KEY>
<KEY name="description
<VALUE> test </VALUE>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>2</VALUE>
</KEY>
<KEY name="name
<VALUE>Sharmoodle</VALUE>
</KEY>
<KEY name="parent
<VALUE>0</VALUE>
</KEY>
<KEY name="path
<VALUE>/2</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>40000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>0</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>1</VALUE>
</KEY>
<KEY name="description
<VALUE/>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>4</VALUE>
</KEY>
<KEY name="name
<VALUE>Steve</VALUE>
</KEY>
<KEY name="parent
<VALUE>0</VALUE>
</KEY>
<KEY name="path
<VALUE>/4</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>10000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>0</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
<SINGLE>
<KEY name="coursecount
<VALUE>0</VALUE>
</KEY>
<KEY name="depth
<VALUE>1</VALUE>
</KEY>
<KEY name="description
<VALUE/>
</KEY>
<KEY name="error
<VALUE null="null"/>
</KEY>
<KEY name="id
<VALUE>3</VALUE>
</KEY>
<KEY name="name
<VALUE>Testing</VALUE>
</KEY>
<KEY name="parent
<VALUE>0</VALUE>
</KEY>
<KEY name="path
<VALUE>/3</VALUE>
</KEY>
<KEY name="sortorder
<VALUE>30000</VALUE>
</KEY>
<KEY name="timemodified
<VALUE>0</VALUE>
</KEY>
<KEY name="visible
<VALUE>1</VALUE>
</KEY>
</SINGLE>
</MULTIPLE>
</RESPONSE>[/code]
<br/>
Thanks for all your help!
View the full article