tags:

views:

745

answers:

2

I have a XML File and I want to read the data and assign it to a string array, so Operative would be assign to 1 array and JobLocation to another

<Demo>
    <JOBOperatives>
 <Operative>
  <Clock>aaaa</Clock>
  <Name>aaaaa</Name>
  <MobileNumber>00000000010</MobileNumber>
  <OperativeTrade>3</OperativeTrade>
  <OperativeTicket>1</OperativeTicket>
 </Operative>
    </JOBOperatives>
 <JobLocation>
  <UPRN>aaa</UPRN>
  <Address1>aaaa</Address1>
  <Address2>aaaa</Address2>
  <Address3>aaaa</Address3>
  <Address4>aaa</Address4>
  <Address5>aa</Address5>
  <PostCode>JR4 4ED</PostCode>
 </JobLocation>

+1  A: 

I take it you mean where each property from the xml is it's own element in the array?

That doesn't seem like a very good data structure, especially as xml schema definitions allow for the items to arrive in any order; your expected indexes could get all screwed up. A strongly-typed object seems more appropriate and is well supported in .Net. At very least you should use a dictionary, so the keys are preserved.

In this case the number of items in each tree is very small and you could end up with many of them, so a dictionary is probably not the best choice. You could do objects, but that would be a lot of extra code just to set it up and I get the impression the xml may come from different sources and be different based on the source (or something where the structure could change regularly, hence the initial desire for loosely-typed validation).

Ultimately your destination is a database, so I think in this case I'll show you an example using a dataset:

string xml = GetXmlString(); // <Demo><JobOperatives><Operative><Clock>aaaa</Clock>...
StringReader sr = new StringReader(xml);
DataSet ds = new DataSet();
ds.ReadXml(sr);

Play around with that: look in the dataset's .Tables collection, at each table's .TableName property, .Columns collection, and .Rows collection, and at each columns .ColumnName and .DataType properties.

Joel Coehoorn
I think the OP is looking for two arrays. One where each element is an <operative> node and one where each element is a <JobLocation> node.
EBGreen
Thats correct, unless you know of a better way. I am wanting to read the data validate it then write the data to a SQL Table.
MartGriff
I know, but even then each element in the array is still a field like Address1, instead of a proper property. An object or dictionary would be a much better structure.
Joel Coehoorn
Can you give me some examples?
MartGriff
A: 

The OuterXml property of the XmlNode class might help you here.

David Kemp