[英]XML output to text file?
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:我正在尝试将 output 到 output 的特定节点到文本文件(输出到控制台正常),但我不断收到错误消息:'System.Xml'到此行:List
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 XmlNodeList
to a variable of type string[]
- the two are incompatible and you can't assign one from the other.该错误是因为您试图将
XmlNodeList
类型的 object 分配给string[]
类型的变量 - 两者不兼容,您不能从另一个分配一个。
If you do this instead then it will at least compile:如果你这样做,那么它至少会编译:
string line = elemList[i].InnerXml;
System.IO.File.WriteAllText(@"C:\VBtest\STIGapp.txt", line);
Although I'm not sure that it will do what you want (if elemList
contains more that one element the above will keep overwriting the given file).虽然我不确定它会做你想要的(如果
elemList
包含一个以上的元素,上面将继续覆盖给定的文件)。
elemList is an XmlNodeList, you can't implicitly cast it to a string array. elemList 是一个 XmlNodeList,您不能将其隐式转换为字符串数组。
You could try this你可以试试这个
string line = elemList[i].InnerText;
System.IO.File.WriteAllLines(@"C:\VBtest\STIGapp.txt", line);
but this of course, depends on your data.但这当然取决于您的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.