从XML文档获取特定数据 [英] Get specific data from XML document
问题描述
你好,
我有这样的xml文档:
< level1>
< level2>
< level3>
< attribute1>
< attribute2>
< attribute3>
</level3>
< level3>
< attribute1>
< attribute2>
< attribute3>
</level3>
</level2>
< level2>
< level3>
< attribute1>
...
...
...
我正在使用c#,我想遍历所有"level3",并且对于每个"level3",我都想读取attribute2,如果它说"true",我想打印相应的attribute3(可以是没有这些属性的"level3".
我将xml保留在XmlDocument中.
然后,我将所有"level3"节点保留如下:
XmlNodeList xnList = document.SelectNodes(String.Format("/level1/level2/level3"));
(文档是XmlDocument).
但是从现在开始,我不知道该如何继续.我尝试通过for..each的xnList进行操作,但对我来说没有任何效果..
非常感谢
Hello,
I have xml document like this:
<level1>
<level2>
<level3>
<attribute1>
<attribute2>
<attribute3>
</level3>
<level3>
<attribute1>
<attribute2>
<attribute3>
</level3>
</level2>
<level2>
<level3>
<attribute1>
...
...
...
I''m using c#, and I want to go thru all "level3", and for every "level3", i want to read attribute2, and if it says "true", i want to print the corresponding attribute3 (can be "level3" without these attributes).
I keep the xml in XmlDocument.
Then I keep all the "level3" nodes like this:
XmlNodeList xnList = document.SelectNodes(String.Format("/level1/level2/level3"));
(document is the XmlDocument).
But from now on, I don''t know exactly how to continue. I tried going thru xnList with for..each, but nothing works fine for me..
Thanks a lot
推荐答案
尝试:
XmlNodeList xnList = document.SelectNodes("/level1/level2/level3/*[self::attribute1 or self::attribute2 or self::attribute3]");
这应该为您提供所有属性节点的列表.
This should give you a list of all attribute nodes.
不,您没有XML这样的文档".您在问题中显示的是不是 XML代码,这是格式不正确的,因此根本无法解析.如果您想问一下解析具有类似于XML的结构的文本,请解释这种格式并询问您的问题,但不要问关于XML的问题-您的问题中没有这种事情.
—SA
No, you don''t have XML "document like this". What you show in your question is not an XML code, this is something not well-formed so it won''t parse at all. If you want to ask about parsing some text which has a structure resembling XML, explain this format and ask your question about it, but don''t ask a question about XML — there is no such thing in your question.
—SA
这篇关于从XML文档获取特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!