?
Solved

Read in an XML file that has a namespace defined

Posted on 2013-01-07
2
Medium Priority
?
350 Views
Last Modified: 2013-01-07
Hi Experts,

I have the below XML file which I cannot change!

<?xml version='1.0'?>
<Motorola xmlns:Falcon='http://www.domain.com/RFID/Readers/Config/Falcon' xmlns='http://www.domain.com/RFID/Readers/Config/Falcon'>
<Config>
<AppVersion major='2' minor='2' build='0' maintenance='5'/>
<CommConfig DHCP='0' IPAddr='192.168.1.33' Mask='255.255.255.0' Gateway='0.0.0.0' DNS='0.0.0.0' HttpRunning='1' TelnetActive='1' FtpActive='1' usbMode='1' WatchdogEnabled='1' SerialConTimeout='0' SNTP='192.168.1.152' SNTPHostName='ZACH-PC' sntpHostDisplayMode='1' llrpClientMode='0' llrpPort='5084' llrpHostIP='192.168.127.2' allowllrpConnOverride='0'/>
<RegionConfig RFBoard='0' RFCountry='United States' RFRegulatory='US FCC 15' RFScanMode='0' LBTEnable='0' ChannelData='FFFFFFFFFFFFFFFF' MACLinkProfile='65535'/>
<SnmpConfig snmpVersion='1' heartbeat='1' epcgReadPointOperStateNotifyEnable='15' epcgReadPointOperNotifyFromState='0' epcgReadPointOperNotifyToState='0' epcgReadPointOperNotifyStateLevel='28080' epcgAntRdPntReadFailureNotifEnable='15' epcgAntRdPntReadFailureNotifLevel='28080' epcgAntRdPntWriteFailureNotifEnable='15' epcgAntRdPntWriteFailureNotifLevel='28080' epcgAntRdPntKillFailureNotifEnable='15' epcgAntRdPntKillFailureNotifLevel='28080' epcgAntRdPntEraseFailureNotifEnable='15' epcgAntRdPntEraseFailureNotifLevel='28080' epcgAntRdPntLockFailureNotifEnable='15' epcgAntRdPntLockFailureNotifLevel='28080' epcgIoPortOperStatusNotifEnable='15' epcgIoPortOperStatusNotifLevel='28080' epcgRdrDevOperNotifStateLevel='6' epcgRdrDevOperStateEnable='1' epcgRdrDevOperNotifFromState='0' epcgRdrDevOperNotifToState='0'/>
<UserList>
<User name='admin' accessLevel='3' PSWD='DADBE0EE74A6528E'/>
</UserList>
<IPReader name='Advanced Reader' desc='Advanced Reader' flags='0' MonoStatic='0' CheckAntenna='1' gpiDebounceTime='0' contact='Company Inc'>
<ReadPoint name='Read Point 1' flags='0'/>
<ReadPoint name='Read Point 2' flags='1'/>
<ReadPoint name='Read Point 3' flags='1'/>
<ReadPoint name='Read Point 4' flags='1'/>
<ReadPoint name='Read Point 5' flags='0'/>
<ReadPoint name='Read Point 6' flags='0'/>
<ReadPoint name='Read Point 7' flags='0'/>
</IPReader>
</Config>
<MOTOROLA_LLRP_CONFIG><LLRP_READER_CONFIG />
</MOTOROLA_LLRP_CONFIG>
<RadioProfileData><RadioRegisterData Address='ffffffff' Data='0'/>
</RadioProfileData>
<CustomProfileData ForceEAPMode='0' MaxNumberOfTagsBuffered='512' EnableDefaultFilter='false'/>
</Motorola >

Open in new window


How can I read in and/or set the value of Motorola.Config.CommConfig.IPAddr?  I tried this but it doesn't seem to work:

                   XmlDocument document = new XmlDocument();
                    document.Load(path);
                    XmlElement xmlRoot = document.DocumentElement;

                    XmlNode node = xmlRoot.SelectSingleNode("/Config/CommConfig");

Open in new window


node comes back as null! Eventhough, I can see that Motorola has a child node called Config

Thanks
0
Comment
Question by:axnst2
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38752051
Use  an XmlNamespaceManager, and assign an artificial prefix when selecting. Also, adjust your XPath--you are not accounting for the root node:

e.g.

using System.Xml;

namespace ConsoleApplication52
{
    class Program
    {
        private static string path = "input.xml";

        static void Main(string[] args)
        {
            XmlDocument document = new XmlDocument();
            XmlNamespaceManager mgr = new XmlNamespaceManager(document.NameTable);

            mgr.AddNamespace("pre", "http://www.domain.com/RFID/Readers/Config/Falcon");
            document.Load(path);

            XmlElement xmlRoot = document.DocumentElement;
            XmlNode node = xmlRoot.SelectSingleNode("//pre:Config/pre:CommConfig", mgr);
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:axnst2
ID: 38752144
Perfect!  Worked like a charm!  Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Integration Management Part 2
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question