从XML文档中选择节点?

时间:2012-03-26 15:50:19

标签: c# fileinfo xmlnode selectsinglenode innertext

所以我有一个xmlDocument,我需要检查是否附加了信用评分。为此,我使用xmlNodes.SelectSingleNode,然后检查innerText。

我的问题是:其中一个节点在实际节点名称中有一个ID字段。所以我认为C#将其解释为节点名称的一部分。

 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就是问题所在。节点在文档中看起来像这样。

<PersPolicy id="AE4562BEE086A92470D4">

我想忽略id部分,因为它会更改每个文档,并且我有数千个要处理的文档。

1 个答案:

答案 0 :(得分:0)

我刚决定使用类来处理节点,如下所示

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); }
    }
}