I’m trying to output a specific node to output to a text file(output to console fine) but I keep getting an error message: ‘System.Xml.XmlNodeList’ to ‘string[]’ on this line:
string[] lines = elemList;
Here is some more code:
namespace countC
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("list.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("version");
for (int i = 0; i < elemList.Count; i++)
{
Console.WriteLine(elemList[i].InnerXml);
string[] lines = elemList;
System.IO.File.WriteAllLines(@"C:\VBtest\STIGapp.txt", lines);
}
Console.ReadKey();
}
}
}
The error is because you are trying to assign an object of type
XmlNodeListto a variable of typestring[]– the two are incompatible and you can’t assign one from the other.If you do this instead then it will at least compile:
Although I’m not sure that it will do what you want (if
elemListcontains more that one element the above will keep overwriting the given file).