V
Vishal Somaiya
Hello
I am trying read from a xml file, pull the values in a object and then add
the object to an ArrayList.
I am using a 'while' loop to move through each node in the xml file and
pulling the required values as needed and setting them to the relevant
property within my object. The reading of the xml file works as it should.
I get a problem when I try to add the object that I created to the
arraylist. It seems to be adding each one, but when I view the results of
the list, it shows the last object passed in for each object stored within
the Arraylist.
Here is the code that is giving me issues:
public ArrayList readXMLClient(string path, string xPathEx)
{
XmlDocument document = new XmlDocument();
document.Load(path);
XPathNavigator navigator = document.CreateNavigator();
int i = document.DocumentElement.ChildNodes.Count;
int j = 0;
ClientData objClientData = new ClientData();
ArrayList objArrayList = new ArrayList();
try
{
XPathNodeIterator _clientCode = navigator.Select(xPathEx +
"/ClientCode");
XPathNodeIterator _clientName = navigator.Select(xPathEx +
"/ClientName");
XPathNodeIterator _clientDetails = navigator.Select( xPathEx +
"/ClientDetails");
while (_clientCode.MoveNext() && _clientName.MoveNext() &&
_clientDetails.MoveNext())
{
objClientData.clientCode = _clientCode.Current.Value.ToString();
objClientData.clientName = _clientName.Current.Value.ToString();
objClientData.clientDetails = _clientDetails.Current.Value.ToString();
objArrayList.Insert(j,objClientData);
j++;
}
}
catch(Exception ex)
{
}
return objArrayList;
}
Any help would be great.
Thanking you in advance
Vish
I am trying read from a xml file, pull the values in a object and then add
the object to an ArrayList.
I am using a 'while' loop to move through each node in the xml file and
pulling the required values as needed and setting them to the relevant
property within my object. The reading of the xml file works as it should.
I get a problem when I try to add the object that I created to the
arraylist. It seems to be adding each one, but when I view the results of
the list, it shows the last object passed in for each object stored within
the Arraylist.
Here is the code that is giving me issues:
public ArrayList readXMLClient(string path, string xPathEx)
{
XmlDocument document = new XmlDocument();
document.Load(path);
XPathNavigator navigator = document.CreateNavigator();
int i = document.DocumentElement.ChildNodes.Count;
int j = 0;
ClientData objClientData = new ClientData();
ArrayList objArrayList = new ArrayList();
try
{
XPathNodeIterator _clientCode = navigator.Select(xPathEx +
"/ClientCode");
XPathNodeIterator _clientName = navigator.Select(xPathEx +
"/ClientName");
XPathNodeIterator _clientDetails = navigator.Select( xPathEx +
"/ClientDetails");
while (_clientCode.MoveNext() && _clientName.MoveNext() &&
_clientDetails.MoveNext())
{
objClientData.clientCode = _clientCode.Current.Value.ToString();
objClientData.clientName = _clientName.Current.Value.ToString();
objClientData.clientDetails = _clientDetails.Current.Value.ToString();
objArrayList.Insert(j,objClientData);
j++;
}
}
catch(Exception ex)
{
}
return objArrayList;
}
Any help would be great.
Thanking you in advance
Vish