Solved

Read in an XML file that has a namespace defined

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PrintingFoundIt(Almost!(Damn!)) 1 30
Problem!!! 8 37
.NET 2008 VB and C# 6 39
Need a modeling tool 2 17
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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