Solved

Read in an XML file that has a namespace defined

Posted on 2013-01-07
2
318 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 500 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Syntax 9 36
How to call weather service by using asmx? 4 29
Macro to import XML in Access 2013 2 34
Calculate number of nights between two dates 5 18
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now