Home All Groups Group Topic Archive Search About
Author
18 Dec 2008 11:41 AM
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;
}

--


Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.

Author
18 Dec 2008 12:00 PM
Anthony Jones
Show quote Hide quote
"K Viltersten" <t***@viltersten.com> wrote in message
news:6qur78Fej6vmU1@mid.individual.net...
> 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;
}


--
Anthony Jones - MVP ASP/ASP.NET
Are all your drivers up to date? click for free checkup

Author
18 Dec 2008 12:17 PM
K Viltersten
Show quote Hide quote
>> 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!



--


Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Author
18 Dec 2008 12:31 PM
Anthony Jones
Show quote Hide quote
"Anthony Jones" <AnthonyWJo***@yadayadayada.com> wrote in message
news:OLBK6gQYJHA.5476@TK2MSFTNGP03.phx.gbl...
>
>
> "K Viltersten" <t***@viltersten.com> wrote in message
> news:6qur78Fej6vmU1@mid.individual.net...
>> 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)

Ooops ^          List<XmlNode>

Show quoteHide quote
> {
>     List<XmlNode> result= new List<XmlNode>();
>     foreach (XmlNode node in list)
>     {
>          if (criteria using otherparams)
>              result.Add(node);
>     }
>    return result;
> }
>
>


--
Anthony Jones - MVP ASP/ASP.NET

Bookmark and Share