是否存在子节点的元素 [英] Whether Element of a Child Node Exist or not

查看:61
本文介绍了是否存在子节点的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i正在读取一个XML文件,但在文件中有时会出现一个子节点,有时候不是这样我需要检查子节点中的索引是否存在

C#代码


i am reading an XML file but in the file sometimes a Child Node is coming and sometimes not so i need to check that index in the Child Node exist
C# Code

foreach (System.Xml.XmlNode ITXTRANChild in ITX_TRAN_list)
                  {
                      DataRow ITX_TRAN_ROW = ITX_TRAN.NewRow();
                      ITX_TRAN_ROW["szCode"] = ITXTRANChild.ChildNodes[0].InnerText.ToString();
                      ITX_TRAN_ROW["szDescription"] = ITXTRANChild.ChildNodes[1].InnerText.ToString();
                      ITX_TRAN_ROW["dDebit"] = ITXTRANChild.ChildNodes[2].InnerText.ToString();
                      ITX_TRAN_ROW["dCredit"] = ITXTRANChild.ChildNodes[3].InnerText.ToString();
//in some files this 4th and 5th index is there and some times not
                      ITX_TRAN_ROW["szAuxValue"] = ITXTRANChild.ChildNodes[4].InnerText.ToString();
                      ITX_TRAN_ROW["szAuxValue2"] = ITXTRANChild.ChildNodes[5].InnerText.ToString();
                      ITX_TRAN_ROW["szTxType"] = ITXTRANChild.ChildNodes[6].InnerText.ToString();
                      ITX_TRAN_ROW["importfilename"] = Path.GetFileName(file).ToString();
                      ITX_TRAN_ROW["importdate"] = DateTime.Now;
                      ITX_TRAN.Rows.Add(ITX_TRAN_ROW);
                  }



XML文件

1)


XML File
1)

<TRANSACTION>
      <szCode>6010000000</szCode>
      <szDescription>Sale Clothes</szDescription>
      <dDebit>0</dDebit>
      <dCredit>122573.0000</dCredit>
      <szTxType>TX_PRODUCT_SALE</szTxType>
    </TRANSACTION>



2)


2)

<TRANSACTION>
      <szCode>6010000000</szCode>
      <szDescription>Sale Clothes</szDescription>
      <dDebit>0</dDebit>
      <dCredit>115417.0000</dCredit>
      <szAuxValue>0</szAuxValue>
      <szAuxValue2>0,0000</szAuxValue2>
      <szTxType>TX_PRODUCT_SALE</szTxType>
    </TRANSACTION>

推荐答案

这是一个想法:假设ITX_TRAN_ROWs你创建的可以处理丢失的条目:



1.在你的'foreach循环开始之内:
Here's one idea: assuming the ITX_TRAN_ROWs you create can handle missing entries:

1. inside the start of your 'foreach loop:
XmlNodeList aux1 = ITXTRANChild.ChildNodes[0].SelectNodes("szAuxValue");
XmlNodeList aux2 = ITXTRANChild.ChildNodes[0].SelectNodes("szAuxValue2");

2。在你的'foreach循环体内:

2. inside the body of your 'foreach loop:

// is a node of Type 'aux1 present
if(aux1.ChildNodes.Count != 0
{
    ITX_TRAN_ROW["szAuxValue"] = aux1[0].InnerText;
}
// is a node of Type 'aux2 present
if(aux2.ChildNodes.Count != 0
{
    ITX_TRAN_ROW["szAuxValue2"] = aux2[0].InnerText;
}

您真的不需要将'InnerText属性转换为字符串:它返回一个字符串。



免责声明:此代码未经过全面测试。您应该对其进行测试。如果是不行,请告诉我,我将删除此回复。

You really don't need to convert the 'InnerText property to a string: it returns a string.

Disclaimer: this code is not fully tested. You should test it. If it doesn't work, let me know, and I'll remove this response.


这篇关于是否存在子节点的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆