Home > Blockchain >  Unable to get element in XML with namespace
Unable to get element in XML with namespace

Time:07-06

<?xml version=""1.0"" encoding=""UTF-8""?>
<serv:message
    xmlns:serv=""http://www.webex.com/schemas/2002/06/service""
    xmlns:com=""http://www.webex.com/schemas/2002/06/common""
    xmlns:sess=""http://www.webex.com/schemas/2002/06/service/session""
    xmlns:train=""http://www.webex.com/schemas/2002/06/service/trainingsession""
    xmlns:qti=""http://www.webex.com/schemas/2002/06/service/trainingsessionqti""
    xmlns:qtiasi=""http://www.webex.com/schemas/2002/06/service/trainingsessionqtiasi"">
    <serv:header>
        <serv:response>
            <serv:result>SUCCESS</serv:result>
            <serv:gsbStatus>PRIMARY</serv:gsbStatus>
        </serv:response>
    </serv:header>
    <serv:body>
        <serv:bodyContent xsi:type=""train:getTrainingSessionResponse""
            xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
            <sess:accessControl>                
                <sess:sessionPassword>12345678</sess:sessionPassword> 
            </sess:accessControl>           
            <train:telephony>
                <sess:telephonySupport>NONE</sess:telephonySupport>
            </train:telephony>          
            <train:repeat>              
                <train:occurenceType>NO_REPEAT</train:occurenceType>
            </train:repeat>
            <train:attendees>
                <sess:participants>
                    <sess:participant>
                        <sess:person>
                            <com:name>Test User</com:name>
                            <com:firstName>Test</com:firstName>
                            <com:lastName>User</com:lastName>
                            <com:address>
                                <com:addressType>PERSONAL</com:addressType>
                            </com:address>
                            <com:phones/>
                            <com:email>[email protected]</com:email>
                            <com:type>VISITOR</com:type>
                        </sess:person>
                        <sess:contactID>1234567980</sess:contactID>
                        <sess:joinStatus>INVITE</sess:joinStatus>
                        <sess:role>ATTENDEE</sess:role>
                    </sess:participant>
                </sess:participants>
            </train:attendees>          
        </serv:bodyContent>
    </serv:body>
</serv:message>

Above XML is standard WebEx GetEvent XML and I need to process. I've added all the namespaces to my namespace manager but still not able to read any of the tags. See below sample code:

            private XmlNamespaceManager GetNameSpace(XmlNameTable objNameTable)
            {
                XmlNamespaceManager objNsManager = new XmlNamespaceManager(objNameTable);
                objNsManager.AddNamespace("serv", "http://www.webex.com/schemas/2002/06/service");
                objNsManager.AddNamespace("ns1", "http://www.webex.com/schemas/2002/06/service/site");
                objNsManager.AddNamespace("body", "http://www.webex.com/schemas/2002/06/service/training");
                objNsManager.AddNamespace("sess", "http://www.webex.com/schemas/2002/06/session");
                objNsManager.AddNamespace("train", "http://www.webex.com/schemas/2002/06/service/trainingsession");
                objNsManager.AddNamespace("ep", "http://www.webex.com/schemas/2002/06/service/ep");
                objNsManager.AddNamespace("meet", "http://www.webex.com/schemas/2002/06/service/meeting");
                return objNsManager;
            }
    
    var sessionPasswordNodes = xmlDoc.SelectSingleNode("/serv:message/serv:body/serv:bodyContent/sess:accessControl/sess:sessionPassword", GetNameSpace(xmlDoc.NameTable));
    
    var attendeesNodes = xmlDoc.SelectSingleNode("/serv:message/serv:body/serv:bodyContent/train:attendees", GetNameSpace(xmlDoc.NameTable));

    var personsNode = attendeesNodes.SelectNodes("/serv:message/serv:body/serv::bodyContent/train:attendees/sess:participants/sess:participant/sess:person", GetNameSpace(xmlDoc.NameTable));

Anybody has any idea what is wrong with above code and how can I make it working?

Also, if you observe the XML person node is child of attendees and suppose I've already got the attendees node then how can I access persons without passing full path again like I am doing now?

Unfortunately because of some reason I can't use LINQ, so have no option but going with the way I tried, dealing with nodes.

CodePudding user response:

It is better to use LINQ to XML API.

It is available in the .Net Framework since 2007.

c#

void Main()
{
    const string filePath = @"e:\Temp\sunil20000.xml";
    XDocument xdoc = XDocument.Load(filePath);

    XNamespace sess = xdoc.Root.GetNamespaceOfPrefix("sess");
    XNamespace train = xdoc.Root.GetNamespaceOfPrefix("train");

    string sessionPassword = xdoc.Descendants(sess   "sessionPassword")
        .FirstOrDefault()?.Value;

    IEnumerable<XElement> attendees = xdoc.Descendants(train   "attendees");
    IEnumerable<XElement> person = attendees.Descendants(sess   "person");
}

CodePudding user response:

Try xml linq :

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


namespace ConsoleApplication23
{

    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement participants = doc.Descendants().Where(x => x.Name.LocalName == "participants").FirstOrDefault();
            XNamespace  nsSess = participants.GetNamespaceOfPrefix("sess");
            XNamespace nsCom = participants.GetNamespaceOfPrefix("com");

            var people = participants.Descendants(nsSess   "person").Select(x => new
            {
                name = (string)x.Element(nsCom   "name"),
                firstName = (string)x.Element(nsCom   "firstName"),
                lastName = (string)x.Element(nsCom   "lastName"),
                address = (string)x.Descendants(nsCom   "addressType").FirstOrDefault(),
                phones = (string)x.Element(nsCom   "phones"),
                email = (string)x.Element(nsCom   "email"),
                type = (string)x.Element(nsCom   "type")
            }).ToList();

        }
    }
 
}
  • Related