icn*_*icn 4 .net c# xml xmldocument xml-comments
我使用XmlDocument来解析xml文件,但似乎XmlDocument始终将xml注释作为xml节点读取:
我的C#代码
XmlDocument xml = new XmlDocument();
xml.Load(filename);
foreach (XmlNode node in xml.FirstChild.ChildNodes) {
}
Run Code Online (Sandbox Code Playgroud)
Xml文件
<project>
<!-- comments-->
<application name="app1">
<property name="ip" value="10.18.98.100"/>
</application>
</project>
Run Code Online (Sandbox Code Playgroud)
.NET不应该跳过XML注释吗?
不,但是node.NodeType应该XmlNodeType.Comment.
如果它不能读取注释,您也无法访问它们,但您可以执行以下操作来获取所有"真实节点":
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
}
Run Code Online (Sandbox Code Playgroud)
或没有LINQ/XDocument:
XmlDocument xml = new XmlDocument();
xml.Load(filename);
foreach (XmlNode node in xml.FirstChild.ChildNodes)
{
if(node.NodeType != XmlNodeType.Comment)
{
//your code
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9517 次 |
| 最近记录: |