The data at the root level is invalid. Line 1, position 1.
C#, 读取xml文件中节点数量
int nodeCount = 0;
XmlDocument doc = new XmlDocument();
doc.Load(file.FullName);
//获得根节点
XmlNode rootNode = doc.DocumentElement;
//在根节点中寻找节点
foreach (XmlNode node in rootNode.ChildNodes)
{
//找到对应的节点
if (node.Name == "url")
{
nodeCount = nodeCount + 1;
}
}
今天在读取XML时,突然遇到这问题了,
总是提示:
The data at the root level is invalid. Line 1, position 1.
-XmlDocument doc = new XmlDocument();
-doc.LoadXml(FilePath);
就是有错误,XML也是对的。
报错就改成:
doc.Load(file.FullName);