Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
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
C#
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 File
1)
XML
<TRANSACTION>
      <szCode>6010000000</szCode>
      <szDescription>Sale Clothes</szDescription>
      <dDebit>0</dDebit>
      <dCredit>122573.0000</dCredit>
      <szTxType>TX_PRODUCT_SALE</szTxType>
    </TRANSACTION>

2)
XML
<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>
Posted
Updated 29-Dec-13 22:24pm
v3
Comments
BillWoodruff 30-Dec-13 3:40am    
Is it the case that a given Node will always have a regular structure: so that you can assume there is some distinct content at positions 4,5 ?

Or, is it the case that: a given Node will vary in the number of ChildNodes ... which would mean you'd need to "skip over" 4 and/or 5 if you recognize they are not present ?
Basmeh Awad 30-Dec-13 4:26am    
i have updated my question please check now.
there will be 2 diffrent files if the case is the first one and if i will read like this

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

it will give error as there are no elements of that node on index 6 or 7

1 solution

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. inside the body of your 'foreach loop:
C#
// 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;
}
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.
 
Share this answer
 
Comments
TrushnaK 30-Dec-13 7:35am    
nice answer....
my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900