Search code examples
c#fileinfoxmlnodeselectsinglenodeinnertext

Selecting a node from within an XML document?


So I have an xmlDocument and I need to check to see if a credit score was appended. To do this I am using xmlNodes.SelectSingleNode and then checking the innerText.

My issue is this: one of the nodes has an ID field in the actual node name. So I think C# is interpreting that as part of the node name.

 public void DeperPostAppend()
    {
        DirectoryInfo CompDir = new DirectoryInfo(FilePrep.CompletedDirectory);

          foreach (FileInfo File in CompDir.GetFiles())
          {
              // Load xml documents for sorting
              XmlDocument xmlDoc = new XmlDocument();
              try
              {
                  xmlDoc.Load(File.FullName);
              }
              catch
              {
                  if (File.Extension != ".xml")
                      return;
              }

            //XmlNode auto = xmlDoc.SelectSingleNode("//ACORD//InsuranceSvcRq//PersAutoPolicyQuoteInqRq");
            XmlNode home = xmlDoc.SelectSingleNode("//ACORD//InsuranceSvcRq//HomePolicyQuoteInqRq");
            XmlNode creditAuto = xmlDoc.SelectSingleNode("//ACORD//InsuranceSvcRq//PersAutoPolicyQuoteInqRq//PersPolicy//CreditScoreInfo//CreditScore");
            XmlNode creditHome = xmlDoc.SelectSingleNode("//ACORD//InsuranceSvcRq//HomePolicyQuoteInqRq//PersPolicy//CreditScoreInfo//CreditScore");

                //if file is type home quote
            if (File.Extension == ".xml" && creditHome != null)
                {
                    if (creditHome.InnerText != "ERR" || creditHome.InnerText != "NOH")
                    {
                        DeperHome();
                    }
                }
                //If file is type Auto Quote
            else if (File.Extension == ".xml" && creditAuto != null)
                {
                    if (creditAuto.InnerText != "ERR" || creditAuto.InnerText != "NOH")
                    {
                        DeperAuto();
                    }
                }
          }

    }//end DeperPostAppend

//ACORD//InsuranceSvcRq//HomePolicyQuoteInqRq//PersPolicy//CreditScoreInfo//CreditScore

PersPolicy is where the issue is. the node looks like this on the document.

<PersPolicy id="AE4562BEE086A92470D4">

I want to ignore the id portion due to the fact that it changes every document and i have thousands of docs to process.


Solution

  • I just decided to use classes to handle the nodes as follows

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;
    
    
    /// <summary>
    /// 
    /// </summary>
    
    public class PersPolicy
    {
        XElement self;
        public PersPolicy(XElement self) { this.self = self; }
    
        public CreditScoreInfo CreditScoreInfo { get { return _CreditScoreInfo ?? (_CreditScoreInfo = new CreditScoreInfo(self.Element("CreditScoreInfo"))); } }
        CreditScoreInfo _CreditScoreInfo;
    
        public string PolicyNumber
        {
            get { return (string)self.Element("PolicyNumber"); }
            set { self.Element("PolicyNumber").SetValue(value); }
        }
    }
    
    /// <summary>
    /// 
    /// </summary>
    public class CreditScoreInfo
    {
        XElement self;
        public CreditScoreInfo(XElement self) { this.self = self; }
    
        public string CreditScore
        {
            get { return (string)self.Element("CreditScore"); }
            set { self.Element("CreditScore").SetValue(value); }
        }
        public string CreditScoreDt
        {
            get { return (string)self.Element("CreditScoreDt"); }
            set { self.Element("CreditScoreDt").SetValue(value); }
        }
    }