XML reading question

Posted on 2006-04-07
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

    Try using xmlNodeType.FirstChild.Attributes["..."] rather than just xmlNodeType.Attributes["..."]
    LVL 12

    Assisted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    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.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now