0

I have one XmlNodeList, I want to create two XmlNodeList out of this. I will be checking for some tag inside each item in List, on the basis of that tag presence I will be adding them to one of the list which I have defined.

I was trying to add the list but I did not any method to add the particular item to a new XmlNodeList which is null in the start . Please help out. What I am missing here.

I have tried List<XmlNode> , it is throwing error System.ArgumentNullException: 'Value cannot be null. Parameter name: source'

class Program
    {
        static void Main(string[] args)
        {
            //Import XMl 
            // XmlNode list as name NEW

            foreach(XmlNode emp in NEW)
            {
                if (emp != null)
                {
                    AddNewList(emp);
                }

            }
        } 

        public static void AddNewList(XmlNode emp)
        {
            //Checking for some tag 
            if(tag!=null)
            {
                // It is throwing error 
                currentList.Append(emp);
            }
        }

        public XmlNodeList currentList = null;
        public XmlNodeList previousList = null;
    }
}
dbc
  • 104,963
  • 20
  • 228
  • 340
deep5459
  • 35
  • 8
  • You have to create the `XmlNode` from some `XmlDocument`, then add it into the `XmlDocument` DOM hierarchy. an `XmlNodeList` is just a filtered set of nodes from a document, to add to the list you have to add to the document in such a way that it gets included in the filter. This is one of the reasons it might be easier to switch to LINQ to XML and `XElement` by the way. See: [How to create an XML document using XmlDocument?](https://stackoverflow.com/q/11492705). – dbc Nov 01 '19 at 17:57
  • 1
    What you expect to happen at the end? XmlNodeList is result of running select query and it is not really possibly to add node to selection in general case... – Alexei Levenkov Nov 01 '19 at 17:59
  • @dbc I don't want to add XmlNodes to existing Xml, I just to add the items in different XmlNodeList on the given conditon – deep5459 Nov 01 '19 at 18:06
  • @AlexeiLevenkov At the end I want each item which is in NEW, either it should be in currentList or previousList – deep5459 Nov 01 '19 at 18:08
  • You can't do that with `XmlNodeList` which is a filtered view into an `XmlDocument`. From the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlnodelist?view=netframework-4.8): *Changes to the children of the node object that the XmlNodeList collection was created from are immediately reflected in the nodes returned by the XmlNodeList properties and methods. XmlNodeList supports iteration and indexed access.* Better to switch to LINQ to XML and `XElement`. – dbc Nov 01 '19 at 18:36
  • *I have tried List , it is throwning error System.ArgumentNullException: 'Value cannot be null. Parameter name: source'* - then might you please share a [mcve] demonstrating your real problem? – dbc Nov 01 '19 at 18:37
  • I don' t have option to use **LINQ to XML and XElement** . I have tried **I have tried with List** , I was able to fix that error by declaring the currentList like this `public static List currentList = new List();`, I am still working on the solution . Once I am done , I will update. – deep5459 Nov 01 '19 at 18:47

1 Answers1

0

The only way to have items in XmlNodeList is to run selection query on XmlNode. There is no other way to construct XmlNodeList.

So the only option to "split" XmlNodeList in two XmlNodeList is to run separate XPath queries with opposite conditions that will select nodes into separate lists.

Note that XmlNodeList is not List<XmlNode> despite very similar name - adding to List<XmlNode> is indeed possible.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • I have tried with List, but it throwing error System.ArgumentNullException: 'Value cannot be null. Parameter name: source' – deep5459 Nov 01 '19 at 18:26