Getting tags OTHER than blobb

  • Thread starter Thread starter K Viltersten
  • Start date Start date
K

K Viltersten

I have a syntax as follows.

XElement elem = ...;
IEnumerable<XElement> enum
= elem.Elements("blobb");

Now, i'd like to recompose it in such way so i'll
get the list of tags NOT named "blobb". Can it
be easily done?
 
maybe something like

IEnumerable<XElement> enum = elem.Elements().Where(e => e.Name !="blobb");
 
maybe something like
IEnumerable<XElement> enum = elem.Elements().Where(e => e.Name
!="blobb");

My bad - i forgot to add that my application is deployed
for C#2.0 and there's nothing to do about it... :(

I can go brute on it, list all nodes, count them, subtract the
number of nodes named "blopp" and return the answer.
However, i was hoping for a solution that doesn't require
me to program that but rather speeds up the process.

Thanks!
 
K said:
My bad - i forgot to add that my application is deployed
for C#2.0 and there's nothing to do about it... :(

The you do not have:
I can go brute on it, list all nodes, count them, subtract the
number of nodes named "blopp" and return the answer.
However, i was hoping for a solution that doesn't require
me to program that but rather speeds up the process.

You will need to code something.

For two ways of doing it see code below.

Arne

===================================

using System;
using System.IO;
using System.Xml;

namespace E
{
public class Program
{
public static void Main(string[] args)
{
string xml =
"<x><blobb>1</blobb><y>2</y><blobb>3</blobb><y>4</y></x>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
int nall = doc.SelectNodes("//*").Count;
int nblobb = doc.SelectNodes("//blobb").Count;
Console.WriteLine(nall - nblobb);
int nnonblobb = 0;
XmlReader xr = new XmlTextReader(new StringReader(xml));
while (xr.Read())
{
if (xr.IsStartElement())
{
if (xr.Name != "blobb")
{
nnonblobb++;
}
}
}
Console.WriteLine(nnonblobb);
Console.ReadKey();
}
}
}
 
Back
Top