Gohyu
Gohyu

Reputation: 478

How to select XML node with XPath query

I want to select all user nodes m:properties from XmlDocument object in XMLNodeList object with C#! How to do this?

This is my C# code where get xml document:

XmlDocument navEmployesXML = GetXmlDataFromUrl("MYURL");
XmlNodeList nodelist = navEmployesXML.SelectNodes("/feed/entry"); I try with full path but doesnt working

This is my xml document format:

<?xml version="1.0" encoding="UTF-8"?>
-
<feed
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
    xmlns="http://www.w3.org/2005/Atom" xml:base="http://10.20.1.8:7018/activity/OData/">
    <id>http://12333:7018/activity/OData/EmList</id>
    <title type="text">EmList</title>
    <updated>2018-07-13T14:40:29Z</updated>
    <link title="EmList" href="EmList" rel="self"/>-
    <entry m:etag="W/"'36%3BUBQAAAJ7%2FxAEEgQVBBcEFQQSBBAEAAAAAA%3D%3D7%3B33898870%3B'"">
        <id>http://12333:7018/activity/OData/EmList('%D0%90%D0%92%D0%95%D0%97%D0%95%D0%92%D0%90')</id>
        <category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="NAV.EmList"/>
        <link title="EmList" href="EmList('%D0%90%D0%92%D0%95%D0%97%D0%95%D0%92%D0%90')" rel="edit"/>
        <title/>
        <updated>2018-07-13T14:40:29Z</updated>-
        <author>
            <name/>
        </author>-
        <content type="application/xml">-
            <m:properties>
                <d:No>avezeva</d:No>
                <d:Company>My Company</d:Company>
                <d:FullName>Angelina Dangeloa</d:FullName>
                <d:USERID_1>myDomain\AVEZEVA</d:USERID_1>
                <d:Job_Title>Organisator</d:Job_Title>
                <d:Department>MY SOFT</d:Department>
                <d:ManagerID>myDomain\AIVANOV</d:ManagerID>
                <d:Days_of_current_year m:type="Edm.Int32">20</d:Days_of_current_year>
                <d:Days_of_last_year m:type="Edm.Int32">0</d:Days_of_last_year>
                <d:Deputy>myDomain\AVEZEVA;myDomain\MMANASIEVA</d:Deputy>
                <d:Deputy1/>
                <d:Second_possition/>
                <d:Union_Membership_No/>
                <d:ETag>36;UBQAAAJ7/xAEEgQVBBcEFQQSBBAEAAAAAA==7;33898870;</d:ETag>
            </m:properties>
        </content>
    </entry>-

I want all nodes → entry - with all users information

Upvotes: 0

Views: 195

Answers (1)

jdweng
jdweng

Reputation: 34421

You have a namespace issue. I recommend using Xml Linq with a dictionary like code below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication53
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string feed = File.ReadAllText(FILENAME);            
            XDocument doc = XDocument.Parse(feed);

            XElement properties = doc.Descendants().Where(x => x.Name.LocalName == "properties").FirstOrDefault();

            Dictionary<string, string> dict = properties.Elements()
                .GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
}

Upvotes: 1

Related Questions