|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Filtering a XmlNodeListhair 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.
Show quote
Hide quote
"K Viltersten" <t***@viltersten.com> wrote in message You can't. The XmlNodeList is the type returned by methods such as 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; > } > 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
Show quote
Hide quote
>> I've been blessed with opportunity to tear my That explains a lot... :)>> 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. > public static IEnumerable<XmlNode> Oh, so the mistake i did was to keep to the> (XmlNodeList list, otherparams) > { > List<XmlNode> result= new List<XmlNode>(); > foreach (XmlNode node in list) > { > if (criteria using otherparams) > result.Add(node); > } > return result; > } 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.
Show quote
Hide quote
"Anthony Jones" <AnthonyWJo***@yadayadayada.com> wrote in message Ooops ^ List<XmlNode>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) 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
Other interesting topics
Suggestion for C# language: Modification to the "new" keyword - Allow type inference in C# to use th
Initializing form (dialog) Referencing object properties Dynamically add property values char x='x'; why not char x="x"; ? Dynamically adding Sql type to a parameter Stack in launch Would a string instance become a value type if it was a member of a struct? a simple linq query A dialog in a different thread than its parent? |
|||||||||||||||||||||||