• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

Read in an XML file that has a namespace defined

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'>
<AppVersion major='2' minor='2' build='0' maintenance='5'/>
<CommConfig DHCP='0' IPAddr='' Mask='' Gateway='' DNS='' HttpRunning='1' TelnetActive='1' FtpActive='1' usbMode='1' WatchdogEnabled='1' SerialConTimeout='0' SNTP='' SNTPHostName='ZACH-PC' sntpHostDisplayMode='1' llrpClientMode='0' llrpPort='5084' llrpHostIP='' 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'/>
<User name='admin' accessLevel='3' PSWD='DADBE0EE74A6528E'/>
<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'/>
<RadioProfileData><RadioRegisterData Address='ffffffff' Data='0'/>
<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();
                    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

1 Solution
käµfm³d 👽Commented:
Use  an XmlNamespaceManager, and assign an artificial prefix when selecting. Also, adjust your XPath--you are not accounting for the root node:


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");

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

Open in new window

axnst2Author Commented:
Perfect!  Worked like a charm!  Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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