Solved

parse xml document with multiple namespaces

Posted on 2009-05-06
4
2,035 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

717 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