XML reading question

Posted on 2006-04-07
Medium Priority
Last Modified: 2010-04-16
<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">
<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">

 // Load XML document, loads into memory
         XmlDocument xmlDoc = new XmlDocument();
         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.
Question by:NewMom2Brandon
LVL 52

Accepted Solution

Carl Tawn earned 1200 total points
ID: 16401195
Try using xmlNodeType.FirstChild.Attributes["..."] rather than just xmlNodeType.Attributes["..."]
LVL 12

Assisted Solution

gbzhhu earned 800 total points
ID: 16401572
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

809 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