Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing XML data 1

Status
Not open for further replies.

Nelviticus

Programmer
Sep 9, 2003
1,819
GB
I'm trying to parse a document list which is in the form of an XMLNode (retrieved from Sharepoint). The code I have works but I'm sure I'm doing it in the wrong way so I wondered if someone could show me how it's supposed to be done.

The OuterXML looks a bit like this (I have stripped out most of the attributes):
Code:
<listitems xmlns:s="blah">
	<rs:data ItemCount="6">
		<z:row ows_DocIcon="xlsx"/>
		<z:row ows_DocIcon="aspx"/>
	</rs:data>
</listitems>

My code is as follows - ndListItems is an XMLNode:
Code:
if (ndListItems.ChildNodes.Count > 2) {
	foreach (XmlNode thisNode in ndListItems) {
		if (thisNode.NodeType == XmlNodeType.Element) {
			foreach (XmlNode subNode in thisNode) {
				if (subNode.NodeType == XmlNodeType.Element) {
					// Do stuff with subNode
				}
			}
		}
	}
}

Is there a better way of doing it than this? I just want to build a list of the <z:row /> items within the <rs:data /> element. Ideally I just want the rows where the 'ows_DocIcon' attribute is "xlsx" but would be happy with just a cleaner way of getting all the rows.

Thanks

Nelviticus
 
Something like this without knowing how you set up, if any, the xmlnamespacemanager.
[tt] XmlNode subNode=ndListItems.SelectSingleNode("//*[local-name()='row' and @ows_DocIcon='xlsx']")[/tt]
 
Excellent tsuji, thank you! I changed it slightly to get an XmlNodeList rather than an XmlNode then I step through it with foreach. It works much better than my old method. I don't have an XmlNamespaceManager.

I guess I need to learn the XPath syntax properly.

Thanks again!

Nelviticus
 
Xpath is very useful. I use it to get nodes by the local name and children name, then get that node's grandparent and so on. If you know how to use CD in command prompt, you have a good idea of how to use XPath already
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top