-3

enter image description hereI am having a XmlNodeList xnlSubParam

              XmlDocument xDoc = new XmlDocument();
              xDoc.Load(somexml);
              XmlNode xnParamList = xDoc.SelectSingleNode("//tag");
              XmlNodeList   xnlSubParam = xnParamList.SelectNodes("subparam");

Now xnlSubParam will contain a list of nodes

How to get the Node attributes and how to delete a node from the list.I jut want to Delete the Subparam list.. from xnlSubparam how to do this ? In my xml i have a Element Named Inside the inside

              <subparam name="test1" displayname="Test1" type="BOOLEAN"/> 
              <subparam name="test2" displayname="Test2" type="BOOLEAN"/>
              <subparam name="test3" displayname="Test3" type="BOOLEAN"/>

enter image description here

Aravind
  • 177
  • 1
  • 3
  • 13
  • 1
    Are you trying to delete it from the document as well, or just the list? And could you use LINQ to XML instead? (I generally find it a nicer API than the old DOM API.) – Jon Skeet Jan 08 '13 at 04:38
  • I dont want to delete it from Document i just want to delete it from XmlNodelist xnlSubParam – Aravind Jan 08 '13 at 04:39
  • checkout this link.. I found it doing a google search remove a node using LinqToXML - http://stackoverflow.com/questions/10378498/remove-xml-nodes-using-linq-to-xml – MethodMan Jan 08 '13 at 04:40
  • No sir its not working ... they gave for XmlDocument i dont need for that – Aravind Jan 08 '13 at 04:45
  • Can you post that Image in a larger form..? one would need to have extraordinary vision to see that, where is the XML that you are working with..? – MethodMan Jan 08 '13 at 04:51
  • Your question is getting less and less clear. "I jut want to Delete the Subparam list" - you've *asked* for the `subparam` nodes... – Jon Skeet Jan 08 '13 at 05:02

1 Answers1

4

I don't know of any way of mutating the XmlNodeList itself. I'd suggest you copy the nodes into a List<XmlNode>, which you can then mutate however you wish in the normal way:

List<XmlNode> nodes = xnlSubParam.Cast<XmlNode>().ToList();
// Now work with nodes

To use these LINQ methods, you need a using directive:

using System.Linq;

This only works in .NET 3.5 and higher, of course (unless you're using LINQBridge or something similar).

Note that personally I'd use LINQ to XML throughout:

var doc = XDocument.Load(somexml);
var nodes = doc.Descendants("tag")
               .First()
               .Elements("subparam")
               .ToList();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Nopes it doent work it shows problem near .Cast becas Cast is not comming in Xmnode.. – Aravind Jan 08 '13 at 04:41
  • @user1903774: It's an extension method - you'll need to a `using` directive for `System.Linq`. I'm assuming you're using .NET 3.5 or higher, mind you - are you? – Jon Skeet Jan 08 '13 at 04:42
  • I am using .NET 4.0 and I am using using System.Xml.Linq; – Aravind Jan 08 '13 at 04:44
  • I would also suggest that you show a small snippet of the XML that you have so that perhaps @Jon Skeet and others here can see what it is your are trying to Delete.. Jon is very knowledgeable btw – MethodMan Jan 08 '13 at 04:45
  • 2
    @user1903774: Well `System.Xml.Linq` isn't the same as `System.Linq`... you need a using directive for `System.Linq`. But it's not clear why you're using the old DOM API if you're *also* using LINQ to XML... – Jon Skeet Jan 08 '13 at 04:45
  • @user1903774: Um, I didn't vote you down, although it's not clear why you've added an image to your question. – Jon Skeet Jan 08 '13 at 04:51
  • That's not your XML.. do you have a snippet of the xml that you are working against..? – MethodMan Jan 08 '13 at 04:57
  • In my xml i have a Element Named Inside the inside – Aravind Jan 08 '13 at 05:01
  • 3
    @user1903774: Please edit your *question* (rather than adding irrelevant comments to my answer) showing your XML (not screenshots) and giving a *clear* indication of what you're trying to achieve. Read http://tinyurl.com/so-list – Jon Skeet Jan 08 '13 at 05:02
  • I want to delete one of this sub param in runtime – Aravind Jan 08 '13 at 05:02
  • 2
    I give up at this point, I'm afraid. I've explained how you can copy your list of nodes to a `List<>` which you can then mutate however you want. It's not at all clear why you can't just use my answer - since I explained that you need a using directive for `System.Linq`, you've given no more information about why my answer wouldn't work for you. – Jon Skeet Jan 08 '13 at 05:10
  • I give up as well Jon I am not sure why individuals are so afraid to follow expert advise such as what you have given him.I am off to answer other posts – MethodMan Jan 08 '13 at 05:14
  • While using this ... this error occurs List nodes = xnlSubParam.Cast().ToList(); Error 5 'System.Xml.XmlNodeList' does not contain a definition for 'Cast' and no extension method 'Cast' accepting a first argument of type 'System.Xml.XmlNodeList' could be found (are you missing a using directive or an assembly reference?) – Aravind Jan 08 '13 at 05:15
  • @user1903774: Then you *haven't* got a `using System.Linq;` directive. – Jon Skeet Jan 08 '13 at 05:22