Home > OS >  Unable to get element in XML
Unable to get element in XML

Time:06-16

<?xml version="1.0" encoding="utf-8"?>
<serv:message
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:serv="http://www.webex.com/schemas/2002/06/service" xsi:schemaLocation="http://www.webex.com/schemas/2002/06/service http://www.webex.com/schemas/2002/06/service.xsd">
    <header>
        <securityContext>
            <webExID/>
            <password/>
            <siteID/>
            <partnerID/>
        </securityContext>
    </header>
    <body>
        <bodyContent xsi:type="java:com.webex.service.binding.training.CreateTrainingSession"
            xmlns="http://www.webex.com/schemas/2002/06/service/training"
            xmlns:com="http://www.webex.com/schemas/2002/06/common"
            xmlns:sess="http://www.webex.com/schemas/2002/06/session"
            xmlns:serv="http://www.webex.com/schemas/2002/06/service"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.webex.com/schemas/2002/06/service/training http://www.webex.com/schemas/2002/06/service/training/trainingsession.xsd">
            <sess:accessControl>
                <sess:sessionPassword/>
            </sess:accessControl>
            <sess:schedule></sess:schedule>
            <metaData>
                <sess:confName/>
                <agenda/>
                <description/>
                <greeting/>
                <location/>
            </metaData>
            <enableOptions>
                <chat/>
                <poll/>
                <audioVideo/>
                <fileShare/>
                <applicationShare/>
                <desktopShare/>
                <annotation/>
                <fullScreen/>
                <voip/>
            </enableOptions>
        </bodyContent>
    </body>
</serv:message>

Above XML is standard VILT Create Event xml and I need to populate it with proper data.

The issue is I am able to get "securityContent" element using below code and node holds total count of child elements that is 4:

var node = xmlDoc.SelectNodes("/serv:message/header/securityContext/*", GetNameSpace(xmlDoc.NameTable));

But when I try to get another node i.e. "metaData" then I get Count 0 and way to getting element is exactly same except the path to the element.

Below is sample code that I've tried but not working:

static void Main(string[] args)
{
    var xmlPathh = @"C:\Users\SKMEENA\Desktop\VILT.xml";// this holds above xml
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(xmlPathh);

    var node = xmlDoc.SelectNodes("/serv:message/header/securityContext/*", GetNameSpace(xmlDoc.NameTable));

    var member = xmlDoc.SelectNodes("/serv:message/body/bodyContent/metaData/*", GetNameSpace(xmlDoc.NameTable));
}

static 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");
    return objNsManager;
}

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

CodePudding user response:

The bodyContent node has a default namespace which applies to it and all its children.

You need to add it to the NamespaceManager and then use it in the XPath

var member = xmlDoc.SelectSingleNode("/serv:message/body/body:bodyContent/body:metaData", GetNameSpace(xmlDoc.NameTable));
member.OuterXml.Dump();
static 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");
    return objNsManager;
}

dotnetfiddle

  • Related