XPath Pattern Using XmlNode and XmlAttribute

XML Source (/lib/data/Quotes/Quotes.xml)

<?xml version="1.0" standalone="no"?>
<IRXML CorpMasterID="105011">
  <StockQuotes PubDate="20100125" PubTime="08:58:06">
    <Stock_Quote PrimaryTicker="Yes" DisplayInDropdown="Yes" SecurityID="27714" Company="Derek Reynolds INC" Ticker="DRR" DisplayTicker="DRR" Exchange="NYSE" Currency="US Dollar" CurrencyLabel="$" CurrencyLabelSuffix="" Type="COMPANY" Class="Common Stock" FirstTradeDate="" FinancialStatusIndicator="">
      <Date Date="20100122" Time="16:00:00">Jan 22 2010  4:00PM</Date>
      <Trade>35.42</Trade>
      <Change>-0.69</Change>
      <Bid>35.42</Bid>
      <Ask>35.43</Ask>
      <DataProviderInfo>
        <DataProvider>
          <Name>MarketWatch</Name>
          <Key>DRR</Key>
        </DataProvider>
        <DataProvider>
          <Name>ThomsonChart</Name>
          <Key>DRR;DRR;DRR</Key>
        </DataProvider>
      </DataProviderInfo>
    </Stock_Quote>
  </StockQuotes>
</IRXML>

C# Code:

using System.Xml;

        // Get XML
        string xmlPath = HttpContext.Current.Server.MapPath("/lib/data/Quotes/Quotes.xml");

        // XML Reader
        XmlTextReader xtr = new XmlTextReader(xmlPath);
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xtr);

        // Close reader
        xtr.Close();

        // Get nodes / attributes
        XmlNode stockRow = xmlDoc.SelectSingleNode("/IRXML/StockQuotes/Stock_Quote");
        XmlNode askNode = stockRow.SelectSingleNode("Ask");
        XmlNode bidNode = stockRow.SelectSingleNode("Bid");
        XmlAttribute tickerAttribute = stockRow.Attributes["Ticker"];
        XmlAttribute exchangeAttribute = stockRow.Attributes["Exchange"];

        // Format values from node/attributes
        string ask = (askNode == null) ? "" : askNode.InnerText;
        string bid = (bidNode == null) ? "" : bidNode.InnerText;
        string ticker = (tickerAttribute == null) ? "" : tickerAttribute.InnerText;
        string exchange = (exchangeAttribute == null) ? "" : exchangeAttribute.InnerText;

Another example using sub-selects within the first select node

<?xml version="1.0"  encoding="ISO-8859-1"?>
<Wizard.com_results version="2.1">
  <Hitlist total="332">
    <HIT>
      <COMPANY_PROFILE>
        <SYMBOL>DRR</SYMBOL>
        <CIK>1234</CIK>
        <NAME>DEREK REYNOLDS CO</NAME>
      </COMPANY_PROFILE>
      <FILING_INFO size="11.96 MB" pages="79">
        <FILING_ID>6575040</FILING_ID>
        <ISSUER></ISSUER>
        <FORM>10-Q</FORM>
        <FORM_DESC>Quarterly report which provides a continuing view of a company&apos;s financial position</FORM_DESC>
        <FORM_GROUP>10K,10Q,8K,6K,DEF14</FORM_GROUP>
        <DATE_FILED>20091030</DATE_FILED>
        <HTML_LINK>http://ccbn.10kwizard.com/xml/contents.xml?ipage=6575040&amp;repo=tenk&amp;sXBRL=1</HTML_LINK>
        <DOC_LINK>http://ccbn.10kwizard.com/xml/download.php?format=RTF&amp;ipage=6575040&amp;sXBRL=1</DOC_LINK>
        <PDF_LINK>http://ccbn.10kwizard.com/xml/download.php?format=PDF&amp;ipage=6575040&amp;sXBRL=1</PDF_LINK>
        <XLS_LINK>http://ccbn.10kwizard.com/xml/download.php?format=XLS&amp;ipage=6575040</XLS_LINK>
        <XBRL_LINK></XBRL_LINK>
        <MDA_LINK></MDA_LINK>
      </FILING_INFO>
    </HIT>

Code that enumerates through base XPATH


            // Get XML
            string xmlPath = HttpContext.Current.Server.MapPath(feedPath);

            XmlTextReader xtr = new XmlTextReader(xmlPath);
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xtr);
            // Close reader
            xtr.Close();

            // Get nodes
            XmlNodeList xmlNodes = xmlDoc.DocumentElement.SelectNodes("//Wizard.com_results/Hitlist/HIT");
            // For each xml node, create the corresponding objects
            foreach (XmlNode xmlNode in xmlNodes)
            {

                XmlElement elt = (XmlElement)xmlNode;

                InvestorFilingData node = InvestorFilingDataCreate(xmlNode);

                // If node was created, then add to our list
                if (node != null) { list.Add(node); }
            }

InvestorFilingData method that digs deeper from base node to extract different values from it’s limbs.

 public static InvestorFilingData InvestorFilingDataCreate(XmlNode xmlNode)
    {
        // Create an object to return
        InvestorFilingData item = new InvestorFilingData();

        if (xmlNode != null)
        {

            XmlNode dateFiledNode = xmlNode.SelectSingleNode("FILING_INFO/DATE_FILED");
            XmlNode filingNode = xmlNode.SelectSingleNode("FILING_INFO/FORM");
            XmlNode companyNode = xmlNode.SelectSingleNode("COMPANY_PROFILE/NAME");  
            XmlNode htmlURLNode = xmlNode.SelectSingleNode("FILING_INFO/HTML_LINK");
            XmlNode docURLNode = xmlNode.SelectSingleNode("FILING_INFO/DOC_LINK");
            XmlNode pdfURLNode = xmlNode.SelectSingleNode("FILING_INFO/PDF_LINK");
            XmlNode xlsURLNode = xmlNode.SelectSingleNode("FILING_INFO/XLS_LINK");

            // Size is located in the attributes of FILING_INFO so select this node before 
            // extracting the size attribute
            XmlNode sizeNode = xmlNode.SelectSingleNode("FILING_INFO");
            XmlAttribute sizeAttribute = sizeNode.Attributes["size"];
            
            

            
            // convert the yyyyMMdd format in the XML into a DateTime object     
            item.DateFiled = (dateFiledNode == null) ? DateTime.MinValue :
                System.DateTime.ParseExact(dateFiledNode.InnerText.Trim(), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);            
          
            item.Filing = (filingNode == null) ? "" : filingNode.InnerText.Trim();
            item.CompanyName = (companyNode == null) ? "" : companyNode.InnerText.Trim();
            item.HTMLURL = (htmlURLNode == null) ? "" : htmlURLNode.InnerText.Trim();
            item.DocURL = (docURLNode == null) ? "" : docURLNode.InnerText.Trim();
            item.PDFURL = (pdfURLNode == null) ? "" : pdfURLNode.InnerText.Trim();
            item.XLSURL = (xlsURLNode == null) ? "" : xlsURLNode.InnerText.Trim();
            item.Size = (sizeAttribute == null) ? "" : sizeAttribute.InnerText.Trim();


            return item;
        }

        return null;
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s