Solved

parse xml document with multiple namespaces

Posted on 2009-05-06
4
2,026 Views
Last Modified: 2013-12-17
I am a rookie at XML and need help. Using C#, Visual studio 2008.
A sample xml document is attached.
I am attempting to parse an XML document with multiple name spaces.
In the attached code snippet I attempt to use : SelectSingleNode() to get the value of  'sequenceNumber' from the 'apcm:Property' node.

wnode = feedDoc.SelectSingleNode("//apcm:Property[@Name='sequenceNumber']", nsmgr);

This returns null.

The next call to 'SelectSingleNode' to get the 'ap:updated' value is successful.
There are several other nodes with the  'apcm:' prefix and I can't seem to parse any of them.
Can anyone help!
feedDoc.Save("C:\\AP_WebFeed\\myAPItemList.xml"); // save original 
 

XmlNamespaceManager nsmgr = new XmlNamespaceManager(feedDoc.NameTable);

nsmgr.AddNamespace("ap","http://www.w3.org/2005/Atom");

nsmgr.AddNamespace("apcm","http://ap.org/scemas/03/2005/apcm");

nsmgr.AddNamespace("apnm","http://ap.org/scemas/03/2005/apnm");
 

XmlNode wnode = null;
 

wnode = feedDoc.SelectSingleNode("//apcm:Property[@Name='sequenceNumber']", nsmgr); // returns null
 

if (wnode != null)

    Globals.m_SequenceNumber = wnode.Attributes["Id"].Value;
 

wnode = feedDoc.SelectSingleNode("//ap:updated", nsmgr); // success
 

if (wnode != null)

    Globals.m_Updated = wnode.InnerText;

Open in new window

ap-xml-sample.txt
0
Comment
Question by:ogdennews
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 24324121
try wnode = feedDoc.SelectSingleNode("//apcm:Property[@apcm:Name='sequenceNumber']", nsmgr);

0
 

Author Comment

by:ogdennews
ID: 24325175
I ttied:
wnode = feedDoc.SelectSingleNode("//apcm:Property[@apcm:Name='sequenceNumber']", nsmgr);

This also returns null
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 24325476
You misspelled the namespace.  It should be
  nsmgr.AddNamespace("apcm", "http://ap.org/schemas/03/2005/apcm");
0
 

Author Comment

by:ogdennews
ID: 24325745
Damn, that fixed it.

I've been looking at that for 2 days.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now