Answered by:
Problems reading an element of an XML file

Question
-
I'm trying to read an 'element' (not sure if that's the proper term) in an XML file.
This is what the XML file looks like:
And I want to loop through all the <Piece> 'elements'. However, when I run this code, and hit a set break point it shows that nodeList has a count of O. In other words, it's finding the desired 'element'.
But, as you can see, it's error trapped so we know that the file exists and is being opened and read without error but is reporting no occurances of 'Piece'.
Any help would be greatly appreciated.
- Edited by Zetar Sunday, April 21, 2019 1:04 AM
Sunday, April 21, 2019 1:02 AM
Answers
-
Completely agreed, it´s a pity that the response that showed how to deal with the namespace was deleted by its author, because that's exactly what you need. In case you missed the alert, the code would look similar to this:
XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable); nsm.AddNamespace("d", "http://schemas.datacontract.org/2004/07/ModelLib"); var nodeList = xmlDoc.SelectNodes("/d:Scenario/d:Pieces/d:Piece", nsm);
- Marked as answer by Zetar Sunday, April 21, 2019 5:51 PM
Sunday, April 21, 2019 12:36 PM
All replies
-
I'm trying to read an 'element' (not sure if that's the proper term) in an XML file.
This is what the XML file looks like:
And I want to loop through all the <Piece> 'elements'. However, when I run this code, and hit a set break point it shows that nodeList has a count of O. .
Any help would be greatly appreciated.
Post the relevant lines of code using he "Insert Code Block" feature of
the forum editor. That way others can copy & paste the code to test it
and/or make corrections.
Post actual lines from the XML file that someone can copy and paste, or post
a link where the file can be downloaded.
Very few people - if any - are going to go to the trouble of trying to manually
retype all of your data and code so that it can be tested.
- Wayne
Sunday, April 21, 2019 5:22 AM -
I agree with Viorel_'s suggestion (which I read in an Alert email but which
at the moment isn't showing in the forum thread) that you will probably have
to use a namespace manager object and to specify explicitly the default
namespace declared in the file.
Just as a followup to that suggestion, note that you will also have to specify
the namespace id and the manager object whenever you use SelectSingleNode.
For example, instead of:
node.SelectSingleNode("Colour").InnerText
you will likely need something like this:
node.SelectSingleNode("d:Colour", nsm).InnerText
- Wayne- Proposed as answer by Alberto PoblacionMVP Sunday, April 21, 2019 12:32 PM
Sunday, April 21, 2019 9:36 AM -
Completely agreed, it´s a pity that the response that showed how to deal with the namespace was deleted by its author, because that's exactly what you need. In case you missed the alert, the code would look similar to this:
XmlNamespaceManager nsm = new XmlNamespaceManager(xmlDoc.NameTable); nsm.AddNamespace("d", "http://schemas.datacontract.org/2004/07/ModelLib"); var nodeList = xmlDoc.SelectNodes("/d:Scenario/d:Pieces/d:Piece", nsm);
- Marked as answer by Zetar Sunday, April 21, 2019 5:51 PM
Sunday, April 21, 2019 12:36 PM -
Thank you very much! This works perfectly!
I don't understand why it works, or why I had to do something different than the 'normal' XML load I've used before, but nonetheless, I'm very grateful!
Sunday, April 21, 2019 5:52 PM -
I don't understand [...] why I had to do something different than the 'normal' XML
Sunday, April 21, 2019 6:28 PM -
Thank you so much. Never encountered this before. The xmins declaration was done by my British partner.Sunday, April 21, 2019 6:43 PM