Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 305
  • Last Modified:

c# XmlDocument node elements/attributes not visible from known nodes

Techies--
I am trying to export an xml document from a Dictionary/hastable in odataSF.ReadFromDictinaryByKey("Position") where "Position" is a string key. The line xdoc.Save(Console.Out) works. The output of the data is in the attached file.
               XmlDocument xdoc = new XmlDocument();
               xdoc = odataSF.ReadFromDictionaryByKey("Position");
               xdoc.Save(Console.Out);

               Console.WriteLine("\n");

Open in new window

     
                    
In the next set of lines, I look to see what the nodes are. The nodes that output from here are "xml" and "feed".

			   
               //capture nodes - start at the very beginning "/"
               List<string> nodeNames = new List<string>();
               foreach (System.Xml.XmlNode node in xdoc.SelectNodes("/"))
               {
                   foreach (System.Xml.XmlNode child in node.ChildNodes)
                   {
                       if (!nodeNames.Contains(child.Name)) nodeNames.Add(child.Name);
                       
                   }
               }
			   
               Console.WriteLine("Names of Nodes: \n");
               foreach (string name in nodeNames)
               {
                   Console.WriteLine("Node: " + name + "\n");
               }

Open in new window


Here is where I am attempting to get specific values back out of the document. No errors, but nothing is found. Doesn't matter whether I put in xml, feed, etc. Nothing
is found.

               XmlNodeList xnList = xdoc.SelectNodes("/xml");
               foreach (XmlNode xn in xnList)
               {
                   string jobTitle = xn["jobTitle"].InnerText;
                   string code = xn["code"].InnerText;

                   Console.WriteLine("Elements: {0} {1}", jobTitle, code);
               }

Open in new window


I am confused about the structure of this doc--I thought that I would see the nodes: feed/entry/content/properties-- with the elements under properties.
Can someone offer direction/advice on what it is I haven't got right?
Positions.xml
0
Paula DiTallo
Asked:
Paula DiTallo
  • 2
1 Solution
 
ste5anSenior DeveloperCommented:
The problem is that yor XML uses a default namespace. Thus your selector can not find it, cause it does not know it. Here is an example showing how to specify it.
0
 
it_saigeDeveloperCommented:
Try this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;

static class XmlExample
{
	public static void Main()
	{
		XmlReader reader = XmlReader.Create("Feeds.xml");
		XElement data = XElement.Load(reader);
		XmlNameTable table = reader.NameTable;
		XmlNamespaceManager manager = new XmlNamespaceManager(table);
		manager.AddNamespace("ns", "http://www.w3.org/2005/Atom");
		manager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
		manager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
		manager.AddNamespace("base", "https://api8.successfactors.com:443/odata/v2/");
		foreach (XElement entries in data.XPathSelectElements("./ns:entry", manager))
		{
			foreach (var contents in (from entry in entries.Elements() where entry.Name.LocalName.Contains("content") select entry))
			{
				foreach (var properties in (from content in contents.Elements() where content.Name.LocalName.Contains("properties") select content))
				{
					foreach (var property in (from property in properties.Elements() where property.Name.LocalName.Contains("jobTitle") || property.Name.LocalName.Contains("code") select property))
						Console.WriteLine(string.Format("Name - {0}; Value - {1}", property.Name.LocalName, property.Value));
				}
			}
		}
		Console.ReadLine();
	}
}

Open in new window

Produces the following output:Capture.JPG
-saige-
0
 
Paula DiTalloIntegration developerAuthor Commented:
Brilliantly done @Saige! :)
0
 
it_saigeDeveloperCommented:
Not a problem.

-saige-
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now