这是我要阅读的xml
<servers>
<server name=" PIOU PIOU || OPTIMAL" ca="1" continent_code="EU"
country="France" country_code="FR" ip="s1.mymumble.fr"
port="20129" region="" url="http://www.mymumble.fr" />
</servers>
现在,当我阅读它时,我就成功获取了“服务器”,并且我也可以阅读其innerxml,即创建了正确的节点。但是当我创建一个带有元素'server'的节点时,该节点为空。我想原因是在“服务器”节点中使用的开始和结束元素的简短形式。但是我的问题是xml来自某个远程服务器,我无法对其进行修改,因此必须以编写方式进行读取。
这是我读取XML的代码:
List<XmlNode> nodeList = new List<XmlNode>();
XmlDocument doc = new XmlDocument();
while (reader.Read())
{
//keep reading until we see my element
if (reader.Name.Equals("server") && (reader.NodeType == XmlNodeType.Element))
{
XmlNode myNode = doc.ReadNode(reader);
Debug.Log(reader.IsEmptyElement ? "its empty" : "not empty");
//this always prints "its empty"
nodeList.Add(myNode);
}
}
foreach( XmlNode node in nodeList)
{
Debug.Log("child:\t"+node.FirstChild.InnerXml);
}
请参阅IsEmptyElement的文档。特别是,此示例:
<item num="123"/> (IsEmptyElement is true).
<item num="123"></item> (IsEmptyElement is false, although element content is empty).
您的节点被认为是空的,因为它使用缩写形式。没有元素“内容”,但是有有属性。
您是否检查了所创建的节点(即myNode
)以查看其包含属性?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句