从xml中获取节点元素
本文关键字:节点 元素 获取 xml | 更新日期: 2023-09-27 18:01:47
我需要在控制台应用程序中使用c#从XML文件中获取并显示enquiryRecord
节点的第1列和第3列元素。
<?xml version="1.0" encoding="UTF-8"?>
<T24 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.temenos.com/T24/OFSML/130 ofsml13.xsd" xmlns="http://www.temenos.com/T24/OFSML/130">
<serviceResponse>
<ofsStandardEnquiry name="E.FOS.REQ.INFO.1.MCB" status="OK">
<enquiryColumn id="RETURN.CODE" label="RETURN.CODE" type="ALPHANUMERIC"/>
<enquiryColumn id="ERROR.MSG" label="ERROR.MSG" type="ALPHANUMERIC"/>
<enquiryColumn id="CUSTOMER" label="CUSTOMER" type="ALPHANUMERIC"/>
<enquiryColumn id="SHORT.NAME" label="SHORT.NAME" type="ALPHANUMERIC"/>
<enquiryColumn id="IS.INDIVIDUAL" label="IS.INDIVIDUAL" type="ALPHANUMERIC"/>
<enquiryColumn id="IS.MINOR" label="IS.MINOR" type="ALPHANUMERIC"/>
<enquiryColumn id="IS.MCB.STAFF" label="IS.MCB.STAFF" type="ALPHANUMERIC"/>
<enquiryColumn id="IS.ACTIVE.CUSTOMER" label="IS.ACTIVE.CUSTOMER" type="ALPHANUMERIC"/>
<enquiryColumn id="IS.KYC.COMPLIANT" label="IS.KYC.COMPLIANT" type="ALPHANUMERIC"/>
<enquiryColumn id="PHONE" label="PHONE" type="ALPHANUMERIC"/>
<enquiryColumn id="OFF.PHONE" label="OFF.PHONE" type="ALPHANUMERIC"/>
<enquiryColumn id="SMS" label="SMS" type="ALPHANUMERIC"/>
<enquiryColumn id="EMAIL" label="EMAIL" type="ALPHANUMERIC"/>
<enquiryColumn id="DE.ADDRESS.ID" label="DE.ADDRESS.ID" type="ALPHANUMERIC"/>
<enquiryColumn id="STREET.ADDR" label="STREET.ADDR" type="ALPHANUMERIC"/>
<enquiryColumn id="ADDRESS.LINE2" label="ADDRESS.LINE2" type="ALPHANUMERIC"/>
<enquiryColumn id="ADDRESS.LINE3" label="ADDRESS.LINE3" type="ALPHANUMERIC"/>
<enquiryColumn id="TOWN.COUNTRY" label="TOWN.COUNTRY" type="ALPHANUMERIC"/>
<enquiryColumn id="POST.CODE" label="POST.CODE" type="ALPHANUMERIC"/>
<enquiryColumn id="COUNTRY" label="COUNTRY" type="ALPHANUMERIC"/>
<enquiryColumn id="COUNTRY.CODE" label="COUNTRY.CODE" type="ALPHANUMERIC"/>
<enquiryColumn id="CONTACT.ADD" label="CONTACT.ADD" type="ALPHANUMERIC"/>
<enquiryColumn id="JOINT.BORR" label="JOINT.BORR" type="ALPHANUMERIC"/>
<enquiryColumn id="SHORT.NAME" label="SHORT.NAME" type="ALPHANUMERIC"/>
<enquiryColumn id="APPLICATION.DATE" label="APPLICATION.DATE" type="ALPHANUMERIC"/>
<enquiryColumn id="CO.CODE" label="CO.CODE" type="ALPHANUMERIC"/>
<enquiryColumn id="COMPANY.NAME" label="COMPANY.NAME" type="ALPHANUMERIC"/>
<enquiryColumn id="PROCESS.DEFINITION" label="PROCESS.DEFINITION" type="ALPHANUMERIC"/>
<enquiryColumn id="PROCESS.DEFINITION.DESC" label="PROCESS.DEFINITION.DESC" type="ALPHANUMERIC"/>
<enquiryColumn id="FOS.TYPE" label="FOS.TYPE" type="ALPHANUMERIC"/>
<enquiryColumn id="COLLATERAL.ID" label="COLLATERAL.ID" type="ALPHANUMERIC"/>
<enquiryColumn id="COLL.CCY" label="COLL.CCY" type="ALPHANUMERIC"/>
<enquiryColumn id="COLL.AMT" label="COLL.AMT" type="ALPHANUMERIC"/>
<enquiryColumn id="ASSET.ID" label="ASSET.ID" type="ALPHANUMERIC"/>
<enquiryColumn id="MAXIMUM.AMT" label="MAXIMUM.AMT" type="ALPHANUMERIC"/>
<enquiryColumn id="FACILITY.TYPE" label="FACILITY.TYPE" type="ALPHANUMERIC"/>
<enquiryColumn id="RF.LIMIT.TYPE" label="RF.LIMIT.TYPE" type="ALPHANUMERIC"/>
<enquiryColumn id="RF.LIMIT.DESC" label="RF.LIMIT.DESC" type="ALPHANUMERIC"/>
<enquiryColumn id="RF.LIMIT.CCY" label="RF.LIMIT.CCY" type="ALPHANUMERIC"/>
<enquiryColumn id="RF.LIMIT.CCY.DESC" label="RF.LIMIT.CCY.DESC" type="ALPHANUMERIC"/>
<enquiryColumn id="RF.LIMIT.AMT" label="RF.LIMIT.AMT" type="ALPHANUMERIC"/>
<enquiryColumn id="RF.LIMIT.EXP.DATE" label="RF.LIMIT.EXP.DATE" type="ALPHANUMERIC"/>
<enquiryColumn id="RF.LIMIT.REVIEW.DATE" label="RF.LIMIT.REVIEW.DATE" type="ALPHANUMERIC"/>
<enquiryColumn id="CATEGORY" label="CATEGORY" type="ALPHANUMERIC"/>
<enquiryColumn id="CATEGORY.DESC" label="CATEGORY.DESC" type="ALPHANUMERIC"/>
<enquiryColumn id="BOM.PURPOSE" label="BOM.PURPOSE" type="ALPHANUMERIC"/>
<enquiryColumn id="HOUSING.PURPOSE.ID" label="HOUSING.PURPOSE.ID" type="ALPHANUMERIC"/>
<enquiryColumn id="HOUSING.PURPOSE.DESC" label="HOUSING.PURPOSE.DESC" type="ALPHANUMERIC"/>
<enquiryColumn id="TERM" label="TERM" type="ALPHANUMERIC"/>
<enquiryColumn id="NRF.CCY" label="NRF.CCY" type="ALPHANUMERIC"/>
<enquiryColumn id="NRF.CCY.DESC" label="NRF.CCY.DESC" type="ALPHANUMERIC"/>
<enquiryColumn id="NRF.AMT" label="NRF.AMT" type="ALPHANUMERIC"/>
<enquiryRecord>
<column>0</column>
<column/>
<column>1123785</column>
<column>SONAyerG</column>
<column>N</column>
<column/>
<column/>
<column>Y</column>
<column>N</column>
<column>7y0</column>
<column/>
<column/>
<column/>
<column>MU00y.C-1123785.PRINT.1</column>
<column>C.OyL</column>
<column>HOy1</column>
<column/>
<column>VAyrAS</column>
<column/>
<column>yrUS</column>
<column>MU</column>
<column/>
<column/>
<column/>
<column>20ry7</column>
<column>MU0yr001</column>
<column>MCB - T24 R14 SIM</column>
<column>FOS.MCB</column>
<column>Secured Facilities-Non Individual</column>
<column>SECURED</column>
<column>CO.1123785.1|CO.1123785.2|CO.1123785.5|CO.1123785.7|CO.1123785.11|CO.1123785.6|CO.1123785.3</column>
<column>MUR|MUR|MUR|MUR|MUR|MUR|MUR</column>
<column>610000|1014000|1014000|350000|1000000.00|750000.00|1500000</column>
<column>AS.1123785.1]AS.437756.1|AS.1123785.1|AS.437756.2|AS.1123785.5|AS.1123785.1]AS.437756.3]AS.765697.1|AS.1123785.4|AS.1123785.2</column>
<column>1500000]||||]]||</column>
<column>REVOLVING|REVOLVING|REVOLVING|REVOLVING|REVOLVING|REVOLVING</column>
<column>101000|107010|107020|107030|107040|107050</column>
<column>OVERDRAFTS|MISC GTEES|TENDER BONDS|PERF BONDS|ADV PAYMT BONDS|FINANCIAL GTEES</column>
<column>MUR|MUR|MUR|MUR|MUR|MUR</column>
<column>Mauritian Rupee|Mauritian Rupee|Mauritian Rupee|Mauritian Rupee|Mauritian Rupee|Mauritian Rupee</column>
<column>2800000.00|1600000.00|1600000.00|1600000.00|1600000.00|1600000.00</column>
<column>29990101|29990101|29990101|29990101|29990101|29990101</column>
<column>20160430|20160430|20160430|20160430|20160430|20160430</column>
<column>|||||</column>
<column>|||||</column>
<column>|||||</column>
<column>|||||</column>
<column>|||||</column>
<column>|||||</column>
<column>|||||</column>
<column>|||||</column>
<column>|||||</column>
</enquiryRecord>
</ofsStandardEnquiry>
</serviceResponse>
</T24>
代码:
var columns = XDocument.Load("XML01.xml").Root
.Descendants("enquiryRecord")
.Descendants("column")
.ToList();
Console.WriteLine((string)columns.ElementAtOrDefault(5)); // 0
Console.WriteLine((string)columns.ElementAtOrDefault(10)); // data
Console.ReadLine();
我该怎么做?
XNamespace ns = "http://www.temenos.com/T24/OFSML/130";
var columns = XDocument.Load("XML01.xml")
.Descendants(ns + "enquiryRecord")
.Descendants(ns + "column")
.ToList();
Console.WriteLine(columns.ElementAtOrDefault(0)?.Value); // 0
Console.WriteLine(columns.ElementAtOrDefault(2)?.Value); // data
// Or
Console.WriteLine((string)columns.ElementAtOrDefault(0)); // 0
Console.WriteLine((string)columns.ElementAtOrDefault(2)); // data
-
ElementAtOrDefault
-避免IndexOutOfRange
异常如果没有这样的位置。 -
?.
-避免NullReferenceException
在位置不存在的情况下返回默认值
问题是上面的代码缺少对这些元素的命名空间的添加。
您可以使用XPath解析XML
var xDocument = XDocument.Load(fileName);
var selectedNodes = xDocument.Root.XPathSelectElements("./node2/ofsStandardEnquiry/enquiryRecord/column");
var nodeArray = selectedNodes.Select(s => s.Value).ToArray();
Console.WriteLine(nodeArray[0]);
Console.WriteLine(nodeArray[2]);
我相信这是可以改进的,但是当我需要解析XML文件时,我通常使用XPath。