XML reading question

<Config>
<PI Identifier="1" DllName="xxx.dll" Interface="T">
   <Connection Type="Serial" Identifier="1" To="Press" IPAddress="localhost" IPPort="6980">
      <Parameters SerialPort="Computer1" BaudRate="1" Databits="8" Parity="None" Stopbits="1" Timeout="0" CTS="0" DTR="0" Device="1" Node="255" BusType ="PCI">
      </Parameters>
   </Connection>
 </PI>
<PressInterface Identifier="1" DllName="xxxx.dll" Interface="T">
   <Connection Type="TCP/IP" Identifier="1" To="Press" IPAddress="localhost" IPPort="6980">
      <Parameters SerialPort="Computer2" BaudRate="19200" Databits ="8" Parity="None" Stopbits="1" Timeout="0" CTS="0" DTR="0" Device="1" Node="255" BusType="PCI">
      </Parameters>
   </Connection>
 </PI>
</Config>

 // Load XML document, loads into memory
         XmlDocument xmlDoc = new XmlDocument();
         xmlDoc.Load(sConfigFile);
       
         foreach (XmlNode xmlNodeType in xmlDoc.SelectNodes("//Connection[@Type='Serial']"))
         {
            //read the document and pull the node into the Connections box
            string sType = xmlNodeType.Attributes["Type"].InnerText;
            string sConnect = xmlNodeType.Attributes["To"].InnerText;
           **********************************************
            string sSerial = xmlNodeType.Attributes["SerialPort"].InnerText
            string sBaud = xmlNodeType.Attributes["BaudRate"].InnerText;
            string sData = xmlNodeType.Attributes["Databits"].InnerText;
            string sParity = xmlNodeType.Attributes["Parity"].InnerText;
            string sStop = xmlNodeType.Attributes["Stopbits"].InnerText;
            string sTimeout = xmlNodeType.Attributes["Timeout"].InnerText;
            string sCTS = xmlNodeType.Attributes["CTS"].InnerText;
            string sDTR = xmlNodeType.Attributes["DTR"].InnerText;
           **********************************************

For the block within the ***How do I pull the information from the Parameter location on the XML that is under the Connection loction that contains Serial. So i would only need the parameter information from the first PI block in the XML.
LVL 2
NewMom2BrandonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
Try using xmlNodeType.FirstChild.Attributes["..."] rather than just xmlNodeType.Attributes["..."]
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gbzhhuCommented:
If only there is 1 parameters element then Carl_town's solution will work otherwise you wuill have to loop through child nodes (Parameters)

I did a test on carl_tawn's suggestion and works fine, code below

            string sSerial = xmlNodeType.FirstChild.Attributes["SerialPort"].InnerText;
            string sBaud = xmlNodeType.FirstChild.Attributes["BaudRate"].InnerText;
            string sData = xmlNodeType.FirstChild.Attributes["Databits"].InnerText;
            string sParity = xmlNodeType.FirstChild.Attributes["Parity"].InnerText;
            string sStop = xmlNodeType.FirstChild.Attributes["Stopbits"].InnerText;
            string sTimeout = xmlNodeType.FirstChild.Attributes["Timeout"].InnerText;
            string sCTS = xmlNodeType.FirstChild.Attributes["CTS"].InnerText;
            string sDTR = xmlNodeType.FirstChild.Attributes["DTR"].InnerText;

The xml you provided has a mismatched element.  Second PI element reads as <PressInterface but the closing tag is <PI
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

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.