我使用XmlDocument解析xml文件,但是XmlDocument似乎总是将xml注释作为xml节点读取:
我的C#代码
XmlDocument xml = new XmlDocument();
xml.Load(filename);
foreach (XmlNode node in xml.FirstChild.ChildNodes) {
}Xml文件
<project>
<!-- comments-->
<application name="app1">
<property name="ip" value="10.18.98.100"/>
</application>
</project>.NET不应该跳过XML注释吗?
发布于 2011-06-24 22:13:03
不是的,但是XmlNodeType.Comment提供的是node.NodeType。
如果它不能读取评论,你也不能访问它们,但你可以做一些类似以下的事情来获得所有的“真实节点”:
XDocument xml = XDocument.Load(filename);
var realNodes = from n in xml.Descendants("application")
where n.NodeType != XmlNodeType.Comment
select n;
foreach(XNode node in realNodes)
{
//your code
}或不使用LINQ/XDocument:
XmlDocument xml = new XmlDocument();
xml.Load(filename);
foreach (XmlNode node in xml.FirstChild.ChildNodes)
{
if(node.NodeType != XmlNodeType.Comment)
{
//your code
}
}发布于 2011-06-24 22:14:29
发布于 2011-06-24 22:20:11
尝尝这个
XmlDocument xml = new XmlDocument();
xml.Load(filename);
foreach (XmlNode node in xml.FirstChild.ChildNodes)
{
if(node.GetType() == XmlNodeType.Comment)
{
//Do nothing
}
else
{
//Your code goes here.
}
}https://stackoverflow.com/questions/6468994
复制相似问题