我正在尝试解析xml。所有节点都有开始和结束标记,但某些行中只有一个节点的标记为<persons/>
在大多数情况下,它看起来像这样:<persons> ... </persons>
当此节点未按此结束时,我无法从xml获取值
这是我的代码:
foreach (HtmlNode man in bm.SelectNodes(".//persons"))
{
//store values
}
我如何克服这个问题?即使一些节点在开始时是这样的:
<persons> </persons>
如果文件中间有这样的标签
<persons/>
我无法从剩余的行中获取剩余的<persons> </persons>
值
答案 0 :(得分:0)
你为什么使用htmlnode? xmlnode就好了。
否则,请显示更多代码。
你是否跨过了这条线?你遇到任何错误吗?
试试这个:
internal string ParseXML()
{
string ppl = "";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
foreach (XmlElement node in doc.SelectNodes(".//person"))
{
string text = node.InnerText; //or loop through its children as well
ppl += text;
}
return ppl;
}