Filtering a XmlNodeList

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

K Viltersten

I've been blessed with opportunity to tear my
hair out because of porting a 3.0-version to
2.0. It's handling XML's.

I need help with the following method. The
problem is at the ???-part. How can i add new
nodes to the node list there?

private static XmlNodeList
getElementsNamed(
XmlNodeList list,
String[] names)
{
XmlNodeList output = new XmlNodeList()

foreach (XmlNode node in list)
foreach (String name in names)
if (node.Name == name)
output.???

return output;
}
 
K Viltersten said:
I've been blessed with opportunity to tear my
hair out because of porting a 3.0-version to
2.0. It's handling XML's.

I need help with the following method. The
problem is at the ???-part. How can i add new
nodes to the node list there?

private static XmlNodeList
getElementsNamed(
XmlNodeList list,
String[] names)
{
XmlNodeList output = new XmlNodeList()

foreach (XmlNode node in list)
foreach (String name in names)
if (node.Name == name)
output.???

return output;
}

You can't. The XmlNodeList is the type returned by methods such as
SelectNodes. Its purpose is to hold a list of existing nodes pull together
by some form of query (by tag name, XPath etc).

It is not designed to be a general purpose repository of nodes, for that you
there are plenty of other generic collections you could choose from.

Consider:-

These general patterns

public static IEnumerable<XmlNode> (XmlNodeList list, otherparams)
{
foreach (XmlNode node in list)
{
if (criteria using otherparams)
yield return node
}
}

Useful if you know all you need is to foreach the result.

OR

public static IEnumerable<XmlNode> (XmlNodeList list, otherparams)
{
List<XmlNode> result= new List<XmlNode>();
foreach (XmlNode node in list)
{
if (criteria using otherparams)
result.Add(node);
}
return result;
}
 
I've been blessed with opportunity to tear my
hair out because of porting a 3.0-version to
2.0. It's handling XML's.

I need help with the following method. The
problem is at the ???-part. How can i add new
nodes to the node list there?

private static XmlNodeList
getElementsNamed(
XmlNodeList list,
String[] names)
{
XmlNodeList output = new XmlNodeList()

foreach (XmlNode node in list)
foreach (String name in names)
if (node.Name == name)
output.???

return output;
}

You can't.

That explains a lot... :)
public static IEnumerable<XmlNode>
(XmlNodeList list, otherparams)
{
List<XmlNode> result= new List<XmlNode>();
foreach (XmlNode node in list)
{
if (criteria using otherparams)
result.Add(node);
}
return result;
}

Oh, so the mistake i did was to keep to the
XML-related classes. Thanks a lot!
 
Anthony Jones said:
K Viltersten said:
I've been blessed with opportunity to tear my
hair out because of porting a 3.0-version to
2.0. It's handling XML's.

I need help with the following method. The
problem is at the ???-part. How can i add new
nodes to the node list there?

private static XmlNodeList
getElementsNamed(
XmlNodeList list,
String[] names)
{
XmlNodeList output = new XmlNodeList()

foreach (XmlNode node in list)
foreach (String name in names)
if (node.Name == name)
output.???

return output;
}

You can't. The XmlNodeList is the type returned by methods such as
SelectNodes. Its purpose is to hold a list of existing nodes pull
together by some form of query (by tag name, XPath etc).

It is not designed to be a general purpose repository of nodes, for that
you there are plenty of other generic collections you could choose from.

Consider:-

These general patterns

public static IEnumerable<XmlNode> (XmlNodeList list, otherparams)
{
foreach (XmlNode node in list)
{
if (criteria using otherparams)
yield return node
}
}

Useful if you know all you need is to foreach the result.

OR

public static IEnumerable<XmlNode> (XmlNodeList list, otherparams)
 
Back
Top