3
我读一个XML文件中的一个非常简单的方法:XmlException:检测到多个文档元素
XmlTextReader reader = new XmlTextReader(dataPath);
while(reader.Read()){
switch (reader.Name){
case "language":
Debug.Log(reader.ReadString());
break;
case "file":
Debug.Log(reader.ReadString());
break;
case "arg":
Debug.Log(reader.ReadString());
break;
}
}
在哪里我的XML是这样的:
<?xml version="1.0" encoding="ISO-8859-1"?>
<config>
<language>EN-US</language>
<file>\File\Doc\sample.txt</file>
</config>
<data>
<arg>LKR</language>
</dara>
我第一个问题是:
XmlException: Multiple document element was detected. file:///C:/prj/as/sample.xml Line 7, position 2.
Mono.Xml2.XmlTextReader.ReadStartTag()
Mono.Xml2.XmlTextReader.ReadContent()
Mono.Xml2.XmlTextReader.Read()
System.Xml.XmlTextReader.Read()
LectorXML.Start() (at as/sampleXML.cs:17)
第二,我的输出是语言和文件,但没有arg。也许是因为是不同的节点?我怎样才能解决这个问题?
单声道在.NET 2.0上,对吗?那么你不应该使用'new XmlTextReader()'。改用'XmlReader.Create()'。而且,如果文档太大而无法放入内存,则最好使用'XmlDocument'。 –